import { Ref, ref } from "vue";
import CSMessage from "./Base/CSMessage";
import { BJ_Casino_Bot } from "./BJ_Casino_Bot";
import { SeatListRequest } from "./Common/ChooseSeat/ChooseSeatRequest";
import { SlotInRequest, SlotOutRequest } from "./Common/Subgame/GameRequest";
import { INetResponse } from "./Engine/CatanEngine/NetManagerV2/Core/INetResponse";
import { Tools } from "./Tools";

export class BJ_Casino_Bot_Lobby {
    //#region public

    public LobbyCongig: any[] = [2, [
        "不選擇資源路徑",
        "體驗聽",
        "一般聽",
        "高手聽",
        "至尊聽",
    ]];

    public LobbyShow: Ref<boolean> = ref(false);
    public Lobby: Ref<number> = ref(0);
    public InputSlot: Ref<string> = ref("");
    public Slot: number;
    public Table: Ref<number> = ref(0);
    public LobbyDefault: Ref<string> = ref("");

    public IsSlotIn: Ref<boolean> = ref(false);

    //#endregion

    //#region private

    private _bj_Casino_Bot: BJ_Casino_Bot;

    //#endregion

    //#region Lifecycle

    /**
     *
     */
    constructor(bj_Casino_Bot: BJ_Casino_Bot) {
        this._bj_Casino_Bot = bj_Casino_Bot;
        this.onLoad();
    }

    public async onLoad(): Promise<void> {
        this.Lobby.value = this.LobbyCongig[0];
        this.LobbyDefault.value = this.LobbyCongig[1][this.Lobby.value];
    }

    //#endregion

    //#region Custom

    public GetLobby(): { key: string; value: string; label: string; }[] {
        const patchs: any[] = this.LobbyCongig[1];
        let options: any[] = [];
        for (let i: number = 0; i < patchs.length; i++) {
            const patch: any = patchs[i];
            options.push({
                value: i,
                label: patch,
            });
        }
        return options;
    }

    public SelectLobby(data: any): void {
        // data为el-option上:value绑定的对象
        this.Lobby.value = data;
    }

    //#endregion

    //#region Onclick

    public async OnclickSlotIn(hall: number, slot: number): Promise<void> {
        if (this.IsSlotIn.value) {
            return;
        }
        this.IsSlotIn.value = true;
        let table: number = 1;
        let req: SeatListRequest = new SeatListRequest(hall, slot);
        await req.SendAsync(true);
        let resp: INetResponse<JSON> = req.Result;
        if (!resp.IsValid) {
            CSMessage.NetError(resp.Method, resp.Status, "機台列表獲取失敗");
            return;
        }
        let data: any = resp.Data;
        for (let i: number = 0; i < data.length; i++) {
            const element: any = data[i];
            if (element[0].length > 0) {
                let aid: string = element[0][0];
                if (aid === this._bj_Casino_Bot.UserData.AID) {
                    table = i + 1;
                    break;
                } else {
                    table = i + 1;
                    break;
                }
            }
        }
        let resp_slotIn: any = await this._slotIn(hall, slot, table);
        this.Slot = slot;
        this.Table.value = table;
        this._bj_Casino_Bot.SlotScript.GameIn(resp_slotIn);
        this._bj_Casino_Bot.SlotShow.value = true;
    }

    public async OnclickSlotOut(): Promise<void> {
        this._bj_Casino_Bot.SlotScript.IsSpin.value = false;
        if (!this.IsSlotIn.value) {
            return;
        }
        this.IsSlotIn.value = false;
        while (this._bj_Casino_Bot.SlotScript.IsRun) {
            await Tools.Sleep(0.5);
        }
        await this._slotOut();
        this.Slot = null!;
        this.Table.value = 0;
        this._bj_Casino_Bot.SlotShow.value = false;
    }

    public OnclickChangeSlot(): void {
        // await this._login();
    }

    //#endregion

    //#region Server Method

    private async _slotIn(hall: number, slot: number, uid: number): Promise<any> {
        let req: SlotInRequest = new SlotInRequest(slot, hall, uid);
        await req.SendAsync(true);
        let resp: INetResponse<JSON> = req.Result;
        if (resp.IsValid) {
            this._bj_Casino_Bot.AddLog(`進機台成功 slot: ${slot}, hall: ${hall}, table: ${uid}`);
            return resp;
        } else {
            switch (resp.Status) {
                case 11:
                    // this._messageInfo(1003);
                    break;
                case 101:
                    // CSMessage.CreateYesMsg(CSSettingsV3.prototype.CommonString(16));
                    break;
                default:
                    break;
            }
            CSMessage.NetError(resp.Method, resp.Status, "進遊戲失敗");
        }
    }

    private async _slotOut(): Promise<any> {
        let req: SlotOutRequest = new SlotOutRequest();
        await req.SendAsync(true);
        let resp: INetResponse<JSON> = <INetResponse<JSON>><unknown>req.Result;
        if (resp.IsValid) {
            this._bj_Casino_Bot.AddLog(`離開機台成功`);
        }
    }

    //#endregion
}
