import CSMessage from "../../../Base/CSMessage";
import { BJ_Casino_Bot } from "../../../BJ_Casino_Bot";
import { BJ_Casino_Bot_Slot } from "../../../BJ_Casino_Bot_Slot";
import { INetResponse } from "../../../Engine/CatanEngine/NetManagerV2/Core/INetResponse";
import { CommonSlotFgSpinRequest, CommonSlotSpinRequest } from "../Request/CommonSlotRequest";

export default class SlotBase {
    //#region public

    public get ID(): number { return this._bj_Casino_Bot.LobbyScript.Slot; }

    //#endregion

    //#region protected

    protected _bj_Casino_Bot: BJ_Casino_Bot;

    protected _bj_Slot: BJ_Casino_Bot_Slot;

    //#endregion

    //#region Lifecycle

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

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

    //#endregion

    //#region Custom

    public async Spin(): Promise<void> {
        let gameRunData: JSON = null;
        let req: CommonSlotSpinRequest = new CommonSlotSpinRequest(this.ID, this._bj_Slot.NowBet);
        await req.SendAsync();
        let resp: INetResponse<JSON> = req.Result;
        if (resp.IsValid) {
            gameRunData = resp.Data;
        } else {
            CSMessage.NetError(resp.Method, resp.Status);
        }
        let money: number = +gameRunData["money"];
        let winMoney: number = 0;
        let winMoneyLog: string = "";
        let freeLog: string = "";
        let resources: any[] = gameRunData["get"];
        let free: any[] = gameRunData["free"];
        if (resources) {
            for (let i: number = 0; i < resources.length; i++) {
                const resource: any[] = resources[i];
                if (resource[0] === 1) {
                    winMoney = resource[1];
                    winMoneyLog = `, winMoney: ${winMoney}`;
                    break;
                }
            }
        }
        if (free) {
            let freeCount: number = free[1];
            await this.FreeSpin(freeCount);
            freeLog = `, hasFree: True, free: ${freeCount}`;
        }
        this._bj_Casino_Bot.UserData.Money = money;
        this._bj_Casino_Bot.SetUI();
        this._bj_Casino_Bot.AddLog(`Slot${this.ID} Spin Bet: ${this._bj_Slot.NowBet}, Money: ${money}${winMoneyLog}${freeLog}`);
    }

    public async FreeSpin(freeCount: number): Promise<void> {
        for (let i: number = 0; i < freeCount; i++) {
            let gameRunData: JSON = null;
            let req: CommonSlotFgSpinRequest = new CommonSlotFgSpinRequest(this.ID);
            await req.SendAsync();
            let resp: INetResponse<JSON> = req.Result;
            if (resp.IsValid) {
                gameRunData = resp.Data;
            } else {
                CSMessage.NetError(resp.Method, resp.Status);
            }
            this._bj_Casino_Bot.AddLog(`Slot${this.ID} FreeSpin MaxCount: ${freeCount}, Count: ${i + 1}`);
        }
    }

    //#endregion
}
