//
//  LNEditGenderPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNEditGenderPanel: LNPopupView {
    private let maleButton = UIButton()
    private let femaleButton = UIButton()
    private let confirmButton = UIButton()
    
    var curGender: LNUserGender = .unknow {
        didSet {
            maleButton.backgroundColor = curGender == .male ? .fill_5 : .primary_1
            femaleButton.backgroundColor = curGender == .female ? .fill_5 : .primary_1
            if curGender == .unknow {
                confirmButton.isEnabled = false
                confirmButton.setBackgroundImage(nil, for: .normal)
            } else if confirmButton.backgroundImage(for: .normal) == nil {
                confirmButton.isEnabled = true
                confirmButton.setBackgroundImage(.primary_8, for: .normal)
            }
        }
    }
    
    var handler: ((LNUserGender) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNEditGenderPanel {
    private func setupViews() {
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00032")
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        maleButton.layer.cornerRadius = 23
        maleButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            curGender = .male
        }), for: .touchUpInside)
        container.addSubview(maleButton)
        maleButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(36)
            make.height.equalTo(46)
        }
        
        let maleView = UIView()
        maleView.isUserInteractionEnabled = false
        maleButton.addSubview(maleView)
        maleView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let maleIc = UIImageView()
        maleIc.image = .icGenderMale
        maleView.addSubview(maleIc)
        maleIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        let maleTitleLabel = UILabel()
        maleTitleLabel.text = LNUserGender.male.desc
        maleTitleLabel.font = .heading_h4
        maleTitleLabel.textColor = .text_5
        maleView.addSubview(maleTitleLabel)
        maleTitleLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(maleIc.snp.trailing).offset(6)
        }
        
        femaleButton.layer.cornerRadius = 23
        femaleButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            curGender = .female
        }), for: .touchUpInside)
        container.addSubview(femaleButton)
        femaleButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(maleButton.snp.bottom).offset(12)
            make.height.equalTo(46)
        }
        
        let femaleView = UIView()
        femaleView.isUserInteractionEnabled = false
        femaleButton.addSubview(femaleView)
        femaleView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let femaleIc = UIImageView()
        femaleIc.image = .icGenderFemale
        femaleView.addSubview(femaleIc)
        femaleIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        let femaleTitleLabel = UILabel()
        femaleTitleLabel.text = LNUserGender.female.desc
        femaleTitleLabel.font = .heading_h4
        femaleTitleLabel.textColor = .text_5
        femaleView.addSubview(femaleTitleLabel)
        femaleTitleLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(femaleIc.snp.trailing).offset(6)
        }
        
        confirmButton.setTitle(.init(key: "A00185"), for: .normal)
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.setBackgroundImage(.primary_8, for: .normal)
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.clipsToBounds = true
        confirmButton.backgroundColor = .fill_4
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            dismiss()
            handler?(curGender)
        }), for: .touchUpInside)
        container.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(femaleButton.snp.bottom).offset(16)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
}
