/**
 * Filename: CheatControls.ts
 * Author: Stevan Bosanac
 * Created Date: 2024-09-17
 *
 * Description:
 * The `CheatControls` class in TypeScript adds cheat controls to a pane and allows for testing cheats
 * by making POST requests to a specified URL.
 *
 * Revision History:
 * 2024-09-18: Initial creation
 *
 */

import { Pane } from 'tweakpane';

/*
 * The `CheatControls` class in TypeScript adds cheat controls to a pane and allows for testing cheats
 * by making POST requests to a specified URL.
 */
export class CheatControls
{
    static controlName = 'CheatControls';
    pane: Pane;

    /**
     * The constructor function initializes the pane and locator properties with the provided values.
     * @param {Pane} pane - The `pane` parameter is likely referring to a graphical user interface (GUI)
     * component that represents a container or area where other GUI elements can be placed or displayed.
     * It could be a window, panel, or any other type of container used in the user interface.
     * @param {any} locator - The `locator` parameter in the constructor is typically used to specify a
     * way to locate elements within the `Pane` object. This can be done using various methods such as
     * XPath, CSS selectors, or other locator strategies depending on the testing framework or library
     * being used. The `locator` parameter helps in
     */
    constructor(pane: Pane)
    {
        this.pane = pane;
    }

    /** The `initialize` function calls the `addCheats` method during initialization. */
    initialize()
    {
        this.addCheats();
    }

    /**
     * The function `addCheats` adds cheat buttons to a folder based on data from a cheats sheet.
     * @returns If the `cheats` property is not an array or if it is not defined, then nothing will be
     * returned. The function will simply exit without adding any cheats to the folder.
     */
    addCheats()
    {
        // cheats not exists in Locator anymore

        //     if (!Array.isArray(Locator.cheats?.sheet)) return;
        //     const folder = (this.pane as any).addFolder({ title: 'Cheats' });

        //    Locator.cheats.sheet.forEach((cheat:any) =>
        //     {
        //         const changeUserBtn = folder.addButton({ title: cheat.name });

        //         changeUserBtn.on('click', this.addTestCheat(cheat.data));
        //     });
    }

    /**
     * The function `addTestCheat` sends a POST request to update game slots with provided data in a
     * specific URL.
     * @param data - The `addTestCheat` function you provided seems to be a function that sends a POST
     * request to a specific URL with some data. The `data` parameter is expected to be an object
     * containing additional information to be included in the request body.
     * @returns The `addTestCheat` function is returning an asynchronous function that makes a POST
     * request to a specific URL with a JSON body containing some data. The function logs the response
     * from the POST request to the console.
     */
    addTestCheat(data: any)
    {
        return async () =>
        {
            try
            {
                const baseUrl = 'https://games-tb-6-epic-bubble-x.k8s.popiplay.dev/'; // window["__OPTIONS__"].games_page_url; // "https://games-tb-6-epic-bubble-x.k8s.popiplay.dev/"
                const url = `${baseUrl}dev_mode/${(window as any).__OPTIONS__.play_token}-EUR/games/Bubbles/update_slots/screen`;
                const body = {
                    command: 'update',
                    player_id: `${(window as any).__OPTIONS__.play_token}-EUR`,
                    identifier: 'Bubbles',
                    subject: 'screen',
                    ...data,
                };
                const postResponse = await fetch(url, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(body),
                    // mode: "no-cors",
                });

                console.log('postResponse:', postResponse);
                // const postResponseObject = await postResponse.json();
                // console.log("postResponseObject:", postResponseObject);
            }
            catch (error)
            {
                console.log('error:', error);
            }
        };
    }
}
