//
//  IMAIViewModel.swift

//
//  Created by Leon on 2025/8/26.
//
import Combine
/// 单例
class IMAIViewModel {
    static var shared = IMAIViewModel()

    
    /// Current Chat aiId
    var aiId: Int = 0
    @Published var aiIMInfo: IMAIUserInfo? {
        didSet {
            if let id = aiIMInfo?.aiId, id > 0{
                aiId = id
            }
            chatBubble = aiIMInfo?.chatBubble
            chatBackgroundImg = aiIMInfo?.backgroundImg
            heartbeatVal = aiIMInfo?.aiUserHeartbeatRelation?.heartbeatVal
        }
    }
    
    @Published var heartbeatVal : CGFloat?{
        didSet {
            aiIMInfo?.aiUserHeartbeatRelation?.heartbeatVal = heartbeatVal
        }
    }
    
    @Published var heartbeatLevelUpString: String?{
        didSet{
            if let string = heartbeatLevelUpString, string.count > 0{
                heartbeatLevelUpString = nil // reset
            }
        }
    }
    @Published var heartbeatLevelDownString: String?{
        didSet{
            if let string = heartbeatLevelDownString, string.count > 0{
                heartbeatLevelDownString = nil
            }
        }
    }
    
    var chatBubble: ChatBubble?
    /// 从IM Base info中或者， 从 Chat setting接口中处理。
    var chatBackgroundImg: String?

    

    var chatSetting: IMChatSetting?
    
    // MARK: - Public
    func updateAI(_ ai: IMAIUserInfo?){
        aiIMInfo = ai
    }
    
    func updateHeartbeatVal(_ val: CGFloat?){
        guard let theVal = val else{return}
        heartbeatVal = theVal
    }

    // MARK: - Functions

    func showChatModelInsufficentCoinSheet() {
        guard aiId > 0 else {
            return
        }

        loadChatSetting { result, chatSetting in
            if result, let setting = chatSetting {
                let sheet = ChatModePickInsufficientCoinSheet(currentSelectedModelCode: setting.modelCode)
                sheet.show()
            }
        }
    }

    // MARK: Setting

    func loadChatSetting(completion: ((Bool, IMChatSetting?) -> Void)?) {
        guard aiId > 0 else {
            completion?(false, nil)
            assert(false)
            return
        }

        AIRoleProvider.request(.chatSetupGet(aiId: aiId), modelType: IMChatSetting.self) { [weak self] result in
            switch result {
            case let .success(model):
                self?.chatSetting = model
                completion?(true, model)
            case .failure:
                completion?(false, nil)
            }
        }
    }

    func updateChatModel(code: String?, completion: ((Bool) -> Void)?) {
        var params = [String: Any]()

        params.updateValue(aiId, forKey: "aiId")
        params.updateValue(code ?? "", forKey: "code")

        AIRoleProvider.request(.chatModelSetup(params: params), modelType: EmptyModel.self) { result in
            Hud.hideIndicator()
            switch result {
            case .success:
                // 发送通知更新其他页面
                NotificationCenter.post(name: .chatSettingUpdated)
                completion?(true)
            case .failure:
                completion?(false)
            }
        }
    }
}
