//
//  YHMainApplicantInformationViewModel.swift
//  galaxy
//
//  Created by EDY on 2024/1/26.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

enum YHSaveType: Int {
    case save = 0
    case submit = 1
}


class YHMainApplicantInformationViewModel: YHBaseViewModel {
    var mainModel: YHMainInformationModel = YHMainInformationModel()
    
    override init() {
        super.init()
    }
    
    func getBaseDataSource() -> [YHSectionItemModel] {
        let item = YHItemModel(id: .id1, isNeed: true, title: "国家/地区", isUserKeyBoard: false, prompts: "请选择", message: mainModel.nationality, type: .country)
        let section = YHSectionItemModel(title: "国籍", models: [item])
        
        let item4 = YHItemModel(id: .id2, isNeed: true, title: "国家/地区", isUserKeyBoard: false, prompts: "请选择", message: mainModel.address?.country, type: .country)
        var string = ""
        if let array = mainModel.address?.area {
            for item in array {
                string = string + item
            }
        }
        let item1 = YHItemModel(id: .id3, isNeed: true, title: "省市地区", isUserKeyBoard: false, prompts: "请选择", message: string, type: .address)
        
        let item2 = YHItemModel(id: .id4, isNeed: true, title: "详细地址", isUserKeyBoard: true, prompts: "请填写小区、楼栋、单元室等", message: mainModel.address?.details)
        let item11 = YHItemModel(id: .id32, isNeed: true, title: "详细地址", isUserKeyBoard: true, prompts: "请选择", message: mainModel.address?.foreign)
        let item22 = YHItemModel(id: .id33, isNeed: true, title: "是否在海外居住一年及以上", isUserKeyBoard: false, prompts: "", message: "\(mainModel.is_live_oversea_year ?? 0)", leftButtonString: "是", rightButtonString: "否")
        let section1 = YHSectionItemModel(title: "现居住地址", models: [item4, item1, item2])
        let section11 = YHSectionItemModel(title: "现居住地址", models: [item4, item11, item22])
        let item3 = YHItemModel(id: .id5, isNeed: true, title: "是否办理", isUserKeyBoard: false, prompts: "", message: "\(mainModel.has_hk_id ?? 0)", leftButtonString: "是", rightButtonString: "否")
        let section2 = YHSectionItemModel(title: "港澳通行证", models: [item3])
        let addressCountry = mainModel.address?.country ?? ""
        if addressCountry.contains("中国") {
            return [section, section1, section2]
        } else {
            return [section, section11, section2]
        }
    }
    
