import NullNetwork from './NullNetwork';

/** Network service that targets a local dev backend. */
export default class LocalNetwork extends NullNetwork
{
    public override name = 'LocalNetwork';

    constructor()
    {
        super();
        const params = new URLSearchParams(window.location.search);

        this._token = params.get('play_token') ?? '';
        this._serverUrl = params.get('server_url') ?? '';
    }

    public override get baseAssetsUrl(): string
    {
        return './';
    }

    public override get baseImgsUrl(): string
    {
        return './imgs/';
    }

    public override get baseUrl(): string
    {
        const sharedUserApiUrl = window.localStorage.getItem('shared_user_api_url');

        return sharedUserApiUrl ? sharedUserApiUrl : __OPTIONS__.api;
    }

    public override async init(): Promise<unknown>
    {
        const data = { command: 'init' };
        const url = this.baseUrl + this.pathInit;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async heartBeat(): Promise<unknown>
    {
        const data = {
            command: 'init',
            options: { balance: true },
        };
        const url = this.baseUrl + this.pathInit;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async bet(bet: number, bonus = false): Promise<unknown>
    {
        const data = {
            command: 'spin',
            options: { bonus, bet },
        };
        const url = this.baseUrl + this.pathBet;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async freespin(): Promise<unknown>
    {
        const data = { command: 'freespin' };
        const url = this.baseUrl + this.pathBet;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async respin(): Promise<unknown>
    {
        const data = { command: 'respin' };
        const url = this.baseUrl + this.pathBet;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async buyFreespins(bet: number): Promise<unknown>
    {
        const data = {
            command: 'spin',
            options: {
                bet,
                purchased_feature: 'freespin_buy',
            },
        };
        const url = this.baseUrl + this.pathBet;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async buyBonus(bet: number): Promise<unknown>
    {
        const data = {
            command: 'spin',
            options: {
                bet,
                purchased_feature: 'bonus_buy',
            },
        };
        const url = this.baseUrl + this.pathBet;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async wildSpin(bet: number): Promise<unknown>
    {
        const data = {
            command: 'spin',
            options: {
                bet,
                purchased_feature: 'bonus_chance',
            },
        };
        const url = this.baseUrl + this.pathBet;

        return this.createRequest(url, this.createRequestInit(data));
    }

    public override async createRequest(url: string, requestInit: RequestInit): Promise<unknown>
    {
        try
        {
            const response = await fetch(url, requestInit);

            return await response.json();
        }
        catch (error)
        {
            return error;
        }
    }
}
