import * as NativeUI from '../libs/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;

let screenRes = mp.game.graphics.getScreenResolution(0, 0);

let sendItem = new UIMenuItem("Starten", "Route starten");
sendItem.BackColor = new Color(13, 71, 161);
sendItem.HighlightedBackColor = new Color(25, 118, 210);

let cancelItem = new UIMenuItem("Abbrechen", "");
cancelItem.BackColor = new Color(213, 0, 0);
cancelItem.HighlightedBackColor = new Color(229, 57, 53);


export default function busRouteList(globalData: GlobalData) {

    var routeMenu: NativeUI.Menu;
    var routeTexts;
    var routeText;

    mp.events.add('showBusRouteMenu', (listRoutes) => {
        if (!globalData.InMenu) {

            globalData.InMenu = true;
            routeTexts = JSON.parse(listRoutes);

            routeMenu = new Menu("Bus Job", "", new Point(50, 50), null, null);
            routeMenu.AddItem(new UIMenuListItem("Route", "", new ItemsCollection(routeTexts)));

            routeMenu.AddItem(sendItem);
            routeMenu.AddItem(cancelItem);
            routeMenu.Visible = true;
            routeText = "Kurz 1";

            routeMenu.ListChange.on((item, index) => {
                switch (item.Text) {
                    case "Route":
                        routeText = String(item.SelectedItem.DisplayText);
                        break;
                }
            });

            routeMenu.ItemSelect.on((item) => {
                if (item.Text === "Starten") {
                    mp.events.callRemote("startBusRoute", routeText);
                    routeMenu.Close();
                    globalData.InMenu = false;
                } else if (item.Text === "Abbrechen") {
                    routeMenu.Close();
                    globalData.InMenu = false;
                }
            });

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