//
//  RolePhotoBrowseViews.swift

//
//  Created by Leon on 2025/7/26.
//

import UIKit

class RolePhotoUnlockEntryView: UIView {
    var iconImageView: UIImageView!
    var unlockWayLabel: UILabel!
    var unlockWayButton: UIButton!

    var editUnlockWayAction: (() -> Void)?

    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
        layer.cornerRadius = 16
        layer.masksToBounds = true

        snp.makeConstraints { make in
            make.height.equalTo(56)
        }

        let titleTipLabel = {
            let v = UILabel()
            v.text = "Unlock Method"
            v.font = .t.tts
            v.textColor = .text
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.centerY.equalToSuperview()
            }
            return v
        }()
        titleTipLabel.isHidden = false

        iconImageView = {
            let v = UIImageView()
            v.image = MWIconFont.image(fromIcon: .iconOrderRemark, size: CGSize(width: 16, height: 16), color: .white)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview().offset(-16)
                make.centerY.equalToSuperview()
                make.width.height.equalTo(16)
            }
            return v
        }()

        unlockWayLabel = {
            let v = UILabel()
            v.text = "Free"
            v.font = .t.tlm
            v.textColor = .text
            addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalTo(iconImageView.snp.leading).offset(-12)
                make.centerY.equalToSuperview()
            }
            return v
        }()

        unlockWayButton = {
            let v = UIButton()
            v.addTarget(self, action: #selector(unlockWayButtonAction), for: .touchUpInside)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
//        #warning("test")
//        setupCoinUnlock(coin: 20)
    }

    @objc func unlockWayButtonAction() {
        editUnlockWayAction?()
    }

    public func setupCoinUnlock(coin: Int) {
        if coin == 0{
            unlockWayLabel.attributedText = nil
            unlockWayLabel.text = "Free"
            return
        }
        
        let attributedString = NSMutableAttributedString()

        let text = " \(String.thousandString(float:Double(coin)/100.0))"
        // 添加图标
        if let iconImage = UIImage(named: "icon_16_diamond") { // 替换为你的图标名称
            let attachment = NSTextAttachment()
            attachment.image = iconImage

            let iconSize = CGSize(width: 16, height: 16) // 调整为你需要的大小
            attachment.bounds = CGRect(origin: .init(x: 0, y: -2), size: iconSize)

            // 将图标添加到 AttributedString
            let iconAttributedString = NSAttributedString(attachment: attachment)
            attributedString.append(iconAttributedString)
        }

        // 添加文字并设置样式
        let textAttributedString = text.withAttributes([
            .font(.t.tlm), // 设置字体
            .textColor(.white)
        ])
        attributedString.append(textAttributedString)
        
        unlockWayLabel.text = nil
        unlockWayLabel.attributedText = attributedString
    }
}

class RolePhotoSetDefaultEntryView: UIView {
    var setButton: StyleButton!
    var titleLabel: UILabel!
    var descriptionLabel: LineSpaceLabel!
    var leftLabelsStackV: UIStackView!
    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
        layer.cornerRadius = 16
        layer.masksToBounds = true

        setButton = {
            let v = StyleButton()
            v.primary(size: .small)
            v.setTitle("Set", for: .normal)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalToSuperview().offset(-16)
            }
            return v
        }()

        leftLabelsStackV = {
            let v = UIStackView()
            v.axis = .vertical
            v.spacing = 4
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.top.equalToSuperview().offset(16)
                make.bottom.equalToSuperview().offset(-16)
                make.trailing.equalTo(setButton.snp.leading).offset(-12)
            }

            return v
        }()

        titleLabel = {
            let v = UILabel()
            v.text = "Set as Default"
            v.font = .t.tts
            v.textColor = .text
            v.numberOfLines = 0
            leftLabelsStackV.addArrangedSubview(v)
            return v
        }()

        descriptionLabel = {
            let v = LineSpaceLabel()
            v.text = "Only apply to the background of your chat with the character"
            let typo = CLSystemToken.typography(token: .tbs)
            v.config(typo)
            v.textColor = .text
            leftLabelsStackV.addArrangedSubview(v)
            return v
        }()
        
        titleLabel.isHidden = false
        descriptionLabel.isHidden = false
    }
    
    // MARK: Public
    func setupIsDefault(_ isDefault: Bool){
        if isDefault{
            setButton.isHidden = true
            descriptionLabel.isHidden = false
            leftLabelsStackV.snp.remakeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.top.equalToSuperview().offset(16)
                make.bottom.equalToSuperview().offset(-16)
                make.trailing.equalToSuperview().offset(-16)
            }
        }else{
            setButton.isHidden = false
            descriptionLabel.isHidden = true
            leftLabelsStackV.snp.remakeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.top.equalToSuperview().offset(16)
                make.bottom.equalToSuperview().offset(-16)
                make.trailing.equalTo(setButton.snp.leading).offset(-12)
            }
        }
    }
    
    func setupChatBackgroundSetMode(){
        titleLabel.text = "Set background"
        descriptionLabel.text = "Only apply to the background of your chat with the character"
    }
}
