//
//  YHServiceCenterMainViewModel.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/23.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import SmartCodable
class YHServiceCenterMainViewModel: YHBaseViewModel {
    //合同列表 数据
    var arrContactList : [YHContactItemModel] = []
    
    //合同对应的状态
    var arrContactStausItmes: [YHContactMainItemStatusModel] = []
    
    //信息完善列表
    var arrInfoFillStep : [YHInformationPerfectModel] = []
    var myInfoFillStep : Int = 0
    var informationFillStepModel : YHPersonInfoFillStepModel?
    
    //我的评分
    var myScoreResultData : YHScoreResultModel?//网络请求的结果数据
    var localScoreData : YHScoreDataModel? //本地缓存的数据
    
}

//请求接口
extension YHServiceCenterMainViewModel {
    //获取我的评分结果
    func getMyScoreDetail( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        loadLocalScoreData()
        
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.scoreDetailApi
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [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(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHScoreResultModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                callBackBlock(false,nil)
                return
            }
            self.myScoreResultData = resultModel
            
            if let localScoreData = self.localScoreData  {
                localScoreData.age_score!.resultData = resultModel.age_score
                localScoreData.language_score!.resultData = resultModel.language_score
                localScoreData.background_score!.resultData = resultModel.background_score
                localScoreData.work_experience_score!.resultData = resultModel.work_experience_score
                localScoreData.education_score!.resultData = resultModel.education_score
                localScoreData.talent_list_score!.resultData = resultModel.talent_list_score
            }
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    //请求合同项
    func getContactList(callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.contractListApi
        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(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHContactItemHoldModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                arrContactList = []
                callBackBlock(false,nil)
                return
            }
            
            arrContactList = resultModel.orders
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    
    //请求合同的状态
    func getContactStatus( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.contractStatusApi
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [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(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHContactMainItemStatusModel2.deserialize(dict: dic as? [AnyHashable : Any]) else {
                self.arrContactStausItmes = []
                callBackBlock(false,nil)
                return
            }

            if resultModel.fill_info_step > 6 {
                let model = YHContactMainItemStatusModel()
                model.type = 1
                model.isFinished = true
                arrContactStausItmes.append(model)
                
                let model2 = YHContactMainItemStatusModel()
                model2.type = 2
                model2.isFinished = false
                arrContactStausItmes.append(model2)
                
                let model3 = YHContactMainItemStatusModel()
                model3.type = 3
                model3.isFinished = false
                arrContactStausItmes.append(model3)
            } else {
                
                let model = YHContactMainItemStatusModel()
                model.type = 1
                model.isFinished = false
                arrContactStausItmes.append(model)
                
                
                let model2 = YHContactMainItemStatusModel()
                model2.type = 2
                model2.isFinished = false
                arrContactStausItmes.append(model2)
            }
            
            callBackBlock(true,nil)
        } failBlock: { err in
            self.arrContactStausItmes = []
            callBackBlock(false,err)
        }
    }
    
    
    
    //请求 信息表的填写状态
    func getPersonInfoFillStep( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.informationStepApi
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [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())
                
                self.arrInfoFillStep = []
                informationFillStepModel = nil
                callBackBlock(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHPersonInfoFillStepModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                self.arrInfoFillStep = []
                informationFillStepModel = nil
                callBackBlock(false,nil)
                return
            }
            informationFillStepModel = resultModel
            myInfoFillStep = resultModel.step
            
            let arr = [1,2,3,4,5,6]
            let target = myInfoFillStep //for test hjl 待填写的项目
            for item in arr {
                let model = YHInformationPerfectModel()
                model.type = item
                arrInfoFillStep.append(model)
                
                if item < target {
                    model.isFinished = true
                } else {
                    model.isFinished = false
                }
                
                
                if item == target  {
                    model.isDoing = true
                }
                
                if item == target + 1 {
                    model.isNextDoingCell = true
                } else {
                    model.isNextDoingCell = false
                }
            }
            
            callBackBlock(true,nil)
        } failBlock: { err in
            self.arrInfoFillStep = []
            self.informationFillStepModel = nil
            callBackBlock(false,err)
        }
    }
    
    
    //提交 温馨提示弹窗
    func submitSweetNotifyOp( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Users.submitSweetNotifyApi
        let _ = YHNetRequest.postRequest(url: strUrl,params: params) {json, code in
            //1. json字符串 转 对象
            guard let model = NetBaseModel.deserialize(dict: json) else {
                let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())

                callBackBlock(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
}

//其他方法
extension YHServiceCenterMainViewModel {
    //加载本地 plist数据
    private func loadLocalScoreData() {
        if let path = Bundle.main.path(forResource: "scoreOptionData", ofType: "plist") {
            if let plistData = FileManager.default.contents(atPath: path) {
                do {
                    let listDictionary = try PropertyListSerialization.propertyList(from: plistData, options: [], format: nil) as! [String: Any]
                    guard let model = YHScoreDataModel.deserialize(dict: listDictionary) else {
                        localScoreData = nil
                        return
                    }
                    localScoreData = model
                } catch {
                    localScoreData = nil
                }
            } else {
                localScoreData = nil
            }
        } else {
            localScoreData = nil
        }
    }
}

//合同数据结构
struct YHContactItemHoldModel : SmartCodable {
    var orders : [YHContactItemModel] = []
}
