//
//  LNOrderRoomViewModel.swift
//  Gami
//
//  Created by OneeChan on 2026/4/3.
//

import Foundation


protocol LNOrderRoomViewModelNotify: LNRoomViewModelNotify {
    func onOrderRoomUserSeatApplyChanged(session: LNOrderRoomViewModel, apply: LNOrderRoomApplyingSeatType)
}
extension LNOrderRoomViewModelNotify {
    func onOrderRoomUserSeatApplyChanged(session: LNOrderRoomViewModel, apply: LNOrderRoomApplyingSeatType) { }
}

class LNOrderRoomViewModel: LNRoomSession {
    private(set) var waitingForSeat: LNOrderRoomApplyingSeatType = .none {
        didSet {
            if oldValue != waitingForSeat {
                LNEventDeliver.notifyEvent { ($0 as? LNOrderRoomViewModelNotify)?.onOrderRoomUserSeatApplyChanged(session: self, apply: self.waitingForSeat) }
            }
        }
    }
    
    override func handleSystemMessage(message: LNRoomPushMessage) {
        super.handleSystemMessage(message: message)
        
        if message.cmd == .MicClear,
           let cmd: LNRoomMicClearMessage = message.decodeCmdMessage(),
           cmd.type == self.waitingForSeat {
            self.waitingForSeat = .none
        }
    }
}
 
extension LNOrderRoomViewModel {
    // 申请列表
    func getApplyList(type: LNRoomApplySeatType, next: String?, filter: String?,
                      handler: @escaping (LNRoomMicApplyListResponse?) -> Void) {
        LNHttpManager.shared.getApplySeatList(roomId: roomId, searchType: type, filter: filter,
                                              size: 30, next: next ?? "")
        { res, err in
            runOnMain {
                handler(res)
            }
            res?.list.forEach {
                LNProfileManager.shared.updateProfileCache(uid: $0.user.userNo, name: $0.user.nickname, avatar: $0.user.avatar)
            }
            if let err {
                showToast(err.errorDesc)
            }
        }
    }
    
    // 申请列表的技能类别
    func getApplyListCategory(handler: @escaping (LNRoomGameCategoryResponse?) -> Void) {
        LNHttpManager.shared.getApplySeatCategory(roomId: roomId) { res, err in
            runOnMain {
                handler(res)
            }
            if let err {
                showToast(err.errorDesc)
            }
        }
    }
    
    
    // 用户列表
    func getRoomUserList(next: String?, playmete: Bool, filter: String?,
                         handler: @escaping (LNRoomUserListResponse?) -> Void) {
        LNHttpManager.shared.getRoomUserList(
            roomId: roomId, size: 30, next: next ?? "",
            playmete: playmete, filter: filter)
        { res, err in
            runOnMain {
                handler(res)
            }
            
            res?.list.forEach {
                LNProfileManager.shared.updateProfileCache(uid: $0.user.userNo, name: $0.user.nickname, avatar: $0.user.avatar)
            }
            
            if let err {
                showToast(err.errorDesc)
            }
        }
    }
    
    // 申请列表的技能类别
    func getPlaymateCategory(handler: @escaping (LNRoomGameCategoryResponse?) -> Void) {
        LNHttpManager.shared.getRoomPlaymateCategory(roomId: roomId) { res, err in
            runOnMain {
                handler(res)
            }
            if let err {
                showToast(err.errorDesc)
            }
        }
    }
}

extension LNOrderRoomViewModel: LNRoomViewModelNotify {
    func onRoomUserDidJoinOnSeat(session: LNRoomSession, newSeat: LNRoomSeatItem) {
        waitingForSeat = .none
    }
    
    func onRoomUserDidApplySeat(session: LNRoomSession, index: Int) {
        waitingForSeat = index == LNOrderRoomSeatNum.guest.rawValue ?
            .guest : index != LNOrderRoomSeatNum.host.rawValue ?
            .playmate : .none
    }
    
    func onRoomUserDidCancelApplySeat(session: LNRoomSession) {
        waitingForSeat = .none
    }
}
