//
//  GXBannerTestCell.swift
//  GXBannerSample
//
//  Created by Gin on 2020/7/24.
//  Copyright © 2020 gin. All rights reserved.
//

import UIKit
import BFFramework

class MVBannerCell: UICollectionViewCell {
    
    lazy var iconIView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        return iv
    }()
    
    lazy var bottmMaskView: UIImageView = {
        let bottmMaskView = UIImageView.init(image: UIImage.init(named: "home_marks"))
        
        return bottmMaskView
    }()
    
    lazy var pauseView: UIImageView = {
        let pauseView = UIImageView.init(image: UIImage.init(named: "video_pause"))
        pauseView.isHidden = false
        return pauseView
    }()
    
    
    /// 音乐歌曲名称
    lazy var musicNameLab: LMJHorizontalScrollText = {
        let name:String = "文件名称"
        let nameWidth: CGFloat = sizeWithText(text: "\(name)", font: UIFont.systemFont(ofSize: 13), size: CGSize(width: cScreenWidth - ((cDefaultMargin * 6 + 16 * 2) * 2) - (25 + cDefaultMargin * 3), height: cDefaultMargin * 3)).width
        let musicNameLab = LMJHorizontalScrollText(frame: CGRect(x: 0, y: 0, width: nameWidth < cDefaultMargin * 4 ? cDefaultMargin * 4 : nameWidth, height: cDefaultMargin * 3))
        musicNameLab.textColor = UIColor.white
        musicNameLab.textFont = UIFont.systemFont(ofSize: 13)
        musicNameLab.speed = 0.03
        musicNameLab.moveDirection = LMJTextScrollMoveLeft
        musicNameLab.moveMode = LMJTextScrollContinuous
        if nameWidth < cDefaultMargin * 4 {
            musicNameLab.text = " \(name) "
        } else {
            musicNameLab.text = "\(name)"
        }
        return musicNameLab
    }()
    
    
    
    /// 音乐标题
    lazy var musicNameView: UIView = {
        let musicNameView = UIView()
        musicNameView.addSubview(musicNameLab)
        let nameWidth: CGFloat = musicNameLab.frame.width + (25 + cDefaultMargin * 3)
        musicNameView.frame = CGRect(x: 16, y: 492, width: nameWidth, height: cDefaultMargin * 3)
//        musicNameView.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
        musicNameView.addCorner(corner: musicNameView.frame.height / 2)
    
        let musicImageView = UIImageView(image:UIImage.init().BF_Image(named: "stuckPoint_reCreate_music"))
        musicImageView.frame = CGRect(x: musicNameView.frame.height / 2 - 5, y: (musicNameView.frame.height - 22) / 2, width: 22, height: 22)
        musicNameView.addSubview(musicImageView)
        musicNameLab.frame.origin.x = musicImageView.frame.maxX + 5
        return musicNameView
    }()
    
    //做同款
    var reCreateBtnClicHandle: ((_ sender: UIButton, _ videoInfo: PQVideoListModel?) -> Void)?
    lazy var reCreateBtn: UIButton = {
        let reCreateBtn = UIButton(type: .custom)
        reCreateBtn.addCorner(corner: 8)
        reCreateBtn.setImage(UIImage(named: "reCreate"), for: .normal)
        reCreateBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        reCreateBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#3DC1C1")
        return reCreateBtn
    }()
    
    //视频分类
    lazy var typeLabe: UILabel = {
        let typeLabe = UILabel()
        typeLabe.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.2)
        typeLabe.alpha = 0.8
        typeLabe.textColor = .white
        typeLabe.text = "热门"
        typeLabe.textAlignment = .center
        typeLabe.addCorner()
        typeLabe.font = UIFont.systemFont(ofSize: 13)
        return typeLabe
    }()
    
    
    var videoData: PQVideoListModel? {
        didSet {
            addData()

        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.iconIView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)

        
        self.typeLabe.snp.remakeConstraints { make in
            make.width.equalTo(50)
            make.height.equalTo(24)
            make.left.equalToSuperview().offset(17)
            make.bottom.equalToSuperview().offset(-14)
        }
        
        self.bottmMaskView.snp.remakeConstraints { make in
            make.width.equalTo(630 / 2)
            make.height.equalTo(150)
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        self.reCreateBtn.snp.remakeConstraints { make in
            make.width.equalTo(92)
            make.height.equalTo(46)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-20)
        }
        
        self.pauseView.snp.remakeConstraints { make in
            make.width.equalTo(72)
            make.height.equalTo(82)
            make.center.equalToSuperview()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.addSubview(self.iconIView)
        self.contentView.addSubview(self.bottmMaskView)
        self.contentView.addSubview(self.typeLabe)
        self.contentView.addSubview(self.musicNameView)
        self.contentView.addSubview(self.reCreateBtn)
        self.contentView.addSubview(self.pauseView)
    
    }
    
    

    func addData() {
        typeLabe.text = videoData?.categoryName
        
        musicNameLab.text = videoData?.reCreateVideoData?.rhythmMusicName ?? ""

        let coverImg = (videoData?.videoCoverSnapshotPath != nil && (videoData?.videoCoverSnapshotPath?.count ?? 0) > 0) ? videoData?.videoCoverSnapshotPath ?? "" : (videoData?.coverImg?["coverImgPath"] as? String ?? "")
        BFLog(message: "coverImg url is \(coverImg)")
        netImage(url: coverImg, mainView: iconIView)
    }
    
    @objc func btnClick(sender: UIButton) {
        if reCreateBtnClicHandle != nil {
            reCreateBtnClicHandle!(sender, videoData)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
