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


class YHFamilyRequestViewModel {
    
    //请求所有家庭成员信息
     func getFamilyInfoList(orderId:Int, callBackBlock:@escaping (_ listInfo: YHFamilyMemberGroupInfo?,_ error:YHErrorModel?)->()) {
        
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.familyInfoApi
        let params = ["order_id": orderId]
         YHHUD.show(.progress(message: "数据加载中..."))
        
        let _ = YHNetRequest.getRequest(url: strUrl, params:params) { json, code in
            YHHUD.hide()
                         
            printLog("model 是 ==> \(json)")
            let dic = json.data
            
            if json.code == 200 {
                guard let resultModel = YHFamilyMemberGroupInfo.deserialize(dict: dic as? Dictionary ) else {
                    let error = YHErrorModel(errorCode: YHErrorCode.dictParseError.rawValue, errorMsg: YHErrorCode.dictParseError.description())
                    callBackBlock(nil, error)
                    return
                }
                _ = resultModel.checkDefaultValue()
                callBackBlock(resultModel, nil)
                
            } else {
                let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
                callBackBlock(nil, error)
            }
            
        } failBlock: { err in
            YHHUD.hide()
            callBackBlock(nil, err)
        }
    }
    
    // 添加家庭成员
    func addOrSaveFamilyMember(params:[String:Any], isShowLoading:Bool = true, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
       
        if isShowLoading {
            YHHUD.show(.progress(message: "数据加载中..."))
        }

       let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.familyInfoApi
       
       let _ = YHNetRequest.postRequest(url: strUrl, params:params) { json, code in
           
           if isShowLoading {
               YHHUD.hide()
           }
           printLog("model 是 ==> \(json)")
           if json.code == 200 {
               callBackBlock(true,nil)
           } else {
               callBackBlock(false, YHErrorModel(errorCode:json.code, errorMsg:json.msg))
           }
           
       } failBlock: { err in
           if isShowLoading {
               YHHUD.hide()
           }
           callBackBlock(false,err)
       }
   }
    
    func deleteFamilyMember(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.deleteFamilyMemberApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:params) { json, code in
            YHHUD.hide()
            printLog("model 是 ==> \(json)")
            if json.code == 200 {
                callBackBlock(true,nil)
            } else {
                callBackBlock(false, YHErrorModel(errorCode:json.code, errorMsg:json.msg))
            }
            
        } failBlock: { err in
            YHHUD.hide()
            callBackBlock(false,err)
        }
    }
    // 提交所有家庭成员信息
    func submitAllFamilyInfo(orderId:Int, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.submitAllFamilyInfoApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:["order_id":orderId]) { json, code in
            YHHUD.hide()
            printLog("model 是 ==> \(json)")
            if json.code == 200 {
                callBackBlock(true,nil)
            } else {
                callBackBlock(false, YHErrorModel(errorCode:json.code, errorMsg:json.msg))
            }
            
        } failBlock: { err in
            YHHUD.hide()
            callBackBlock(false,err)
        }
    }
    
    func requestFamilyMemberInfo(orderId:Int, detailId:Int, callBackBlock:@escaping (_ detail: YHFamilyMember?, _ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.familyInfoDetailApi
        
        let _ = YHNetRequest.getRequest(url: strUrl, params:["order_id":orderId,
                                                             "id":detailId]) { json, code in
            
            YHHUD.hide()
            printLog("model 是 ==> \(json)")
            YHHUD.hide()
             
            let dic = json.data
            if json.code == 200 {
                guard let resultModel = YHFamilyMember.deserialize(dict: dic as? Dictionary ) else {
                    let error = YHErrorModel(errorCode: YHErrorCode.dictParseError.rawValue, errorMsg: YHErrorCode.dictParseError.description())
                    callBackBlock(nil, error)
                    return
                }
                _ = resultModel.checkDefaultValue()
                callBackBlock(resultModel, nil)
                
            } else {
                let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
                callBackBlock(nil, error)
            }
            
        } failBlock: { err in
            YHHUD.hide()
            callBackBlock(nil, err)
        }
    }
    
    // memberType 只能传 父亲、母亲、配偶
    func requestFamilyMemberDetailInfo(memberType:YHFamilyMemberType, orderId:Int, callBack:((_ detail:YHFamilyMember?)->Void)?) {
        
        let params:[String:Any] =  ["order_id": orderId]
        self.getFamilyInfoList(orderId:orderId) {
            listInfo, error in
            guard let callBack = callBack else { return }
            if let listInfo = listInfo {
                if memberType == .spouse {
                    callBack(listInfo.spouse)
                } else if memberType == .father {
                    callBack(listInfo.father)
                } else if memberType == .mother {
                    callBack(listInfo.mother)
                } else {
                    callBack(nil)
                }
            }
        }
    }
}
