//
//  RolePhotoResultSettingViews.swift

//
//  Created by Leon on 2025/9/19.
//

protocol RolePhotoGenerateSetGridCellDelegate: AnyObject {
    func generateSetCellTapPrice(obj: AIUserImageQuery?)
}

class RolePhotoGenerateSetGridCell: RoleCreateAppearanceResultCell {
    weak var delegate: RolePhotoGenerateSetGridCellDelegate?
    
    var regenerateType: RolePhotoGenerateType = .album{
        didSet{
            if regenerateType == .figure{
                lockWayView.isHidden = true
            }
        }
    }

    var lockWayView: UnlockWaySetFlagView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupGridViews()
    }

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

    private func setupGridViews() {
        lockWayView = {
            let v = UnlockWaySetFlagView()
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(8)
                make.bottom.equalToSuperview().offset(-8)
            }
            return v
        }()

        lockWayView.topButton.addTarget(self, action: #selector(tapUnlockButton), for: .touchUpInside)
    }

    override func config(data: AIUserImageQuery) {
        super.config(data: data)

        lockWayView.config(data.unlockPrice ?? 0)
    }

    override func setupSelected(_ selected: Bool) {
        super.setupSelected(selected)
        
        selectMark.image = UIImage(named: "checkmark_tick")
        
//        if regenerateType == .album{
//            selectMark.isHidden = false
//            selectMark.image = selected ? UIImage(named: "checkmark_tick") : UIImage(named: "checkmark_tick_light")
//        }else {
//            selectMark.image = UIImage(named: "checkmark_tick")
//        }
    }

    @objc private func tapUnlockButton() {
        delegate?.generateSetCellTapPrice(obj: data)
    }
}

class UnlockWaySetFlagView: UIView {
    var stackH: UIStackView!

    var iconAndLabel: CLIconLabel!
    var settingIcon: UIImageView!
    var topButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

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

    private func setupViews() {
        backgroundColor = .c.csedn

        snp.makeConstraints { make in
            make.height.equalTo(32)
        }
        stackH = {
            let v = UIStackView()
            v.spacing = 8
            v.alignment = .center
            addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalToSuperview().offset(-8)
                make.leading.equalToSuperview().offset(12)
            }
            return v
        }()

        iconAndLabel = {
            let v = CLIconLabel()
            v.spacing = 4
            v.iconImageView.image = UIImage.icon16Diamond
            v.contentLabel.font = .t.tls
            stackH.addArrangedSubview(v)
            return v
        }()

        settingIcon = {
            let v = UIImageView()
            v.image = MWIconFont.image(fromIcon: .iconSetting, size: CGSize(width: 12, height: 12), color: .white)
            stackH.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 12, height: 12))
            }
            return v
        }()

        topButton = {
            let v = UIButton()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        iconAndLabel.iconImageView.isHidden = true
        iconAndLabel.contentLabel.text = "Free"
    }

    func config(_ price: Int) {
        if price > 0 {
            iconAndLabel.iconImageView.isHidden = false
            let coin = Coin(cents: price)
            iconAndLabel.contentLabel.text = coin.thousandthsFormatted
        } else {
            iconAndLabel.iconImageView.isHidden = true
            iconAndLabel.contentLabel.text = "Free"
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        cornerRadius = bounds.size.height * 0.5
    }
}
