//
//  ChatSettingListController.swift

//
//  Created by Leon on 2025/8/16.
//

import UIKit

class ChatSettingListController: CLViewController<ChatSettingListView> {
    var aiId : Int!
    
    var data: IMChatSetting?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupViews()
        setupDats()
        setupEvents()
    }
    

    private func setupViews() {
        navigationView.alpha0Title = "Setting"
    }
    
    private func setupDats(){
        loadMyChatSetting()
    }
    
    private func setupEvents(){
        NotificationCenter.default.addObserver(self, selector: #selector(notifyChatSettingUpdated), name: AppNotificationName.chatSettingUpdated.notificationName, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(notifyChatSettingUpdated), name: AppNotificationName.chatPersonaUpdated.notificationName, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(notifyChatBackgroundChanged), name: AppNotificationName.chatSettingBackgroundChanged.notificationName, object: nil)
    }

    private func loadMyChatSetting(){
        Hud.showIndicator()
        AIRoleProvider.request(.chatSetupGet(aiId: aiId), modelType: IMChatSetting.self) {[weak self] result in
            Hud.hideIndicator()
            switch result {
            case .success(let model):
                self?.data = model
                self?.container.config(model)
            case .failure:
                break
            }
        }
    }
    
    @objc private func notifyChatSettingUpdated(){
        loadMyChatSetting()
    }
    
    @objc private func notifyChatBackgroundChanged(){
        self.navigationController?.popViewController(animated: false)
    }
}
