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


import UIKit

class MVMineProductCell: UICollectionViewCell {
    var btnClickHandle: ((_ sender: UIButton, _ videoData: PQVideoListModel?) -> Void)?

    lazy var coverImageView: UIImageView = {
        let coverImageView = UIImageView()
//        coverImageView.contentMode = .scaleAspectFill
//        coverImageView.clipsToBounds = true
        coverImageView.isUserInteractionEnabled = false
//        coverImageView.tag = cCellTag
        coverImageView.backgroundColor = UIColor.white
        coverImageView.addCorner(corner: 6)
        return coverImageView
    }()

    lazy var marksView: UIImageView = {
        let marksView = UIImageView(image: UIImage(named: "home_marks"))
//        marksView.contentMode = .scaleAspectFill
//        marksView.clipsToBounds = true
        marksView.isUserInteractionEnabled = false
        return marksView
    }()

    lazy var moreBtn: UIButton = {
        let moreBtn = UIButton(type: .custom)
        moreBtn.setImage(UIImage(named: "icon_video_point"), for: .normal)
        moreBtn.tag = 1
        moreBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        return moreBtn
    }()

    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textAlignment = .left
        titleLabel.textColor = .white
        titleLabel.numberOfLines = 1
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        return titleLabel
    }()

    lazy var statusBtn: UIButton = {
        let statusBtn = UIButton(type: .custom)
        statusBtn.backgroundColor = cShadowColor
        statusBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        statusBtn.setTitleColor(UIColor.white, for: .normal)
        statusBtn.tag = 2
        statusBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        return statusBtn
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(coverImageView)
        coverImageView.addSubview(marksView)
        contentView.addSubview(statusBtn)
        contentView.addSubview(moreBtn)
        contentView.addSubview(titleLabel)
    }

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

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

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

    func addData() {
        let coverImg : String = (("\(videoData?.coverImg?["coverImgPath"] ?? "")".count) > 0) ? "\(videoData?.coverImg?["coverImgPath"] ?? "")" : (videoData?.videoCoverSnapshotPath ?? "")
        coverImageView.setNetImage(url: coverImg)
        titleLabel.text = videoData?.title ?? ""
        statusBtn.isHidden = (videoData?.auditStatus == 5 && videoData?.transcodeStatus == 3 && (videoData?.transcodeStatus == 1 || videoData?.transcodeStatus == 3))
        if videoData?.transcodeStatus == 2 {
            statusBtn.setTitle("转码中", for: .normal)
        } else {
            statusBtn.setTitle("制作中", for: .normal)
        }
    }

    func addLayout() {
        let margin: CGFloat = 12
        let moreH: CGFloat = 30

        coverImageView.snp.remakeConstraints { make in
            make.size.equalToSuperview()
        }

        statusBtn.snp.makeConstraints { make in
            make.size.equalToSuperview()
        }

        marksView.snp.remakeConstraints { make in
            make.width.left.bottom.equalToSuperview()
        }

        moreBtn.snp.remakeConstraints { make in
            make.width.height.equalTo(moreH)
            make.right.bottom.equalToSuperview()
        }

        titleLabel.snp.remakeConstraints { make in
            make.centerY.equalTo(moreBtn)
            make.left.equalToSuperview().offset(margin)
            make.right.equalTo(moreBtn.snp.left).offset(cDefaultMargin / 2)
        }
    }

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