import * as NativeUI from 'NativeUI';

const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const UIMenuCheckboxItem = NativeUI.UIMenuCheckboxItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;

import InputHelper from '../inputhelper';

export default function (globalData: GlobalData) {

    var keyBound = false;
    var menu: NativeUI.Menu = null;
    var joblist: string[] = [];

    mp.events.add("SERVER:CityHall_ShowHelpText", (joblista) => {
        mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um das Stadthallen-Menü zu öffnen');
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);
        
        joblist = JSON.parse(joblista);

        if (!keyBound) {
            keyBound = true;
            mp.keys.bind(0x45, false, keyPressHandler);
        }
    });

    mp.events.add("SERVER:CityHall_ClearHelpText", () => {
        mp.game.ui.clearHelp(false);
        if (menu) menu.Close();
        if (keyBound) {
            mp.keys.unbind(0x45, false, keyPressHandler);
            keyBound = false;
        }
    });

    function keyPressHandler(): void {
        if (globalData.InChat || globalData.InMenu) return;
        menu = new Menu("Stadthalle", "", new Point(50, 50), null, null);

        globalData.InMenu = true;
        mp.gui.chat.show(false);

        var jobItem = new UIMenuItem("Jobs", "Jobcenter");
        menu.AddItem(jobItem);

        var jobMenu = new Menu("Jobcenter", "Suche dir einen neuen Job aus", new Point(50, 50), null, null);

        for (var i = 0; i < joblist.length; i++) {
            var job = joblist[i];
            jobMenu.AddItem(new UIMenuItem(job));
        }

        var cancelJobItem = new UIMenuItem("Job kuendigen");
        cancelJobItem.BackColor = new Color(213, 0, 0);
        cancelJobItem.HighlightedBackColor = new Color(229, 57, 53);
        jobMenu.AddItem(cancelJobItem);

        menu.BindMenuToItem(jobMenu, jobItem);

        jobMenu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => {
            if (item === cancelJobItem) { // Job kündigen
                mp.events.callRemote("CLIENT:JobCenter_CancelJob");
            } else {
                mp.events.callRemote("CLIENT:JobCenter_AcceptJob", index + 1);
            }

            menu.Close();
        });

        jobMenu.Visible = false;

        var groupCreateItem = new UIMenuItem("Gruppe erstellen", "Erstelle eine neue Gruppe");
        groupCreateItem.SetRightLabel("~g~50.000$");
        menu.AddItem(groupCreateItem);

        var cancelItem = new UIMenuItem("Schließen");
        cancelItem.BackColor = new Color(213, 0, 0);
        cancelItem.HighlightedBackColor = new Color(229, 57, 53);
        menu.AddItem(cancelItem);

        menu.Open();

        menu.ItemSelect.on((item: NativeUI.UIMenuItem, index) => {
            if (item === groupCreateItem) { // Gruppe
                var input = new InputHelper("Wie soll die Gruppe heißen?", globalData);
                input.show();
                input.getValue((name: string) => {
                    mp.events.callRemote("CLIENT:CityHall_CreateGroup", name);
                    menu.Close();
                });
            }
        });

        menu.MenuClose.on(() => {
            globalData.InMenu = false;
            mp.gui.chat.show(true);
        });
    }
}