//
//  PersonalInformationFillViews.swift

//
//  Created by Leon on 2025/7/18.
//
import ActiveLabel
import Combine
class PersonalInformationFillView: CLContainer {
    var bottomView: UIView!
    var activeLabel: ActiveLabel!
    var bottomButton: StyleButton!

    var container: LTScrollContainer!
    var titleView: TitleView!
    var nicknameTitleField: TitleTextField!
    var sexView: GenderSelectView!
    var birthdaySelectView: CLSelectView!

    // Data

    @Published var nickname: String?
    @Published var birthdayStr: String?
    @Published var birthdayDate: Date?
    private var cancellables = Set<AnyCancellable>()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupEvents()
    }

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

    private func setupViews() {
        bottomView = {
            let v = UIView()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.bottom.equalToSuperview()
                // make.height.equalTo(196)
            }
            return v
        }()

        activeLabel = {
            let v = ActiveLabel()
            v.textColor = UIColor.c.ctsn
            v.font = CLSystemToken.font(token: .tbs)
            v.numberOfLines = 0
            bottomView.addSubview(v)
            v.snp.makeConstraints { make in
                make.bottom.equalToSuperview().offset(-16 - UIWindow.safeAreaBottom)
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()

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

        container = {
            let v = LTScrollContainer()
            v.stack.spacing = 24
            v.stack.alignment = .leading
            addSubview(v)
            v.snp.makeConstraints { make in
                // make.top.equalTo(navigationView.snp.bottom)
                make.top.equalToSuperview().offset(UIWindow.navBarTotalHeight)
                make.leading.trailing.equalToSuperview()
                make.bottom.equalTo(bottomView.snp.top)
            }
            return v
        }()

        titleView = {
            let v = TitleView()
            v.optionInnerBottomPadding = 0
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
            }
            return v
        }()

        nicknameTitleField = {
            let v = TitleTextField()
            v.maxLimit = 20
            v.minLimit = 2
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()

        sexView = {
            let v = GenderSelectView()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()

        birthdaySelectView = {
            let v = CLSelectView()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()

        #warning("translate")
        titleView.title = "login.personal.information".localized()

        nicknameTitleField.titleLabel.text = "nickname".localized()
        nicknameTitleField.placeholder = "Nickname"

        birthdaySelectView.titleLabel.text = "Birthday"
        birthdaySelectView.placeholder = "Birthday"

        bottomButton.setTitle("Register", for: .normal)

        let userAgreenment = "<Crushlevel User Agreement>"
        let privacyPolicy = "<Crushlevel Privacy Policy>"
        let full = "By clicking to register, you have read and agree to \(userAgreenment) and \(privacyPolicy)"
        let activeType1 = ActiveType.custom(pattern: userAgreenment)
        let activeType2 = ActiveType.custom(pattern: privacyPolicy)
        activeLabel.text = full
        activeLabel.enabledTypes = [activeType1, activeType2]
        activeLabel.customColor[activeType1] = .red // .cl.cpvn
        activeLabel.customColor[activeType2] = .red // .cl.cpvn
        activeLabel.handleCustomTap(for: activeType1) { str in
            dlog("tap\(str)")
        }
        activeLabel.handleCustomTap(for: activeType2) { str in
            dlog("tap\(str)")
        }
        activeLabel.customize { label in
            label.configureLinkAttribute = { type, attributes, _ in
                var attr = attributes
                if type == activeType1 || type == activeType2 {
                    attr[NSAttributedString.Key.foregroundColor] = UIColor.c.cpvn
                }
                return attr
            }
        }
    }

    private func setupEvents() {
        nicknameTitleField.textfield.textDidChanged = { [weak self] textField in
            self?.nickname = textField.text?.trimmed
        }
        
        $birthdayStr.sink {[weak self] str in
            self?.birthdaySelectView.contentStr = str
        }.store(in: &cancellables)
    }
}
