//
//  YHCertificateViewModel.swift
//  galaxy
//
//  Created by edy on 2024/3/15.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import SmartCodable

class YHCertificateViewModel: YHBaseViewModel {
    var certificateEntryModel : YHCertificateEntryModel?
    
    var arrQuestionAndAnswer : [YHQuestionAndAnswerModel?] = []
    
    //获取问答列表
    func getQuestionAndAnserList( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Certificate.questionAndAnswerApi
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            printLog("model 是 ==> \(json)")
            
            if json.code == 200 {
                let dic = json.data
                
                guard let resultModel = [YHQuestionAndAnswerModel].deserialize(array: dic as? [Any]) else {
                    let err = YHErrorModel(errorCode: YHErrorCode.dictParseError.rawValue, errorMsg: YHErrorCode.dictParseError.description())
                    
                    callBackBlock(false,err)
                    return
                }
                self.arrQuestionAndAnswer = resultModel
                
                callBackBlock(true,nil)
            } else {
                
                let err = YHErrorModel(errorCode: Int32(json.code), errorMsg: json.msg.isEmpty ? "" : json.msg)
                callBackBlock(false,err)
                
            }
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    
    //我的证件  状态数据统计
    func getCertificateStatisticData( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Certificate.statisticApi
        
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            printLog("model 是 ==> \(json)")
            
            if json.code == 200 {
                let dic = json.data
                guard let resultModel = YHCertificateEntryModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                    let err = YHErrorModel(errorCode: YHErrorCode.dictParseError.rawValue, errorMsg: YHErrorCode.dictParseError.description())
                    
                    callBackBlock(false,err)
                    return
                }
                self.certificateEntryModel = resultModel
            } else {
                
                let err = YHErrorModel(errorCode: Int32(json.code), errorMsg: json.msg.isEmpty ? "" : json.msg)
                callBackBlock(false,err)
                
            }
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    func getUploadCertificateList(isSilient:Bool = false, callBack:@escaping (_ listInfo: YHUploadCertificateInfo?, _ error:YHErrorModel?)->()) {
        
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.familyInfoApi
        
        if !isSilient {
            YHHUD.show(.progress(message: "数据加载中..."))
        }
         
        let params = ["id": 0]
        let _ = YHNetRequest.getRequest(url: strUrl, params:params) { json, code in
            
            if !isSilient {
                YHHUD.hide()
            }
                         
            printLog("model 是 ==> \(json)")
            let dic = json.data
            
            if code == NetWorkCode.success.rawValue {
                guard let result = YHUploadCertificateInfo.deserialize(dict: dic as? Dictionary ) else {
                    callBack(nil, YHErrorModel(errorCode:Int32(json.code), errorMsg:"解析失败".local))
                    return
                }
                callBack(result, nil)
                
            } else {
                let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
                callBack(nil, error)
            }
            
        } failBlock: { err in
            callBack(nil, err)
        }
    }
}
