//
//  YHChildInfoContainerVC.swift
//  galaxy
//
//  Created by edy on 2024/2/5.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

//@objc protocol YHchildInfoVCProtocol {
//    
//    @objc optional func updateStepView()
//    @objc optional func saveInfo()
//}

class YHChildInfoContainerVC: YHBaseViewController, YHSpouseInfoVCProtocol {

    let familyRequest:YHFamilyRequestViewModel = YHFamilyRequestViewModel()

    var orderId:Int = 0

    var child:YHFamilyMember? {
        didSet {
            initalInfoVC.child = child
            certificateVC.familyMember = child
            basicInfoVC.child = child
        }
    }
    
    var currentIndex = 0 {
        didSet {
            currentIndex %= self.children.count
            stepView.currentIndex = currentIndex
            for (i, vc) in self.children.enumerated() {
                vc.view.isHidden = (currentIndex != i)
                if currentIndex == i {
                    
                }
            }
        }
    }
    
    var stepView:YHStepView = {
        let step = YHStepView()
        return step
    }()
    
    var bottomView: YHSaveAndSubmitView = {
        return YHSaveAndSubmitView.createView()
    }()
        
    let initalInfoVC =  YHChildPrimaryInfoVC()
    let uploadVC = UIViewController()
    let certificateVC = YHCertificateInfoController()
    let basicInfoVC = YHChildBasicInfoVC()

    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
    }
    
    func createUI() {
        
        guard child != nil else { return }
        
        initalInfoVC.delegate = self
        certificateVC.delegate = self
        basicInfoVC.delegate = self
        
        updateStepView()

        currentIndex = 0
        
        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() {
        currentIndex += 1;
        
    }
    
    func updateStepView() {
        
        guard let child = child else { return }
        
        for vc in self.children {
            vc.willMove(toParent: nil)
            vc.removeFromParent()
        }
        
        if child.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]
            stepView.block = {[weak self]  (index) in
                guard let self = self else { return }
                print("follow:%d", self.orderId)
            }
            
        } else {
            self.addChild(initalInfoVC)
            self.addChild(basicInfoVC)
            
            self.view.addSubview(basicInfoVC.view)
            self.view.addSubview(initalInfoVC.view)
            
            stepView.dataSource = ["初始信息".local, "基本信息".local]
            stepView.block = {[weak self]  (index) in
                guard let self = self else { return }
                print("follow:%d", self.orderId)

            }
        }
        self.view.bringSubviewToFront(stepView)
        self.view.bringSubviewToFront(bottomView)

    }
}

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