//
//  LNHttpManager+Room.swift
//  Gami
//
//  Created by OneeChan on 2026/3/11.
//

import Foundation


private let kNetPath_Room_Ability = "/live/host/info"
private let kNetPath_Room_Create = "/live/room/create"
private let kNetPath_Room_Update = "/live/room/update"

private let kNetPath_Room_Mic_Invite_Send = "/live/mic/invite/send"
private let kNetPath_Room_Mic_Apply_Submit = "/live/mic/apply/submit"
private let kNetPath_Room_Mic_Apply_List = "/live/mic/apply/list"
private let kNetPath_Room_Mic_Apply_Clear = "/live/mic/apply/clear"
private let kNetPath_Room_Mic_Apply_Cancel = "/live/mic/apply/cancel"
private let kNetPath_Room_Mic_Apply_Audit = "/live/mic/apply/audit"
private let kNetPath_Room_Mic_Apply_category = "/live/mic/apply/category"

private let kNetPath_Room_User_List = "/live/viewer/list"
private let kNetPath_Room_User_Category = "/live/viewer/category"

private let kNetPath_Room_Entrance = "/live/room/alive"
private let kNetPath_Room_Random = "/live/room/random"

private let kNetPath_Room_Cur = "/live/view/watching"

private let kNetPath_Room_Search = "/live/room/search"
private let kNetPath_Room_List = "/live/room/list"

private let kNetPath_Room_Unavailable = "/live/room/unavailable"


// MARK: 房间管理
extension LNHttpManager {
    func getCreateRoomAbility(completion: @escaping (LNRoomAbilityVO?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Ability, completion: completion)
    }
    
    func createRoom(roomTitle: String, roomCover: String, forbidAudio: Bool,
                    completion: @escaping (LNRoomCreateResponse?, LNHttpError?) -> Void)
    {
        post(path: kNetPath_Room_Create, params: [
            "roomTitle": roomTitle,
            "roomCover": roomCover,
            "forbidAudio": forbidAudio
        ], completion: completion)
    }
    
    func updateRoom(roomId: String, roomTitle: String, roomCover: String,
                    forbidAudio: Bool, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Update, params: [
            "roomId": roomId,
            "roomTitle": roomTitle,
            "roomCover": roomCover,
            "forbidAudio": forbidAudio
        ], completion: completion)
    }
}
 
// MARK: 麦位管理
extension LNHttpManager {
    func applySeat(roomId: String, index: Int,
                   completion: @escaping (LNHttpError?) -> Void)
    {
        var params: [String: Any] = [
            "roomId": roomId
        ]
        params["index"] = index
        post(path: kNetPath_Room_Mic_Apply_Submit, params: params, completion: completion)
    }
    
    func getApplySeatList(roomId: String, searchType: LNRoomApplySeatType, filter: String?, size: Int, next: String,
                          completion: @escaping (LNRoomMicApplyListResponse?, LNHttpError?) -> Void)
    {
        var params: [String: Any] = [
            "roomId": roomId,
            "searchType": searchType.rawValue,
            "page": [
                "size": size,
                "next": next
            ]
        ]
        if let filter, !filter.isEmpty {
            params["skillCategories"] = [filter]
        }
        
        post(path: kNetPath_Room_Mic_Apply_List, params: params, completion: completion)
    }
    
    func getApplySeatCategory(roomId: String, completion: @escaping (LNRoomGameCategoryResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Mic_Apply_category, params: [
            "id": roomId
        ], completion: completion)
    }
    
    func clearApplySeatList(roomId: String, searchType: LNRoomApplySeatType,
                            completion: @escaping (LNHttpError?) -> Void)
    {
        post(path: kNetPath_Room_Mic_Apply_Clear, params: [
            "roomId": roomId,
            "searchType": searchType.rawValue
        ], completion: completion)
    }
    
    func cancelApplySeat(roomId: String,
                         completion: @escaping (LNHttpError?) -> Void)
    {
        post(path: kNetPath_Room_Mic_Apply_Cancel, params: [
            "roomId": roomId
        ], completion: completion)
    }
    
    func handleApplySeat(roomId: String, applyId: String, accept: Bool = true,
                         completion: @escaping (LNHttpError?) -> Void)
    {
        post(path: kNetPath_Room_Mic_Apply_Audit, params: [
            "roomId": roomId,
            "applyId": applyId,
            "pass": accept
        ], completion: completion)
    }
}

// MARK: 观众
extension LNHttpManager {
    func getRoomUserList(roomId: String, size: Int, next: String, playmete: Bool,
                         filter: String?, completion: @escaping (LNRoomUserListResponse?, LNHttpError?) -> Void) {
        var param: [String: Any] = [
            "roomId": roomId,
            "page": [
                "size": size,
                "next": next
            ],
            "playmete": playmete
        ]
        if let filter, !filter.isEmpty {
            param["skillCategories"] = [filter]
        }
        post(path: kNetPath_Room_User_List, params: param, completion: completion)
    }
    
    func getRoomPlaymateCategory(roomId: String, completion: @escaping (LNRoomGameCategoryResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_User_Category, params: [
            "roomId": roomId
        ], completion: completion)
    }
    
    func inviteUserToSeat(roomId: String, uid: String, index: Int, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Mic_Invite_Send, params: [
            "roomId": roomId,
            "userId": uid,
            "micIndex": index,
        ], completion: completion)
    }
    
    func getUserCurRoom(uid: String, completion: @escaping (LNUserCurRoomResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Cur, params: [
            "id": uid
        ], completion: completion)
    }
}

// MARK: 入口
extension LNHttpManager {
    func getHallEntrance(completion: @escaping (LNRoomEntranceResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Entrance, completion: completion)
    }
    
    func getRandomRoom(completion: @escaping (LNRandomRoomResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Random, params: [
            "roomTypes": LNRoomType.availableType.map({ $0.rawValue }),
        ], completion: completion)
    }
    
    func searchRoom(keyword: String, size: Int,
                    next: String, completion: @escaping (LNSearchRoomResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Search, params: [
            "keyword": keyword,
            "roomTypes": LNRoomType.availableType.map({ $0.rawValue }),
            "page": [
                "size": size,
                "next": next
            ]
        ], completion: completion)
    }
    
    func getRoomList(size: Int, next: String, completion: @escaping (LNSearchRoomResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Room_List, params: [
            "roomTypes": LNRoomType.availableType.map({ $0.rawValue }),
            "page": [
                "size": size,
                "next": next
            ]
        ], completion: completion)
    }
    
    func reportRoomUnavailable(id: String, code: String, reason: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Room_Unavailable, params: [
            "id": id,
            "supplier": 1,
            "errorCode": code,
            "errorReson": reason
        ], completion: completion)
    }
}
