//
//  LNGenderSetupViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/2.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToGenderSetup() {
        let vc = LNGenderSetupViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNGenderSetupViewController: LNViewController {
    private let maleIc = UIImageView()
    private let maleBg = UIImageView()
    private let maleLabel = UILabel()
    
    private let femaleIc = UIImageView()
    private let femaleBg = UIImageView()
    private let femaleLabel = UILabel()
    
    private let nextButton = UIButton()
    
    private var curGender: LNUserGender = .unknow {
        didSet {
            switch curGender {
            case .male:
                maleIc.image = .icLoginGenderMaleSelected
                maleBg.image = .icLoginGenderSelected
                maleLabel.textColor = .text_1
                
                femaleIc.image = .icLoginGenderFemale
                femaleBg.image = nil
                femaleLabel.textColor = .text_5
            case .female:
                maleIc.image = .icLoginGenderMale
                maleBg.image = nil
                maleLabel.textColor = .text_5
                
                femaleIc.image = .icLoginGenderFemaleSelected
                femaleBg.image = .icLoginGenderSelected
                femaleLabel.textColor = .text_1
            case .unknow:
                break
            }
            nextButton.isEnabled = curGender != .unknow
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        enableDragBack = false
        
        setupViews()
    }
}

extension LNGenderSetupViewController {
    private func setupViews() {
        navigationBarColor = .clear
        
        let bg = UIImageView()
        bg.image = .icLoginProfileBg
        view.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(fakeNaviBgView)
        }
        
        setupNavigationBar()
        
        let titles = buildTitles()
        view.addSubview(titles)
        titles.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(22)
            make.top.equalToSuperview().offset(22)
        }
        
        let gender = buildGenderView()
        view.addSubview(gender)
        gender.snp.makeConstraints { make in
            make.centerY.equalToSuperview().multipliedBy(0.8)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        nextButton.setBackgroundImage(.primary_8, for: .normal)
        nextButton.setTitle(.init(key: "A00101"), for: .normal)
        nextButton.setTitleColor(.text_1, for: .normal)
        nextButton.titleLabel?.font = .heading_h3
        nextButton.layer.cornerRadius = 23.5
        nextButton.clipsToBounds = true
        nextButton.isEnabled = false
        nextButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            let config = LNProfileUpdateConfig()
            config.gender = self.curGender
            self.view.pushToBaseInfoSetup(config)
        }), for: .touchUpInside)
        view.addSubview(nextButton)
        nextButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-4)
            make.height.equalTo(47)
        }
    }
    
    private func buildTitles() -> UIView {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 6
        
        let mainLabel = UILabel()
        mainLabel.text = .init(key: "A00106")
        mainLabel.font = .init(name: UIFont.boldFontName, size: 40)
        mainLabel.textColor = .text_5
        mainLabel.numberOfLines = 0
        stackView.addArrangedSubview(mainLabel)
        
        let subTitleLabel = UILabel()
        subTitleLabel.text = .init(key: "A00107")
        subTitleLabel.font = .heading_h1
        subTitleLabel.textColor = .text_5
        subTitleLabel.numberOfLines = 0
        stackView.addArrangedSubview(subTitleLabel)
        
        let descLabel = UILabel()
        descLabel.font = .body_m
        descLabel.textColor = .text_4
        descLabel.text = .init(key: "A00108")
        descLabel.numberOfLines = 0
        stackView.addArrangedSubview(descLabel)
        
        return stackView
    }
    
    private func buildGenderView() -> UIView {
        let container = UIView()
        
        let maleView = UIView()
        maleView.onTap { [weak self] in
            guard let self else { return }
            self.curGender = .male
        }
        container.addSubview(maleView)
        maleView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(0.5)
        }
        
        let maleImage: UIImage = .icLoginGenderSelected
        maleBg.backgroundColor = .fill_2
        maleBg.layer.cornerRadius = 30
        maleBg.isUserInteractionEnabled = false
        maleView.addSubview(maleBg)
        maleBg.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.size.equalTo(maleImage.size)
        }
        
        maleIc.image = .icLoginGenderMale
        maleIc.isUserInteractionEnabled = false
        maleView.addSubview(maleIc)
        maleIc.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(maleBg.snp.bottom).offset(-68)
        }
        
        maleLabel.text = .init(key: "A00109")
        maleLabel.font = .heading_h1_5
        maleLabel.textColor = .text_5
        maleView.addSubview(maleLabel)
        maleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-16)
        }
        
        let femaleView = UIView()
        femaleView.onTap { [weak self] in
            guard let self else { return }
            self.curGender = .female
        }
        container.addSubview(femaleView)
        femaleView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(1.5)
        }
        
        let femaleImage: UIImage = .icLoginGenderSelected
        femaleBg.backgroundColor = .fill_2
        femaleBg.layer.cornerRadius = 30
        femaleBg.isUserInteractionEnabled = false
        femaleView.addSubview(femaleBg)
        femaleBg.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.size.equalTo(femaleImage.size)
        }
        
        femaleIc.image = .icLoginGenderFemale
        femaleIc.isUserInteractionEnabled = false
        femaleView.addSubview(femaleIc)
        femaleIc.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(femaleBg.snp.bottom).offset(-68)
        }
        
        femaleLabel.text = .init(key: "A00110")
        femaleLabel.font = .heading_h1_5
        femaleLabel.textColor = .text_5
        femaleView.addSubview(femaleLabel)
        femaleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-16)
        }
        
        return container
    }
    
    private func setupNavigationBar() {
        customBack = { [weak self] in
            guard let self else { return }
            navigationController?.popViewController(animated: true)
            LNAccountManager.shared.logout()
        }
        
        let skipButton = UIButton()
        skipButton.setTitle(.init(key: "A00286"), for: .normal)
        skipButton.titleLabel?.font = .heading_h2
        skipButton.setTitleColor(.text_5, for: .normal)
        skipButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            
            let config = LNProfileUpdateConfig()
            view.pushToBaseInfoSetup(config)
        }), for: .touchUpInside)
        setRightButton(skipButton)
    }
}

#if DEBUG

import SwiftUI

struct LNGenderSetupViewControllerPreview: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        LNGenderSetupViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        
    }
}

#Preview(body: {
    LNGenderSetupViewControllerPreview()
})
#endif
