//
//  YHHomePageViewModel.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/4.
//  Copyright © 2024 www.davidhuang.com. All rights reserved.
//

import UIKit

class YHHomePageViewModel : YHBaseViewModel {
    //数据源
    var configModel:YHHomeCofigMode?
}

//接口
extension YHHomePageViewModel {
    //请求首页的配置
    func getHomeConfig(callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Common.homeConfigApi
        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 = YHHomeCofigMode.deserialize(dict: dic as? [AnyHashable : Any]) else {
                callBackBlock(false,nil)
                return
            }
            configModel = resultModel
            
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
}
