import { IUi, IBrowser } from "../../game";
import { Menu } from "../../libs/NativeUI/index";
import { GlobalData } from "../..";

export default class RageUi implements IUi {
    setHelpText(text: string): void {
        mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName(text);
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);
    }
    clearHelpText(): void {
        mp.game.ui.clearHelp(true);
    }
    private _inMenu: boolean = false;
    private _inChat: boolean = false;
    private _activeMenu: Menu = null;

    get inChat() {
        return this._inChat || GlobalData.InChat;
    }

    set inChat(value: boolean) {
        this._inChat = value;
        GlobalData.InChat = value;
    }

    get activeMenu(): Menu {
        return this._activeMenu;
    }

    set activeMenu(value: Menu) {
        if (this.activeMenu && this.activeMenu.Visible) {
            this.activeMenu.Close(true);
        }

        this._activeMenu = value;

        if (this.activeMenu) {
            this.activeMenu.Open();
        }

        this.inMenu = this.activeMenu != null;
    }

    get inMenu() {
        return this._inMenu || GlobalData.InMenu;
    }

    set inMenu(value: boolean) {
        this._inMenu = value;
        GlobalData.InMenu = value;
        this.toggleChat(!value);
    }

    toggleChat(toggle: boolean) {
        mp.gui.chat.show(toggle);
    }

    openBrower(path: string): IBrowser {
        return new RageBrowser(path);
    }

    sendChatMessage(message: string) {
        mp.gui.chat.push(message);
    }

    setCursor(freeze: boolean, show: boolean): void {
        mp.gui.cursor.show(freeze, show);
    }
}

export class RageBrowser implements IBrowser {
    private rageBrowser: BrowserMp;

    constructor(path: string) {
        this.rageBrowser = mp.browsers.new(path);
    }

    close(): void {
        if (this.rageBrowser) {
            this.rageBrowser.destroy();
            this.rageBrowser = null;
        }
    }

    executeJs(script: string) {
        if (this.rageBrowser) {
            this.rageBrowser.execute(script);
        }
    }
}