    func getDocumentInformation() -> [YHSectionItemModel] {
        let item = YHItemModel(id: .id8, isNeed: true, title: "身份证号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.cn_identity_card?.number)
        let item1 = YHItemModel(id: .id9, isNeed: true, title: "签发地", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.cn_identity_card?.issue_at)
        let item2 = YHItemModel(id: .id10, isNeed: true, title: "签发日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.cn_identity_card?.issue_date_start_at, type: .time)
        let item3 = YHItemModel(id: .id11, isNeed: true, title: "届满日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.cn_identity_card?.issue_date_end_at, type: .time)
        let section = YHSectionItemModel(title: "中国身份证", models: [item, item1, item2, item3])
        
        let item10 = YHItemModel(id: .id12, isNeed: true, title: "身份证号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.hk_macao_pass?.number)
        let item11 = YHItemModel(id: .id13, isNeed: true, title: "签发地", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.hk_macao_pass?.issue_at)
        let item12 = YHItemModel(id: .id14, isNeed: true, title: "签发日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.hk_macao_pass?.issue_date_start_at, type: .time)
        let item13 = YHItemModel(id: .id15, isNeed: true, title: "届满日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.hk_macao_pass?.issue_date_end_at, type: .time)
        let section1 = YHSectionItemModel(title: "港澳通行证", models: [item10, item11, item12, item13])

        let item4 = YHItemModel(id: .id16, isNeed: false, title: "证件类别", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.passport?.passport_type, type: .id)
        let item5 = YHItemModel(id: .id17, isNeed: false, title: "证件号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.passport?.number)
        let item6 = YHItemModel(id: .id18, isNeed: false, title: "签发地", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.passport?.issue_at)
        let item7 = YHItemModel(id: .id19, isNeed: false, title: "签发日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.passport?.issue_date_start_at, type: .time)
        let item8 = YHItemModel(id: .id20, isNeed: false, title: "届满日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.passport?.issue_date_end_at, type: .time)
        let section2 = YHSectionItemModel(title: "护照及其他旅行证件(选填)", models: [item4, item5, item6, item7, item8])
        if mainModel.has_hk_id == 1 {
            return [section, section1, section2]
        } else {
            return [section, section2]
        }
    }
    
    func getDetailDataSource() -> [YHSectionItemModel] {
        let item = YHItemModel(id: .id21, isNeed: true, title: "主申请人姓名", isUserKeyBoard: true, prompts: "请输入", message: mainModel.username)
        let item1 = YHItemModel(id: .id22, isNeed: false, title: "曾用名", isUserKeyBoard: true, prompts: "请与户口本一致，若无则不填", message: mainModel.used_name)
        let item2 = YHItemModel(id: .id23, isNeed: false, title: "婚前姓氏", isUserKeyBoard: true, prompts: "如有则填写，仅支持输入字母", message: mainModel.surname)
        let item3 = YHItemModel(id: .id24, isNeed: true, title: "出生日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.birthday, type: .time)
        let item4 = YHItemModel(id: .id25, isNeed: true, title: "出生国家/地区", isUserKeyBoard: false, prompts: nil, message: "\(mainModel.birth_place_aboard ?? 0)", leftButtonString: "国内", rightButtonString: "国外")
        var string = ""
        if let array = mainModel.birth_place?.area {
            for item in array {
                string = string + item
            }
        }
        let item5 = YHItemModel(id: .id26, isNeed: true, title: "出生城市", isUserKeyBoard: false, prompts: "请选择", message: string, type: .address)
        let item55 = YHItemModel(id: .id34, isNeed: true, title: "出生城市", isUserKeyBoard: true, prompts: "请选择", message: mainModel.birth_place?.foreign)
        let item6 = YHItemModel(id: .id27, isNeed: true, title: "性别", isUserKeyBoard: false, prompts: "请选择", message: mainModel.sex, type: .sex)
        let item7 = YHItemModel(id: .id28, isNeed: true, title: "婚姻状况", isUserKeyBoard: false, prompts: "请选择", message: mainModel.married, type: .marry)
        var item555 = item5
        if mainModel.birth_place_aboard == 1 {
            item555 = item55
        }
        let section = YHSectionItemModel(title: "主申请人信息", models: [item, item1, item2, item3, item4, item555, item6, item7])
        
        let item8 = YHItemModel(id: .id29, isNeed: true, title: "手机号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.mobile)
        let item9 = YHItemModel(id: .id30, isNeed: true, title: "电子邮箱", isUserKeyBoard: true, prompts: "请输入", message: mainModel.email)
        let section1 = YHSectionItemModel(title: "联系方式", models: [item8, item9])
        
        let item10 = YHItemModel(id: .id31, isNeed: true, title: "是否办理过", isUserKeyBoard: false, prompts: nil, message: "\(mainModel.has_hk_id ?? 0)", leftButtonString: "是", rightButtonString: "否")
        let item11 = YHItemModel(id: .id29, isNeed: true, title: "香港身份证号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.hk_id_number)
        let section2 = YHSectionItemModel(title: "香港身份证", models: [item10])
        let section22 = YHSectionItemModel(title: "香港身份证", models: [item10, item11])
        var section222 = section2
        if mainModel.has_hk_id == 1 {
            section222 = section22
        }
        return [section, section1, section222]
    }
    
    func getIDCardDataSource() -> [YHSectionItemModel] {
        let item = YHItemModel(id: .id5, isNeed: true, title: "身份证", isUserKeyBoard: false, prompts: "请输入", message: nil)
        let section = YHSectionItemModel(title: "中国身份证(选填)", models: [item])
        let item1 = YHItemModel(id: .id6, isNeed: true, title: "港澳通行证(选填)", isUserKeyBoard: false, prompts: "请输入", message: nil)
        let section1 = YHSectionItemModel(title: "中国身份证(选填)", models: [item1])
        if mainModel.has_hk_id == 1 {
            return [section, section1]
        } else {
            return [section]
        }
    }
    
    func updateModel(_ item: YHItemModel) {
        guard let type = item.id else { return }
        switch type {
            
        case .id1:
            mainModel.nationality = item.message
        case .id2:
            mainModel.address?.country = item.message
        case .id3:
            mainModel.address?.area = item.value
        case .id4:
            mainModel.address?.details = item.message
        case .id5:
            mainModel.has_hk_id = item.value?.first?.int
        case .id6:
            printLog("1")
        case .id7:
            printLog("1")
        case .id8:
            mainModel.certificates?.cn_identity_card?.number = item.message
        case .id9:
            mainModel.certificates?.cn_identity_card?.issue_at = item.message
        case .id10:
            mainModel.certificates?.cn_identity_card?.issue_date_start_at = item.message
        case .id11:
            mainModel.certificates?.cn_identity_card?.issue_date_end_at = item.message
        case .id12:
            mainModel.certificates?.hk_macao_pass?.number = item.message
        case .id13:
            mainModel.certificates?.hk_macao_pass?.issue_at = item.message
        case .id14:
            mainModel.certificates?.hk_macao_pass?.issue_date_start_at = item.message
        case .id15:
            mainModel.certificates?.hk_macao_pass?.issue_date_end_at = item.message
        case .id16:
            mainModel.certificates?.passport?.passport_type = item.value?.first
        case .id17:
            mainModel.certificates?.passport?.number = item.message
        case .id18:
            mainModel.certificates?.passport?.issue_at = item.message
        case .id19:
            mainModel.certificates?.passport?.issue_date_start_at = item.message
        case .id20:
            mainModel.certificates?.passport?.issue_date_end_at = item.message
        case .id21:
            mainModel.username = item.message
        case .id22:
            mainModel.used_name = item.message
        case .id23:
            mainModel.surname = item.message
        case .id24:
            mainModel.birthday = item.message
        case .id25:
            mainModel.birth_place_aboard = item.value?.first?.int
        case .id26:
            mainModel.birth_place?.area  = item.value
        case .id27:
            mainModel.sex = item.value?.first
        case .id28:
            mainModel.married = item.value?.first
        case .id29:
            mainModel.mobile = item.message
        case .id30:
            mainModel.email = item.message
        case .id31:
            mainModel.has_hk_id = item.value?.first?.int
        case .id32:
            mainModel.address?.foreign = item.message
        case .id33:
            mainModel.is_live_oversea_year = item.value?.first?.int
        case .id34:
            mainModel.birth_place?.foreign = item.message
        }
    }
    
    
    func requestMainInformation(_ orderID: String, callBackBlock:@escaping (_ success: YHMainInformationModel?, _ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/order-information/get_information?order_id=\(orderID)"
        let _ = YHNetRequest.getRequest(url: strUrl) { [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            guard let model = NetBaseModel.deserialize(dict: json) else {
                let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())
                
                callBackBlock(nil,error)
                return
            }
            let dic = model.data?.peel
            guard let result = YHMainInformationModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                callBackBlock(nil,nil)
                return
            }
            self.mainModel = result
            callBackBlock(result, nil)
        } failBlock: { err in
            callBackBlock(nil,err)
        }
    }
    
    func updateMainInformation(_ saveType: YHSaveType, callBackBlock:@escaping (_ success: Bool)->()) {
        var type = ""
        if saveType == .save {
            type = "save"
        } else {
            type = "submit"
        }
        let area: [String] = mainModel.address?.area ?? []
        let birthArea: [String] = mainModel.birth_place?.area ?? []
        let address = ["area": area,
                       "country": mainModel.address?.country ?? "",
                       "details": mainModel.address?.details ?? "",
                       "foreign": mainModel.address?.foreign ?? ""] as [String : Any]
        let birthPlace = ["area": birthArea,
                          "country": mainModel.birth_place?.country ?? "",
                          "details": mainModel.birth_place?.details ?? "",
                          "foreign": mainModel.birth_place?.foreign ?? ""] as [String : Any]
        let cnID = ["img_back": mainModel.certificates?.cn_identity_card?.img_back ?? "",
                    "img_front": mainModel.certificates?.cn_identity_card?.img_front ?? "",
                    "issue_at": mainModel.certificates?.cn_identity_card?.issue_at ?? "",
                    "issue_date_end_at": mainModel.certificates?.cn_identity_card?.issue_date_end_at ?? "",
                    "issue_date_start_at": mainModel.certificates?.cn_identity_card?.issue_date_start_at ?? "",
                    "number": mainModel.certificates?.cn_identity_card?.number ?? ""] as [String : Any]
        let hkID = ["img_back": mainModel.certificates?.hk_macao_pass?.img_back ?? "",
                    "img_front": mainModel.certificates?.hk_macao_pass?.img_front ?? "",
                    "issue_at": mainModel.certificates?.hk_macao_pass?.issue_at ?? "",
                    "issue_date_end_at": mainModel.certificates?.hk_macao_pass?.issue_date_end_at ?? "",
                    "issue_date_start_at": mainModel.certificates?.hk_macao_pass?.issue_date_start_at ?? "",
                    "number": mainModel.certificates?.hk_macao_pass?.number ?? ""] as [String : Any]
        let passport = ["img_back": mainModel.certificates?.passport?.img_back ?? "",
                        "img_front": mainModel.certificates?.passport?.img_front ?? "",
                        "issue_at": mainModel.certificates?.passport?.issue_at ?? "",
                        "issue_date_end_at": mainModel.certificates?.passport?.issue_date_end_at ?? "",
                        "issue_date_start_at": mainModel.certificates?.passport?.issue_date_start_at ?? "",
                        "number": mainModel.certificates?.passport?.number ?? "",
                        "passport_type": mainModel.certificates?.passport?.passport_type ?? 0] as [String : Any]
        let params: [String : Any] = ["address": address,
                    "address_aboard": mainModel.address_aboard ?? 0,
                    "age":mainModel.age ?? "",
                    "birth_place": birthPlace,
                    "birth_place_aboard": mainModel.birth_place_aboard ?? 0,
                    "birthday": mainModel.birthday ?? "",
                    "email": mainModel.email ?? "",
                    "has_hk_id": mainModel.has_hk_id ?? 0,
                    "hk_id_number": mainModel.hk_id_number ?? "",
                    "id": mainModel.id ?? 0,
                    "is_handled": mainModel.is_handled ?? "",
                    "is_live_oversea_year": mainModel.is_live_oversea_year ?? 0,
                    "married": mainModel.married ?? 0,
                    "mobile": mainModel.mobile ?? "",
                    "nationality": mainModel.nationality ?? "",
                    "order_id": mainModel.order_id ?? "",
                    "sex": mainModel.sex ?? 0,
                    "step": mainModel.step ?? 0,
                    "surname": mainModel.surname ?? "",
                    "used_name": mainModel.used_name ?? "",
                    "username": mainModel.username ?? "",
                    "save_type": type,
                    "username_pinyin": ["family_name": mainModel.username_pinyin?.family_name ?? "",
                                        "given_name": mainModel.username_pinyin?.given_name ?? ""],
                    "certificates":["cn_identity_card": cnID,
                                    "hk_macao_pass": hkID,
                                    "passport": passport]
                    ]
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/order-information/update_information"
        let _ = YHNetRequest.postRequest(url: strUrl, params: params) { [weak self] json, code in
            guard let self = self else { return }
            callBackBlock(true)
        } failBlock: { err in
            callBackBlock(false)
        }
    }
}
