//
//  YHSpouseInfoVC.swift
//  galaxy
//
//  Created by edy on 2024/1/31.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

@objc protocol YHSpouseInfoVCProtocol {
    
    @objc optional func updateStepView()
    @objc optional func saveInfo()
    
}

class YHSpouseInfoContainerVC: YHBaseViewController, YHSpouseInfoVCProtocol {

    let familyRequest:YHFamilyRequestViewModel = YHFamilyRequestViewModel()

    var orderId:Int = 0

    var spouse:YHFamilyMember? {
        didSet {
            if let spouse = spouse {
                initalInfoVC.spouse = spouse
                certificateVC.familyMember = spouse
                basicInfoVC.spouse = spouse
            }
        }
    }
    
    var currentIndex = 0 {
        didSet {
            stepView.currentIndex = currentIndex
            for (i, vc) in self.children.enumerated() {
                vc.view.isHidden = (currentIndex != i)
            }
        }
    }
    
    var stepView:YHStepView = {
        let stepV = YHStepView()
        return stepV
    }()
    
    var bottomView: YHSaveAndSubmitView = {
        return YHSaveAndSubmitView.createView()
    }()
        
    let initalInfoVC =  YHSpousePrimaryInfoVC()
    let uploadVC = UIViewController()
    let certificateVC = YHCertificateInfoController()
    let basicInfoVC = YHSpouseBasicInfoVC()

    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
    }
    
    func createUI() {
        
        guard let spouse = spouse else { return }
        
        initalInfoVC.delegate = self
        certificateVC.delegate = self
        basicInfoVC.delegate = self
        
        updateStepView()
        
        view.addSubview(stepView)
        view.addSubview(bottomView)
        
        bottomView.submitBlock = {
            [weak self] in
            guard let self = self else { return }
            self.didClickSubmitBtn()
        }
        
        bottomView.saveBlock = {
            [weak self] in
            guard let self = self else { return }
            self.saveInfo()
        }
        
        stepView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.left.right.equalTo(view)
            make.height.equalTo(YHStepView.height)
        }
        
        bottomView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-k_Height_safeAreaInsetsBottom())
            make.height.equalTo(YHSaveAndSubmitView.height)
        }
    }
    
    func didClickSubmitBtn() {
        
    }
    
    func updateStepView() {
        
        guard let spouse = spouse else { return }
        
        for vc in self.children {
            vc.willMove(toParent: nil)
            vc.removeFromParent()
        }
        
        if spouse.isFollow() {
            
            self.addChild(initalInfoVC)
            self.addChild(uploadVC)
            self.addChild(certificateVC)
            self.addChild(basicInfoVC)
            
            self.view.addSubview(basicInfoVC.view)
            self.view.addSubview(certificateVC.view)
            self.view.addSubview(uploadVC.view)
            self.view.addSubview(initalInfoVC.view)
            
            stepView.dataSource = ["初始信息".local, "证件上传".local, "证件信息".local, "基本信息".local]
            
        } else {
            self.addChild(initalInfoVC)
            self.addChild(basicInfoVC)
            
            self.view.addSubview(basicInfoVC.view)
            self.view.addSubview(initalInfoVC.view)
            
            stepView.dataSource = ["初始信息".local, "基本信息".local]
            
        }
        
        print("step:\(spouse.step)")
        currentIndex = spouse.step

        stepView.block = {
            [weak self] index in
            guard let self = self else { return }
            print("index:\(index)")
            currentIndex = index
        }
        
        self.view.bringSubviewToFront(stepView)
        self.view.bringSubviewToFront(bottomView)

    }
}

extension YHSpouseInfoContainerVC {
    func saveInfo() {
        
        guard  let spouse = spouse else { return }
        guard let info = spouse.toDictionary() else { return }
        
        let dict:[String: Any] = ["orderId":spouse.orderId,
                                  "relation":spouse.relationType.rawValue,
                                  "step":spouse.step,
                                  "next":false,
                                  "info":info]
        
        self.familyRequest.addOrSaveFamilyMember(params:dict) { [weak self] success, error in
            guard let self = self else { return }
            if success {
                
            }
        }
    }
}
