//
//  DiscoverHomeRolesViewModel.swift

//
//  Created by Leon on 2025/9/13.
//

/// 每个Character下一个
class DiscoverHomeRolesViewModel {
    /// 固定
    var character: DictNode?
    // @Required
    var tags: [DictNode]?

    var selectTagCode: String?
    
    // var filterParams = [String:Any]()
    var filterModel : RolesFilterModel?

    func loadRoles(pn: Int, codes: [String], exList: [Int]? = nil, completion: ((_ status: Bool, _ datas: [AIRoleInfo]?) -> Void)?) {

        var request = AIRoleListRequest()
        request.pn = pn
        if let theCharacterCode = character?.code{
            request.characterCodeList = [theCharacterCode]
        }
        if codes.count > 0{
            request.tagCodeList = codes
        }
        request.exList = exList
        var params = request.toNonNilDictionary()
        
        if let filterCondition = filterModel{
            // 筛选条件
            if let sexs = filterCondition.sex, sexs.count > 0, let firstSex = sexs.first{ // 后续可能会改成多选
                params.updateValue(firstSex.rawValue, forKey: "sex")
            }
            
            if let age = filterCondition.age{
                params.updateValue(age.rawValue, forKey: "age")
            }
            
            if let roleCodes = filterCondition.roleCodeList, roleCodes.count > 0{
                params.updateValue(roleCodes, forKey: "roleCodeList")
            }
        }

        // params = params.merged(with: filterParams)
        
        DiscoverProvider.request(.homeRolesList(params: params), modelType: [AIRoleInfo].self) { result in
            switch result {
            case let .success(model):
                completion?(true, model)
            case .failure:
                completion?(false, nil)
            }
        }
    }
}
