//
//  RoleCreateAppearanceResultCell.swift

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

/// 形象生成图Cell （包括生成中、队列中、生成成功状态）
class RoleCreateAppearanceResultCell: UICollectionViewCell {
    var block: UIView!
    var iv: CLImageView!

    var selectMark: UIImageView!
    var viewFullButon: EPIconTertiaryDarkButton!
    
    var photoStateView: PhotoGenerateStateOverlay!

    var tapFullBrowseAction: ((_ data: AIUserImageQuery, _ image: UIImage?) -> Void)?

    var data: AIUserImageQuery?

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

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        bringSubviewToFront(photoStateView)
    }

    private func setupBaseViews() {
        block = {
            let v = UIView()
            v.backgroundColor = .c.csen
            v.layer.cornerRadius = 12
            v.clipsToBounds = true
            v.layer.borderWidth = 1
            v.layer.borderColor = UIColor.c.cpn.cgColor
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        iv = {
            let v = CLImageView(frame: .zero)
            v.layer.cornerRadius = 12
            v.clipsToBounds = true
            v.contentMode = .scaleAspectFill
            v.backgroundColor = .c.csbn
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        selectMark = {
            let v = UIImageView()
            v.image = UIImage(named: "checkmark_tick")
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview().offset(-8)
                make.top.equalToSuperview().offset(16)
            }
            return v
        }()

        viewFullButon = {
            let v = EPIconTertiaryDarkButton(radius: .round, iconSize: .small, iconCode: .iconFullimage)
            v.addTarget(self, action: #selector(tapViewFullButton), for: .touchUpInside)
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(v.bgImageSize())
                make.trailing.equalToSuperview().offset(-12)
                make.bottom.equalToSuperview().offset(-12)
            }
            return v
        }()
        
        photoStateView = {
            let v = PhotoGenerateStateOverlay()
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            v.isHidden = true
            return v
        }()

        layoutIfNeeded()
        setupSelected(false)
    }

    public func config(data: AIUserImageQuery) {
        self.data = data
        let state = data.status

        viewFullButon.isHidden = true
        iv.loadImage(nil)
        
        switch state {
        case .pending:
            photoStateView.showLoading()
        case .completed:
            photoStateView.hideLoading()
            photoStateView.isHidden = true
            if let url = data.imageUrl {
                iv.loadImage(url)
                viewFullButon.isHidden = false
            }
        // 根据选择情况
        case .failed, .nsfw:
            setupSelected(false)
            photoStateView.showFailed()
        }
    }

    func setupSelected(_ selected: Bool) {
        if selected {
            selectMark.isHidden = false
            block.layer.borderWidth = 2
        } else {
            selectMark.isHidden = true
            block.layer.borderWidth = 0
        }
    }

    // MARK: - Action

    @objc private func tapViewFullButton() {
        tapFullBrowseAction?(data!, iv.image)
    }

    // MARK: private

    override func prepareForReuse() {
        super.prepareForReuse()
        iv.loadImage(nil)
    }
    
//    class RoleLoadingContainer: UIView{
//        override func layoutSubviews() {
//            super.layoutSubviews()
//            addDashedBorder(to: self, cornerRadius: 16)
//        }
//    }
}
