//
//  GenderSelectView.swift

//
//  Created by Leon on 2025/7/18.
//
import Combine
import UIKit

class GenderSelectView: UIView {
    var titleLabel: UILabel!
    var stackV: UIStackView!
    var stackH: UIStackView!
    var supportLabel: UILabel!

    var maleBtn: EPChipCenterIconButton!
    var femaleBtn: EPChipCenterIconButton!
    var unknowBtn: EPChipCenterIconButton!

    @Published var sex: Sex?
    private var cancellables = Set<AnyCancellable>()

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

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    private func setupViews() {
        titleLabel = UILabel()
        titleLabel.font = CLSystemToken.typography(token: .tlm).font!
        titleLabel.textColor = .c.ctpn
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.leading.equalToSuperview()
        }

        stackV = {
            let v = UIStackView()
            v.axis = .vertical
            v.spacing = 12
            v.alignment = .leading
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.top.equalTo(titleLabel.snp.bottom).offset(12)
                make.bottom.equalToSuperview()
            }
            return v
        }()

        stackH = {
            let v = UIStackView()
            v.spacing = 12
            v.distribution = .fillEqually
            stackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.height.equalTo(48)
                make.leading.trailing.equalToSuperview()
            }
            return v
        }()

        maleBtn = {
            let v = EPChipCenterIconButton()
            v.addTarget(self, action: #selector(tapBtn(sender:)), for: .touchUpInside)
            v.iconCode = IconCode.male
            v.isSelected = false
            stackH.addArrangedSubview(v)
            return v
        }()

        femaleBtn = {
            let v = EPChipCenterIconButton()
            v.addTarget(self, action: #selector(tapBtn(sender:)), for: .touchUpInside)
            v.iconCode = IconCode.female
            stackH.addArrangedSubview(v)
            return v
        }()
        
        unknowBtn = {
            let v = EPChipCenterIconButton()
            v.addTarget(self, action: #selector(tapBtn(sender:)), for: .touchUpInside)
            v.iconCode = IconCode.nonconforming
            v.isSelected = false
            stackH.addArrangedSubview(v)
            return v
        }()

        supportLabel = {
            let v = UILabel()
            v.font = CLSystemToken.font(token: .tbs)
            v.textColor = .c.ctsn
            v.numberOfLines = 0
            v.isHidden = true
            stackV.addArrangedSubview(v)
            return v
        }()
        
        titleLabel.text = "Sex"
    }

    private func setupEvents() {
        $sex
            .sink { [weak self] newSex in
                self?.resetSelectState()
                // 在此响应 sex 的变化
                dlog("性别变化为: \(String(describing: newSex))")
                switch newSex {
                case .female:
                    self?.femaleBtn.isSelected = true
                case .male:
                    self?.maleBtn.isSelected = true
                case .noncomfirming:
                    self?.unknowBtn.isSelected = true
                default: break
                }

                // 你可以在这里做任何后续处理
            }
            .store(in: &cancellables)
    }

    private func resetSelectState() {
        femaleBtn.isSelected = false
        maleBtn.isSelected = false
        unknowBtn.isSelected = false
    }

    @objc private func tapBtn(sender: UIButton) {
        if sender == maleBtn {
            sex = .male
        } else if sender == femaleBtn {
            sex = .female
        } else if sender == unknowBtn {
            sex = .noncomfirming
        } else {
            sex = nil
        }
        // sender.isSelected = true
    }
}
