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

import UIKit
import BFCommonKit

public class PQAssetCategoryCell: UICollectionViewCell {
    public var representedAssetIdentifier: String!
    lazy public var videoImageView: UIImageView = {
        let videoImageView = UIImageView()
        videoImageView.addCorner(corner: 4)
        videoImageView.contentMode = .scaleAspectFill
        return videoImageView
    }()

    lazy public var categoryNameLab: UILabel = {
        let categoryNameLab = UILabel()
        categoryNameLab.font = UIFont.systemFont(ofSize: 14)
        categoryNameLab.textColor = PQBFConfig.shared.styleTitleColor
        return categoryNameLab
    }()

    lazy public var countLab: UILabel = {
        let countLab = UILabel()
        countLab.font = UIFont.systemFont(ofSize: 14)
        countLab.textColor = PQBFConfig.shared.styleTitleColor
        return countLab
    }()

    lazy public var seleImage: UIImageView = {
        let seleImage = UIImageView()
        seleImage.tintColor = PQBFConfig.shared.styleTitleColor
        seleImage.image = UIImage.moduleImage(named: "icon_uploadVideo_do", moduleName: "BFFramework",isAssets: false)?.withRenderingMode(.alwaysTemplate)
        seleImage.isHidden = true
        return seleImage
    }()

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

    override public init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(videoImageView)
        contentView.addSubview(categoryNameLab)
        contentView.addSubview(countLab)
        contentView.addSubview(seleImage)
    }

    override public var isSelected: Bool {
        didSet {
            seleImage.isHidden = !isSelected
        }
    }

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

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

    public func addData() {
        videoImageView.image = uploadData?.image
        categoryNameLab.text = "\(uploadData?.title ?? "")"
        countLab.text = "(\(uploadData?.categoryList.count ?? 0))"
    }

    public func addLayout() {
        let margin: CGFloat = 12
        let imageW: CGFloat = 66
        let countW: CGFloat = sizeWithText(text: countLab.text ?? "", font: UIFont.systemFont(ofSize: 14), size: CGSize(width: cScreenWidth - imageW - margin * 6, height: cDefaultMargin * 2)).width + cDefaultMargin
        let maxW: CGFloat = cScreenWidth - margin - imageW - margin - countW - margin - 13 - margin
        videoImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(margin)
            make.width.height.equalTo(imageW)
            make.centerY.equalToSuperview()
        }
        categoryNameLab.snp.makeConstraints { make in
            make.left.equalTo(videoImageView.snp.right).offset(margin)
            make.width.lessThanOrEqualTo(maxW)
            make.centerY.equalToSuperview()
        }
        countLab.snp.remakeConstraints { make in
            make.left.equalTo(categoryNameLab.snp.right)
            make.centerY.equalToSuperview()
            make.width.equalTo(countW)
        }
        seleImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-margin)
            make.width.equalTo(13)
            make.height.equalTo(10)
        }
    }
}
