//
//  MVPlayControlViewCell.swift
//  MusicVideoPlus
//
//  Created by SanW on 2021/6/22.
//


import UIKit

class MVPlayControlViewCell: UICollectionViewCell {
    let musicWidth: CGFloat = cScreenWidth - 12 - (16 + cDefaultMargin) * 2 - (12 + cDefaultMargin * 7 + 12)
    var btnClickHandle: ((_ sender: UIButton, _ videoData: PQVideoListModel?) -> Void)?
    lazy var coverImageView: UIImageView = {
        let coverImageView = UIImageView()
        coverImageView.contentMode = .scaleAspectFit
        coverImageView.clipsToBounds = true
        coverImageView.isUserInteractionEnabled = true
        coverImageView.tag = cCellTag
        coverImageView.backgroundColor = UIColor.black
        let ges = UITapGestureRecognizer(target: self, action: #selector(tagClick))
        coverImageView.addGestureRecognizer(ges)
        return coverImageView
    }()

    lazy var maskImageView: UIImageView = {
        let maskImageView = UIImageView(image: UIImage(named: "play_mask"))
        maskImageView.contentMode = .scaleAspectFill
        maskImageView.clipsToBounds = true
        return maskImageView
    }()

    lazy var pauseBtn: UIButton = {
        let pauseBtn = UIButton(type: .custom)
        pauseBtn.setImage(UIImage(named: "video_pause"), for: .normal)
        pauseBtn.setImage(UIImage(named: "icon_video_refresh"), for: .selected)
        pauseBtn.tag = 1
        pauseBtn.isUserInteractionEnabled = false
//        pauseBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        pauseBtn.isHidden = true
        return pauseBtn
    }()

    lazy var watchCountLab: UILabel = {
        let watchCountLab = UILabel()
        watchCountLab.addShadow()
        watchCountLab.textColor = UIColor.white
        watchCountLab.font = UIFont.systemFont(ofSize: 11)
        return watchCountLab
    }()

    lazy var nickNameBtn: UIButton = {
        let nickNameBtn = UIButton(type: .custom)
        nickNameBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        nickNameBtn.titleLabel?.lineBreakMode = .byTruncatingTail
        nickNameBtn.contentHorizontalAlignment = .left
        nickNameBtn.setTitleColor(UIColor.white, for: .normal)
        nickNameBtn.tag = 2
        nickNameBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        return nickNameBtn
    }()

    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 = 5
        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: 17, weight: .semibold)
        reCreateBtn.setTitleColor(UIColor.white, for: .normal)
        reCreateBtn.addCorner(corner: 8)
        reCreateBtn.setImage(UIImage(named: "reCreate"), for: .normal)
        reCreateBtn.tag = 3
        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: 1)
        reCreateBtn.eventInterval = 0.5
        return reCreateBtn
    }()

    lazy var wechatImage: UIButton = {
        let wechatImage = UIButton(type: .custom)
        wechatImage.setImage(UIImage(named: "mi_upload_share_friend"), for: .normal)
        wechatImage.tag = 4
        wechatImage.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        wechatImage.layer.shadowColor = UIColor(white: 0, alpha: 0.5).cgColor
        wechatImage.layer.shadowOffset = CGSize(width: 2, height: 2)
        return wechatImage
    }()

    lazy var wechatLab: UILabel = {
        let wechatLab = UILabel()
        wechatLab.textAlignment = .center
        wechatLab.textColor = UIColor.white
        wechatLab.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        wechatLab.text = "分享好友"
        wechatLab.addShadow()
        wechatLab.layer.shadowColor = UIColor(white: 0, alpha: 0.5).cgColor
        wechatLab.layer.shadowOffset = CGSize(width: 2, height: 2)
        return wechatLab
    }()

    lazy var wechatFriImage: UIButton = {
        let wechatFriImage = UIButton(type: .custom)
        wechatFriImage.setImage(UIImage(named: "mi_upload_share_timeline"), for: .normal)
        wechatFriImage.tag = 5
        wechatFriImage.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        wechatFriImage.layer.shadowColor = UIColor(white: 0, alpha: 0.5).cgColor
        wechatFriImage.layer.shadowOffset = CGSize(width: 2, height: 2)
        return wechatFriImage
    }()

    lazy var wechatFriLab: UILabel = {
        let wechatFriLab = UILabel()
        wechatFriLab.textAlignment = .center
        wechatFriLab.textColor = UIColor.white
        wechatFriLab.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        wechatFriLab.text = "分享朋友圈"
        wechatFriLab.addShadow()
        wechatFriLab.layer.shadowColor = UIColor(white: 0, alpha: 0.5).cgColor
        wechatFriLab.layer.shadowOffset = CGSize(width: 2, height: 2)
        return wechatFriLab
    }()

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

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(coverImageView)
        contentView.addSubview(maskImageView)
        contentView.addSubview(pauseBtn)
        contentView.addSubview(watchCountLab)
        contentView.addSubview(nickNameBtn)
        contentView.addSubview(titleLabel)
        contentView.addSubview(musicContenView)
        musicContenView.addSubview(musicBgView)
        musicContenView.addSubview(musicImageView)
        musicBgView.addSubview(musicNameLab)
        contentView.addSubview(reCreateBtn)
        contentView.addSubview(wechatImage)
        contentView.addSubview(wechatLab)
        contentView.addSubview(wechatFriImage)
        contentView.addSubview(wechatFriLab)
        contentView.addSubview(progressView)
    }

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

    @objc class func playControlCell(collectionView: UICollectionView, indexPath: IndexPath) -> MVPlayControlViewCell {
        let cell: MVPlayControlViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: MVPlayControlViewCell.self), for: indexPath) as! MVPlayControlViewCell
        return cell
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        pauseClick()
        progressView.progress = 0
    }

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

    func addData() {
        pauseClick()
        progressView.progress = 0
        if (videoData?.height ?? 0) <= (videoData?.width ?? 0) {
            coverImageView.contentMode = .scaleAspectFit
        } else {
            coverImageView.contentMode = .scaleAspectFill
        }
        coverImageView.clipsToBounds = true
        let coverImg = (videoData?.videoCoverSnapshotPath != nil && (videoData?.videoCoverSnapshotPath?.count ?? 0) > 0) ? videoData?.videoCoverSnapshotPath ?? "" : videoData?.coverImg?["coverImgPath"] as! String
        coverImageView.setNetImage(url: coverImg)
        if videoData?.shareCountFriend != nil, videoData?.shareCountFriend ?? 0 > 0 {
            wechatLab.text = (videoData?.shareCountFriend ?? 0).changeUnit()
        } else {
            wechatLab.text = "分享好友"
        }
        if videoData?.shareCount != nil, videoData?.shareCount ?? 0 > 0 {
            wechatFriLab.text = (videoData?.shareCount ?? 0).changeUnit()
        } else {
            wechatFriLab.text = "分享朋友圈"
        }
        watchCountLab.text = "\(videoData?.playCountFormatStr ?? "0")次观看"
        nickNameBtn.setTitle("@\(videoData?.userInfo?.nickName ?? "")", for: .normal)
        titleLabel.text = videoData?.title ?? ""
        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 ?? "")                                                                    "
        }
    }

    func updateProgress(progress: Float) {
        progressView.setProgress(progress, animated: false)
    }

    func addLayout() {
        let shareW: CGFloat = cDefaultMargin * 7
        let margin: CGFloat = 12
        let musicH: CGFloat = 32
        coverImageView.snp.remakeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }

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

        pauseBtn.snp.remakeConstraints { make in
            make.width.height.equalTo(shareW)
            make.center.equalToSuperview()
        }

        reCreateBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(margin)
            make.right.equalToSuperview().offset(-margin)
            make.height.equalTo(margin * 4)
            make.bottom.equalToSuperview().offset(-(cSafeAreaHeight + margin * 2))
        }

        wechatFriLab.snp.remakeConstraints { make in
            make.bottom.equalTo(reCreateBtn.snp.top).offset(-cDefaultMargin * 2)
            make.right.equalTo(reCreateBtn)
            make.width.equalTo(shareW)
        }

        wechatFriImage.snp.remakeConstraints { make in
            make.bottom.equalTo(wechatFriLab.snp.top)
            make.width.height.equalTo(shareW)
            make.right.equalTo(wechatFriLab)
        }

        wechatLab.snp.remakeConstraints { make in
            make.bottom.equalTo(wechatFriImage.snp.top).offset(-margin)
            make.width.height.right.equalTo(wechatFriLab)
        }
        wechatImage.snp.remakeConstraints { make in
            make.bottom.equalTo(wechatLab.snp.top)
            make.width.height.equalTo(shareW)
            make.right.equalTo(wechatFriLab)
        }

        musicContenView.snp.remakeConstraints { make in
            make.height.equalTo(musicH)
            make.right.equalTo(wechatFriLab.snp.left).offset(-margin)
            make.left.equalTo(reCreateBtn)
            make.bottom.equalTo(reCreateBtn.snp.top).offset(-cDefaultMargin * 2)
        }

        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()
        }

        titleLabel.snp.remakeConstraints { make in
            make.bottom.equalTo(musicContenView.snp.top).offset(-margin)
            make.left.equalTo(reCreateBtn)
            make.right.equalTo(musicContenView)
        }

        nickNameBtn.snp.remakeConstraints { make in
            make.left.equalTo(reCreateBtn)
            make.right.equalTo(musicContenView)
            make.bottom.equalTo(titleLabel.snp.top).offset(-margin / 2)
        }

        watchCountLab.snp.remakeConstraints { make in
            make.left.equalTo(reCreateBtn)
            make.right.equalTo(musicContenView)
            make.bottom.equalTo(nickNameBtn.snp.top).offset(-margin / 2)
        }

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

    func pauseClick(isHidden: Bool = true, isSelected: Bool = false) {
        pauseBtn.isHidden = isHidden
        pauseBtn.isSelected = isSelected
    }

    @objc func tagClick() {
        if pauseBtn.isSelected, !pauseBtn.isHidden, !isNetConnected() {
            cShowHUB(superView: self, msg: "没有网络连接")
            return
        }
        pauseClick(isHidden: !pauseBtn.isHidden)
        btnClick(sender: pauseBtn)
    }

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