import { Ref, ref } from "vue";
import { BJ_Casino_Bot } from "./BJ_Casino_Bot";
import SlotBase from "./Common/Game/Base/SlotBase";
import { Tools } from "./Tools";

export class BJ_Casino_Bot_Slot {
    //#region public

    public BetDefault: Ref<string> = ref("");

    public SpinDelay: Ref<number> = ref(1);

    public Slot: SlotBase = null!;

    public GameInData: any = null!;

    public BetGroup: number[] = [];

    public NowBetIndex: number = null!;

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

    public IsRun: boolean = false;

    //#endregion

    //#region private

    private _bj_Casino_Bot: BJ_Casino_Bot;

    //#endregion

    //#region get set

    public get NowBet(): number { return this.BetGroup[this.NowBetIndex]; }

    //#endregion

    //#region Lifecycle

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

    public async onLoad(): Promise<void> {
        //
    }

    public async GameIn(gameInData: any): Promise<void> {
        this.GameInData = gameInData;
        let Data: any = gameInData.Data;
        this.BetGroup = Data.br;
        this.NowBetIndex = Data.db;
        this.BetDefault.value = this.BetGroup[Data.db].toString();
    }

    //#endregion

    //#region Custom

    public async Spin(): Promise<void> {
        this.IsRun = true;
        await this.Slot.Spin();
        await Tools.Sleep(this.SpinDelay.value * 1000);
        if (this.IsSpin.value && this._bj_Casino_Bot.LobbyScript.IsSlotIn.value) {
            this.Spin();
        } else {
            this.IsRun = false;
        }
    }

    public SelectBet(data: any): void {
        this.NowBetIndex = data;
        this._bj_Casino_Bot.AddLog(`改變Bet: ${this.NowBet}`);
    }

    //#endregion

    //#region Onclick

    public async OnclickSpin(): Promise<void> {
        if (this.IsRun) {
            return;
        }
        let id: string = this._bj_Casino_Bot.LobbyScript.InputSlot.value;
        let slot: any;
        try {
            slot = await import(/* @vite-ignore */`./Common/Game/Slot${id}`);
        } catch (error) {
            // this._bj_Casino_Bot.AddLog(`Slot${id} Spin error: ${error}`);
        }
        if (!slot) {
            slot = await import(/* @vite-ignore */`./Common/Game/Base/SlotBase`);
        }
        const slot_default: typeof SlotBase = slot.default;
        this.Slot = new slot_default(this._bj_Casino_Bot, this);
        this.IsSpin.value = true;
        this.Spin();
    }

    public async OnclickStop(): Promise<void> {
        this.IsSpin.value = false;
    }

    //#endregion
}
