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);

var routeMenu: NativeUI.Menu;
var busRouteSelectColShape = mp.colshapes.newCircle(-535, -2144, 2.5);

export default function busRouteList(globalData: IGlobalData) {
    var skillSelect;
    var routeSelect;
    var skill = 0;
    var routeSelected = false;

    mp.events.add(RageEnums.EventKey.PLAYER_EXIT_COLSHAPE, (colshape) => {
        if (colshape == busRouteSelectColShape) {
            if (routeMenu && routeMenu.Visible) {
                routeMenu.Close();
            }
        }
    });

    mp.events.add('SERVER:StartBusJob', (jobskill) => {
        skill = jobskill;
        if (!globalData.InMenu) {
            globalData.InMenu = true;

            skillSelect = ["Anfänger", "Fortgeschrittener", "Profi"];

            routeMenu = new Menu("Bus Job", "", new Point(0, screenRes.y / 3), null, null);
            routeMenu.AddItem(new UIMenuListItem("Route wählen", "", new ItemsCollection(skillSelect)));
            routeSelect = "Anfänger";
            routeMenu.AddItem(sendItem);
            routeMenu.AddItem(cancelItem);
            routeMenu.Visible = true;

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

            routeMenu.ItemSelect.on((item) => {
                if (item.Text === "Starten") {

                    if (routeSelect == skillSelect[1] && skill < 300) {
                        mp.game.audio.playSoundFrontend(1, "Hack_Failed", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", true);
                    }
                    else if (routeSelect == skillSelect[2] && skill < 800)
                    {
                        mp.game.audio.playSoundFrontend(1, "Hack_Failed", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", true);
                    }
                    else
                    {
                        mp.events.callRemote("CLIENT:StartBusRoute", routeSelect);
                        routeSelected = true;
                        routeMenu.Close();
                        globalData.InMenu = false;
                    }

                   
                } else if (item.Text === "Abbrechen") {
                    routeMenu.Close();
                }
            });

            routeMenu.MenuClose.on(() => {
                if (!routeSelected) {
                    mp.events.callRemote("CLIENT:Job_StopJob");
                }
                routeSelected = false;

                globalData.InMenu = false;
            });
        }
    });

    mp.events.add("MenuSelect:Error", () => {
        mp.game.audio.playSoundFrontend(1, "Hack_Failed", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", true);
    });
}