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

import BFFramework
import UIKit

class MVBannerCell: UICollectionViewCell {
    // 做同款
    var reCreateBtnClicHandle: ((_ sender: UIButton, _ videoInfo: PQVideoListModel?) -> Void)?
    let musicWidth: CGFloat = (9.0 / 16.0 * (cScreenHeigth - (cDevice_iPhoneStatusBarHei + 45 + 60 + (cDefaultMargin * 2 + cSafeAreaHeight) + cDefaultMargin * 3 + 15))) - 15 - (16 + cDefaultMargin) * 2 - (10 + 106 + 10)
    // 封面
    lazy var converView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        iv.addCorner(corner: 8)
        return iv
    }()

    lazy var progressView: UIProgressView = {
        let progressView = UIProgressView(progressViewStyle: .default)
        progressView.progressTintColor = .white
        return progressView

    }()

//    lazy var backShadowView: UIView = {
//        let backShowView = UIView()
//        backShowView.layer.masksToBounds = false
//        backShowView.layer.shadowColor = UIColor.black.cgColor
//        backShowView.layer.shadowOpacity = 0.3 // 设置阴影的透明度
//        backShowView.layer.shadowRadius = 4
//        backShowView.layer.shadowOffset = CGSize(width: 0, height: 4) // 设置阴影的偏移量
//        return backShowView
//    }()

    // 下面 marks view
    lazy var bottmMaskView: UIImageView = {
        let bottmMaskView = UIImageView(image: UIImage(named: "home_marks"))
        bottmMaskView.isUserInteractionEnabled = true
        bottmMaskView.addCorner(corner: 8)
        return bottmMaskView
    }()

    // 暂停view
    lazy var pauseView: UIImageView = {
        let pauseView = UIImageView(image: UIImage(named: "video_pause"))
        pauseView.isHidden = false
        return pauseView
    }()

    /// 音乐内容view
    lazy var musicContenView: UIView = {
        let musicContenView = UIView()
        return musicContenView
    }()

    // 音乐图片
    lazy var musicImageView: UIImageView = {
        let musicImageView = UIImageView(image: UIImage(named: "musicName_icon"))
        musicImageView.showLoadingAnimation(duration: 3)
        return musicImageView
    }()

    // 音乐背景
    lazy var musicBgView: UIImageView = {
        let musicBgView = UIImageView(image: UIImage(named: "musicName_bg"))
        return musicBgView
    }()
    
    /// 标题
    lazy var titleLabel:  UILabel = {
        let titleLabel = UILabel()
        titleLabel.textAlignment = .left
        titleLabel.textColor = .white
        titleLabel.numberOfLines=0
        titleLabel.lineBreakMode = .byCharWrapping
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        return titleLabel
    }()

    /// 音乐歌曲名称
    lazy var musicNameLab: LMJHorizontalScrollText = {
        let musicNameLab = LMJHorizontalScrollText(frame: CGRect(x: 12 + cDefaultMargin, y: 0, width: musicWidth, height: 32))
        musicNameLab.textColor = UIColor.white
        musicNameLab.textFont = UIFont.systemFont(ofSize: 15, weight: .semibold)
        musicNameLab.speed = 0.03
        musicNameLab.moveDirection = LMJTextScrollMoveLeft
        musicNameLab.moveMode = LMJTextScrollContinuous
        musicNameLab.stop()
        return musicNameLab
    }()

    lazy var reCreateBtn: UIButton = {
        let reCreateBtn = UIButton(type: .custom)
        reCreateBtn.setTitle(" 使用音乐", for: .normal)
        reCreateBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
        reCreateBtn.setTitleColor(UIColor.white, for: .normal)
        reCreateBtn.addCorner(corner: 8)
        reCreateBtn.setImage(UIImage(named: "reCreate"), for: .normal)
        reCreateBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        reCreateBtn.backgroundColor = UIColor(red: 61.0 / 255.0, green: 193.0 / 255.0, blue: 193.0 / 255.0, alpha: 0.8)
        return reCreateBtn
    }()

    // 视频分类
    lazy var typeImage: UIImageView = {
        let typeImage = UIImageView(image: UIImage(named: "categary_icon"))
        return typeImage
    }()

    // 视频分类
    lazy var typeLab: UILabel = {
        let typeLab = UILabel()
        typeLab.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
        typeLab.textColor = UIColor.white
        return typeLab
    }()

    override func prepareForReuse() {
        musicNameLab.stop()
    }

    var videoData: PQVideoListModel? {
        didSet {
            addLayout()
            addData()
      
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
//        contentView.addSubview(backShadowView)
        contentView.addSubview(converView)
        contentView.addSubview(bottmMaskView)
        contentView.addSubview(pauseView)
        bottmMaskView.addSubview(progressView)
        bottmMaskView.addSubview(typeImage)
        bottmMaskView.addSubview(titleLabel)
        
        
        bottmMaskView.addSubview(typeLab)
        bottmMaskView.addSubview(reCreateBtn)
        bottmMaskView.addSubview(musicContenView)
        musicContenView.addSubview(musicBgView)
        musicContenView.addSubview(musicImageView)
        musicBgView.addSubview(musicNameLab)
    }

    func addData() {
        typeLab.text = "\(videoData?.categoryName ?? "")"
        BFLog(message: "videoDatavideoData \(String(describing: videoData?.title))")

     
       let textSize =  sizeWithText(text: videoData?.title ?? "", font: UIFont.systemFont(ofSize: 16, weight: .bold), size: CGSize.init(width:    adapterWidth(width: 300), height: CGFloat.greatestFiniteMagnitude))
        
        BFLog(message: "XXXXXXX videoData?.title is \(String(describing: videoData?.title)) height \(textSize.height)  width is \(reCreateBtn.frame.maxX - 12)")
        titleLabel.snp.remakeConstraints { make in
            make.width.equalTo(  adapterWidth(width: 300))
            make.height.equalTo(textSize.height + 2)
            make.left.equalToSuperview().offset(12)
            make.bottom.equalTo(musicContenView.snp_top).offset(-cDefaultMargin)
        }
        BFLog(message: "videoData?.title is \(String(describing: videoData?.title)) height \(textSize.height)")
        titleLabel.text = videoData?.title ?? ""
   

        
        if videoData?.categoryImage != nil {
            typeImage.setNetImage(url: "\(videoData?.categoryImage ?? "")", placeholder: UIImage(named: "categary_icon")!)
        } else {
            typeImage.image = UIImage(named: "categary_icon")
        }
        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: converView)
        let nameWidth: CGFloat = sizeWithText(text: "\(videoData?.reCreateVideoData?.rhythmMusicName ?? "")  ", font: UIFont.systemFont(ofSize: 15, weight: .semibold), size: CGSize(width: cScreenWidth, height: 32)).width
        musicNameLab.stop()
        if nameWidth > musicWidth {
            musicNameLab.move()
            musicNameLab.text = "\(videoData?.reCreateVideoData?.rhythmMusicName ?? "")  "
        } else {
            musicNameLab.stop()
            musicNameLab.text = "\(videoData?.reCreateVideoData?.rhythmMusicName ?? "")                                  "
        }
    }

    ///
    /// - Returns: <#description#>
    func addLayout() {
//        backShadowView.snp.remakeConstraints { make in
//            make.width.equalToSuperview()
//            make.height.equalToSuperview()
//            make.left.equalToSuperview()
//            make.top.equalToSuperview()
//        }

        converView.snp.remakeConstraints { make in
            make.left.bottom.top.right.equalToSuperview()
        }

        pauseView.snp.remakeConstraints { make in
            make.width.equalTo(82)
            make.height.equalTo(82)
            make.center.equalToSuperview()
        }
        
        

        bottmMaskView.snp.remakeConstraints { make in
            make.width.equalToSuperview()
            make.height.equalTo(150 + 100)
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
        }

        progressView.snp.remakeConstraints { make in
            make.height.equalTo(2)
            make.left.equalToSuperview().offset(3)
            make.right.equalToSuperview().offset(-3)
            make.bottom.equalToSuperview()
        }

        reCreateBtn.snp.remakeConstraints { make in
            make.width.equalTo(106)
            make.height.equalTo(46)
            make.right.equalToSuperview().offset(-cDefaultMargin)
            make.bottom.equalTo(progressView.snp_top).offset(-cDefaultMargin * 2)
        }

        musicContenView.snp.remakeConstraints { make in
            make.centerY.equalTo(reCreateBtn)
            make.height.equalTo(32)
            make.right.equalTo(reCreateBtn.snp_left).offset(-cDefaultMargin)
            make.left.equalToSuperview().offset(cDefaultMargin)
        }

        musicImageView.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.width.height.equalTo(musicContenView.snp_height)
        }

        musicBgView.snp.makeConstraints { make in
            make.left.equalTo(musicImageView.snp_centerX)
            make.right.top.height.equalToSuperview()
        }

        if(videoData?.title?.count ?? 0 > 0){
            typeImage.snp.remakeConstraints { make in
                make.left.equalTo(musicContenView.snp_left)
                make.height.width.equalTo(18)
                make.bottom.equalTo(titleLabel.snp_top).offset(-8)
            }
        }else{
            typeImage.snp.remakeConstraints { make in
                make.left.equalTo(musicContenView.snp_left)
                make.height.width.equalTo(18)
                make.bottom.equalTo(musicContenView.snp_top).offset(-cDefaultMargin)
            }
        }
   
        typeLab.snp.remakeConstraints { make in
            make.left.equalTo(typeImage.snp_right).offset(5)
            make.centerY.equalTo(typeImage)
            make.right.equalTo(musicContenView)
        }
    }

    @objc func btnClick(sender: UIButton) {
        if reCreateBtnClicHandle != nil {
            reCreateBtnClicHandle!(sender, videoData)
        }
    }

    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
