//
//  CoinsRechargeBottomView.swift

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

import ActiveLabel


class CoinsRechargeBottomView: UIView {
    var operateButton: StyleButton!
    var activeLabel: ActiveLabel!
    var checkButton: EPRadioButton!

    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.cbd
        activeLabel = {
            let v = ActiveLabel()
            v.textColor = .text
            v.font = .t.tbs
            v.numberOfLines = 0
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(48)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                make.bottom.equalToSuperview().offset(-UIWindow.safeAreaBottom - 16)
            }
            return v
        }()

        checkButton = {
            let button = EPRadioButton()
            //button.setupStyle(.rectangle)
            button.setupRadioStyle(.checkEmpty)
            button.addTarget(self, action: #selector(tapCheckButton), for: .touchUpInside)
            addSubview(button)
            button.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.top.equalTo(activeLabel).offset(2)
                make.size.equalTo(CGSize(width: 16, height: 16))
            }
            button.isSelected = true
            return button
        }()

        operateButton = {
            let v = StyleButton()
            v.primary(size: .large)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                make.top.equalToSuperview().offset(16)
                make.bottom.equalTo(activeLabel.snp.top).offset(-16)
            }
            return v
        }()

        operateButton.setTitle("Recharge", for: .normal)

        setupAgreenment()
    }

    // MARK: - Public

    // MARK: - Helper

    func setupAgreenment() {
        let userAgreenment = "CrushLevel Top-Up Agreement"
        let full = "I have read and agree to the \(userAgreenment)"
        let activeType1 = ActiveType.custom(pattern: userAgreenment)

        activeLabel.text = full
        activeLabel.enabledTypes = [activeType1]
//        activeLabel.customColor[activeType1] = .red
        activeLabel.handleCustomTap(for: activeType1) { str in
            dlog("tap\(str)")
            AppRouter.goRechargeH5()
        }

        activeLabel.customize { label in
            label.configureLinkAttribute = { type, attributes, _ in
                var attr = attributes
                if type == activeType1 {
                    attr[NSAttributedString.Key.foregroundColor] = UIColor.c.cpvn
                    attr[NSAttributedString.Key.font] = UIFont.t.tbsm
                }
                return attr
            }
        }
    }

    // MARK: - Action

    @objc func tapCheckButton() {
        checkButton.isSelected = !checkButton.isSelected
        
        operateButton.isEnabled = checkButton.isSelected
    }
}
