//
//  SessionController+Operate.swift
//  LegendTeam
//
//  Created by 梁博 on 20/12/21.
//

import Foundation
import UIKit
import NIMSDK

extension SessionController {
    func requestUserInfo(userId: Int) {
        if userId <= 0 {
            return
        }
        
        IMAIViewModel.shared.aiId = userId
        
        AIRoleProvider.request(.imUserBaseInfo(aiId: userId), modelType: IMAIUserInfo.self) {[weak self] result in
            Hud.hideIndicator()
            switch result {
            case let .success(model):
                if model != nil {
                    self?.aiInfo = model
                    IMAIViewModel.shared.updateAI(model)
                    
                    self?.reloadViews()
                    
                    // 首次加载数据
                    self?.loadMessages {[weak self] messageDatas, error in
                        if self?.firstLoadedUser == false{
                            self?.scrollToBottom(self?.tableView)
                            self?.firstLoadedUser = true
                        }
                    }
                    
                }
            case let .failure(error):
                switch error {
                case let .serviceError(code, _):
                    if code == .userExist || code == .aiRoleNotExist {
                        let alert = Alert(title: "The Role has been deleted", text: "The role has been deleted and cannot be accessed and interacted.")
                        let action1 = AlertAction(title: "Got it", actionStyle: .confirm) {[weak self] in
                            self?.close()
                        }
                        alert.addAction(action1)
                        alert.show()
                    }
                default:
                    break
                }
                break
            }
        }
    }

    func testOperate(){
        
        // Create image message
        let imagePath = AppConst.cachePath
        
        let message = V2NIMMessageCreator.createImageMessage(imagePath, name: "imageName", sceneName: "nim_defalt_im", width: 200, height: 200)
        
        
    }
}
