import * as NativeUI from 'NativeUI';

const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;

export default function vehicleMenu(globalData: GlobalData) {
    let menuBrowser: BrowserMp = null;

    mp.events.add('ToggleVehicleMenu', () => {
        if (menuBrowser !== null) {
            menuBrowser.destroy();
            menuBrowser = null;
            mp.gui.chat.show(true);
            mp.gui.cursor.show(false, false);
            mp.game.graphics.stopScreenEffect("FocusIn");
            mp.game.graphics.startScreenEffect("FocusOut", 0, false);
            return;
        }

        mp.game.graphics.stopScreenEffect("FocusOut");
        mp.game.graphics.startScreenEffect("FocusIn", 0, false);
        menuBrowser = mp.browsers.new("package://assets/html/vehiclemenu/index.html");
        mp.gui.chat.show(false);
        mp.gui.cursor.show(true, true);
    });

    mp.events.add('doAction', (action) => {
        mp.events.call('ToggleVehicleMenu');

        switch (action) {
            case 8: // Motor
                mp.events.callRemote("VehicleMenu_ToggleEngine");
                break;
            case 5: // Auf -/ Abschließen
                mp.events.callRemote("VehicleMenu_LockCar");
                break;
            case 3: // Türen
                showDoorsMenu();
                break;
            case 7: // Parken
                mp.events.callRemote("CLIENT:VehicleMenu_ParkCar");
                break;
        }
    });

    function showDoorsMenu() {
        globalData.InMenu = true;

        var doorMenu = new Menu("Türen", "Türen verwalten", new Point(50, 50), null, null);

        doorMenu.AddItem(new UIMenuListItem("Tür", "Welche Tür darf's sein?", new ItemsCollection([
            " Fahrertür",
            " Beifahrertür",
            "Hinten links",
            "Hinten rechts",
            "Motorhaube",
            "Kofferraum"
        ])));

        doorMenu.AddItem(new UIMenuItem("Alle öffnen", "Öffnet alle Türen"));
        doorMenu.AddItem(new UIMenuItem("Alle schließen", "Schließt alle Türen"));

        doorMenu.ItemSelect.on((item, index) => {
            if (index === 0) {
                var doorId = -1;
                switch (item.SelectedItem.DisplayText) {
                    case " Fahrertür":
                        doorId = 0;
                        break;
                    case " Beifahrertür":
                        doorId = 1;
                        break;
                    case "Hinten links":
                        doorId = 2;
                        break;
                    case "Hinten rechts":
                        doorId = 3;
                        break;
                    case "Motorhaube":
                        doorId = 4;
                        break;
                    case "Kofferraum":
                        doorId = 5;
                        break;
                }
                if (doorId !== -1) {
                    mp.events.callRemote("VehicleMenu_ToggleSingleDoor", doorId);
                }
            } else if (index === 1) {
                mp.events.callRemote("VehicleMenu_OpenAllDoors");
            } else if (index === 2) {
                mp.events.callRemote("VehicleMenu_CloseAllDoors");
            }
        });

        doorMenu.MenuClose.on(() => {
            globalData.InMenu = false;
        });

        doorMenu.Open();
    }
}