//
//  LNJoinUsViewController.swift
//  Gami
//
//  Created by OneeChan on 2026/1/19.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToJoinUs() {
        let vc = LNJoinUsViewController()
        
        let fromWeb = navigationController?.viewControllers.last is LNWebViewController
        if fromWeb, var viewControllers = navigationController?.viewControllers {
            viewControllers.removeLast()
            viewControllers.append(vc)
            navigationController?.setViewControllers(viewControllers, animated: true)
        } else {
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}


enum LNJoinUsStep: Int, CaseIterable {
    case none
    case inputPhone
    case inputCaptcha
    case inputInfo
    case selectSkill
    case skillInfoInput
    
    var backStep: LNJoinUsStep? {
        switch self {
        case .none: nil
        case .inputPhone: nil
        case .inputCaptcha: .inputPhone
        case .inputInfo: nil
        case .selectSkill: nil
        case .skillInfoInput: .selectSkill
        }
    }
}

class LNJoinUsViewController: LNViewController {
    private var curStep: LNJoinUsStep = .inputPhone {
        didSet {
            view.endEditing(true)
            inputPhoneView.isHidden = true
            inputCaptchaView.isHidden = true
            inputInfoView.isHidden = true
            selectSkillView.isHidden = true
            inputSkillView.isHidden = true
            switch curStep {
            case .none: break
            case .inputPhone:
                inputPhoneView.isHidden = false
                headerView.update(1)
            case .inputCaptcha:
                inputCaptchaView.isHidden = false
            case .inputInfo:
                inputInfoView.isHidden = false
                headerView.update(2)
            case .selectSkill:
                selectSkillView.isHidden = false
                headerView.update(3)
            case .skillInfoInput:
                inputSkillView.isHidden = false
            }
        }
    }
    
    private let headerView = LNJoinUsHeaderView()
    private let inputPhoneView = LNJoinUsInputPhoneView()
    private let inputCaptchaView = LNJoinUsInputCaptchaView()
    private let inputInfoView = LNJoinUsInputInfoView()
    private let selectSkillView = LNJoinUsSelectSkillView()
    private let inputSkillView = LNJoinUsSkillFieldsEditView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        
        curStep = .none
        loadInfos()
    }
}

extension LNJoinUsViewController {
    private func loadInfos() {
        showLoading()
        LNGameMateManager.shared.getJoinGameMateInfo { [weak self] res in
            dismissLoading()
            guard let self else { return }
            guard let res else { return }
            
            if res.step3Complete || res.underReview {
                view.pushToJoinUsReview(false)
                return
            }
            
            if res.step2Complete {
                if let step3 = res.setp3, let code = step3.bizCategoryCode, !code.isEmpty,
                    let skill = LNGameMateManager.shared.gameCategory(for: code) {
                    curStep = .skillInfoInput
                    inputSkillView.update(skill, info: step3)
                } else {
                    curStep = .selectSkill
                }
            } else if res.step1Complete {
                curStep = .inputInfo
                if let info = res.setp2 {
                    inputInfoView.update(info)
                }
            } else {
                curStep = .inputPhone
                if let info = res.setp2 {
                    inputInfoView.update(info)
                }
            }
        }
    }
}

extension LNJoinUsViewController:
    LNJoinUsInputPhoneViewDelegate,
    LNJoinUsInputCaptchaViewDelegate,
    LNJoinUsInputInfoViewDelegate,
    LNJoinUsSelectSkillViewDelegate {
    func joinUsSelectSkillView(view: LNJoinUsSelectSkillView, didSelect skill: LNGameCategoryItemVO) {
        showLoading()
        LNGameMateManager.shared.getCreateSkillFields(id: skill.code) { [weak self] info in
            dismissLoading()
            
            guard let self else { return }
            guard let info else { return }
            curStep = .skillInfoInput
            
            inputSkillView.update(skill, info: info)
        }
    }
    
    func joinUsInputInfoViewDidFinish(view: LNJoinUsInputInfoView) {
        curStep = .selectSkill
    }
    
    func joinUsInputCaptchaViewDidFinish(view: LNJoinUsInputCaptchaView) {
        curStep = .inputInfo
    }
    
    func joinUsInputPhoneView(view: LNJoinUsInputPhoneView, didFinished code: String, phone: String) {
        curStep = .inputCaptcha
        inputCaptchaView.update(code, phone: phone)
    }
}

extension LNJoinUsViewController {
    private func setupViews() {
        navigationBarColor = .clear
        view.backgroundColor = .primary_1
        customBack = { [weak self] in
            guard let self else { return }
            if let back = curStep.backStep {
                curStep = back
            } else {
                navigationController?.popViewController(animated: true)
            }
        }
        
        title = .init(key: "B00031")
        
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(fakeNaviBgView)
        }
        
        let container = UIView()
        container.layer.cornerRadius = 20
        container.backgroundColor = .fill
        view.addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.bottom.equalToSuperview()
            make.top.equalTo(headerView.snp.bottom).offset(-56)
        }
        
        inputPhoneView.delegate = self
        container.addSubview(inputPhoneView)
        inputPhoneView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        inputCaptchaView.delegate = self
        container.addSubview(inputCaptchaView)
        inputCaptchaView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        inputInfoView.delegate = self
        container.addSubview(inputInfoView)
        inputInfoView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        selectSkillView.delegate = self
        container.addSubview(selectSkillView)
        selectSkillView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        container.addSubview(inputSkillView)
        inputSkillView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

#if DEBUG

import SwiftUI

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

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