//
//  PQSelecteVideoItemCell.swift
//  PQSpeed
//
//  Created by SanW on 2020/8/1.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import Photos
import UIKit
import BFCommonKit

class PQSelecteVideoItemCell: UICollectionViewCell {
    var representedAssetIdentifier: String!
    lazy var videoImageView: UIImageView = {
        let videoImageView = UIImageView()
        videoImageView.contentMode = .scaleAspectFill
        videoImageView.clipsToBounds = true
        return videoImageView
    }()

    lazy var placeHoldeImageView: UIImageView = {
        let placeHoldeImageView = UIImageView()
        placeHoldeImageView.image = UIImage(named: "cut_place")
        placeHoldeImageView.clipsToBounds = true
        return placeHoldeImageView
    }()

    lazy var timeLab: UILabel = {
        let timeLab = UILabel()
        timeLab.font = UIFont.systemFont(ofSize: 11)
        timeLab.textColor = UIColor.white
        timeLab.text = "00:00"
        return timeLab
    }()
 
    lazy var seleImage: UIImageView = {
        let seleImage = UIImageView(image:UIImage.moduleImage(named: "icon_upload_do", moduleName: "BFFramework",isAssets: false))
        return seleImage
    }()

    lazy var bgView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = UIColor.init(red: 61.0 / 255.0 , green: 193.0 / 255.0 , blue: 193.0 / 255.0, alpha: 0.6)
        return bgView
    }()

    @objc class func selecteVideoItemCell(collectionView: UICollectionView, indexPath: IndexPath) -> PQSelecteVideoItemCell {
        let cell: PQSelecteVideoItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PQSelecteVideoItemCell", for: indexPath) as! PQSelecteVideoItemCell
        return cell
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(videoImageView)
        videoImageView.addSubview(timeLab)
        videoImageView.addSubview(placeHoldeImageView)
        contentView.addSubview(bgView)
        bgView.addSubview(seleImage)
        bgView.isHidden = !isSelected
    }

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

    var uploadData: PQUploadModel? {
        didSet {
            addData()
            addLayout()
        }
    }

    var thubImage: UIImage? {
        didSet {
            addData()
            videoImageView.image = thubImage
            addLayout()
        }
    }

    func addData() {
        videoImageView.contentMode = uploadData?.contentMode ?? .scaleAspectFill
        if uploadData?.contentMode == .scaleAspectFit {
            addCorner(corner: 6)
        } else {
            addCorner(corner: 0)
        }
        if uploadData?.image != nil {
            videoImageView.image = uploadData?.image
        } else if uploadData?.imageUrl != nil, (uploadData?.imageUrl.count ?? 0) > 0 {
            netImage(url: uploadData?.imageUrl ?? "", mainView: videoImageView)
//        }else if(uploadData?.asset != nil){
//            PQPHAssetVideoParaseUtil.requestAssetImage(asset: (uploadData?.asset)!, itemSize: CGSize.init(width: 0, height: 0)) { [weak self](image, info) in
//                self?.videoImageView.image = image
//            }
        } else {
            videoImageView.image = nil
        }
        timeLab.text = (uploadData?.asset?.duration ?? 0).formatDurationToHMS()
        timeLab.isHidden = (uploadData?.asset?.duration ?? 0) <= 0.0
        bgView.isHidden = !(uploadData?.isSelected ?? false)
        videoImageView.backgroundColor = (((uploadData?.videoWidth ?? 0) < (uploadData?.videoHeight ?? 0)) || (uploadData?.image == nil && (uploadData?.imageUrl == nil || (uploadData?.imageUrl.count ?? 0) <= 0))) ? UIColor.hexColor(hexadecimal: "#212223") : UIColor.black
        placeHoldeImageView.isHidden = !(uploadData?.image == nil && (uploadData?.imageUrl == nil || (uploadData?.imageUrl.count ?? 0) <= 0))
    }

    func addLayout() {
        placeHoldeImageView.snp.remakeConstraints { make in
            make.width.equalTo(28)
            make.height.equalTo(24)
            make.center.equalToSuperview()
        }
        videoImageView.snp.remakeConstraints { make in
            make.top.bottom.left.right.equalToSuperview()
        }
        timeLab.snp.makeConstraints { make in
            make.bottom.equalTo(videoImageView).offset(-cDefaultMargin / 2)
            make.right.equalTo(videoImageView).offset(-cDefaultMargin / 2)
        }
        bgView.snp.makeConstraints { make in
            make.size.equalToSuperview()
        }
        seleImage.snp.makeConstraints { make in
            make.width.height.equalTo(23)
            make.right.equalToSuperview().offset(-cDefaultMargin)
            make.top.equalToSuperview().offset(cDefaultMargin)
        }
    }
}
