//
//  TSPTPUploadCell.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/25.
//

class TSPTPUploadCell : TSBaseCollectionCell{
    
    var upLoadImage:UIImage? = nil {
        didSet{
            if let image = upLoadImage {
                upLoadView.isHidden = true
                uploadImageView.isHidden = false
                deleteBtn.isHidden = false
                uploadImageView.image = image
            }else {
                upLoadView.isHidden = false
                uploadImageView.isHidden = true
                deleteBtn.isHidden = true
                uploadImageView.image = nil
            }
        }
    }
    
    lazy var upLoadView: UIView = {
        let bgView = UIView()
        
        let addImageView = UIImageView.createImageView(imageName: "add")
        bgView.addSubview(addImageView)
        addImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        let textLabel = UILabel.createLabel(text: "Upload Image".localized,font: .font(size: 16),textColor: .white)
        bgView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.top.equalTo(addImageView.snp.bottom).offset(16)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        return bgView
    }()
    
    lazy var uploadImageView: UIImageView = {
        let uploadImageView = UIImageView()
        uploadImageView.contentMode = .scaleAspectFit
        uploadImageView.cornerRadius = 12
        upLoadView.isHidden = true
        return uploadImageView
    }()
    
    
    lazy var bgView: UIView = {
        
        let bgView = UIView()
        bgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action:#selector(clickBgView)))
        let bgImageView = UIImageView.createImageView(imageName: "ptp_upload_bg")
        bgImageView.contentMode = .scaleToFill
        bgView.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.leading.bottom.trailing.top.equalTo(0)
        }

        bgView.addSubview(upLoadView)
        upLoadView.snp.makeConstraints { make in
            make.top.equalTo(88)
            make.centerX.equalToSuperview()
        }
        
        bgView.addSubview(uploadImageView)
        uploadImageView.snp.makeConstraints { make in
            make.top.leading.equalTo(8)
            make.bottom.trailing.equalTo(-8)
        }
        
        return bgView
    }()
    
    
    lazy var deleteBtn: UIButton = {
        let deleteBtn = UIButton.createButton(backgroundImage: UIImage(named: "delete_redRound")) { [weak self]  in
            guard let self = self else { return }
            upLoadImage = nil
            actionHandler(any: "delete")
        }
        deleteBtn.isHidden = true
        return deleteBtn
    }()
    
    override func creatUI() {
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.leading.top.equalTo(16)
            make.bottom.trailing.equalTo(-16)
        }
        
        contentView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.trailing.equalTo(-4)
            make.width.height.equalTo(32)
        }
    }
    

    @objc func clickBgView() {
        actionHandler(any: "add")
    }
    
    
    override func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
        super.renderView(with: object, component: component, attributes: attributes)
        if let itemModel = object as? TSGenmojiCoLItemModel{
            if let model = itemModel.ptpStyleModels.first {
                upLoadImage = model.image
            }
        }
    }
}
