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 saveItem = new UIMenuItem("Waffen Nehmen", "");
saveItem.BackColor = new Color(13, 71, 161);
saveItem.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 weaponList(globalData: GlobalData) {

    var weaponMenu: NativeUI.Menu;

    var primaries;
    var secondaries;
    var melees;
    var specialsWep;
    var time;

    var primary = "";
    var secondary = "";
    var melee = "";
    var specialWep = "";
    var timer = "";

    //Weapon Menu

    mp.events.add('showWeaponMenu', (primariesArr, secondariesArr, meleesArr, specialsArr, jsonTime) => {
        if (!globalData.InMenu) {

            globalData.InMenu = true;

            primaries = primariesArr;
            secondaries = secondariesArr;
            melees = meleesArr;
            specialsWep = specialsArr;
            time = JSON.parse(jsonTime);

            let dealItem = new UIMenuListItem("Waffentransport", "", new ItemsCollection(time));
            dealItem.BackColor = new Color(204, 170, 0);
            dealItem.HighlightedBackColor = new Color(255, 213, 0);

            weaponMenu = new Menu("Waffenschrank", "Stelle deine Waffenausrüstung zusammen", new Point(50, 50), null, null);

            weaponMenu.AddItem(new UIMenuListItem("Primäre", "", new ItemsCollection(secondaries)));
            weaponMenu.AddItem(new UIMenuListItem("Sekundäre", "", new ItemsCollection(primaries)));
            weaponMenu.AddItem(new UIMenuListItem("Nahkampf", "", new ItemsCollection(melees)));
            weaponMenu.AddItem(new UIMenuListItem("Spezial", "", new ItemsCollection(specialsWep)));
            weaponMenu.AddItem(dealItem);
            weaponMenu.AddItem(saveItem);
            weaponMenu.AddItem(cancelItem);
            weaponMenu.Visible = true;

            weaponMenu.ListChange.on((item, index) => {
                switch (item.Text) {
                    case "Primäre":
                        if (item.SelectedItem.DisplayText === "Keine") {
                            primary = "";
                        } else {
                            primary = String(item.SelectedItem.DisplayText);
                        }
                        break;
                    case "Sekundäre":
                        if (item.SelectedItem.DisplayText === "Keine") {
                            secondary = "";
                        } else {
                            secondary = String(item.SelectedItem.DisplayText);
                        }
                        break;
                    case "Nahkampf":
                        if (item.SelectedItem.DisplayText === "Keine") {
                            melee = "";
                        } else {
                            melee = String(item.SelectedItem.DisplayText);
                        }
                        break;
                    case "Spezial":
                        if (item.SelectedItem.DisplayText === "Keine") {
                            specialWep = "";
                        } else {
                            specialWep = String(item.SelectedItem.DisplayText);
                        }
                        break;
                }
            });

            weaponMenu.ItemSelect.on((item) => {
                if (item.Text === "Waffen Nehmen") {
                    mp.events.callRemote("saveWeaponSelection", primary, secondary, melee, specialWep);
                    weaponMenu.Close();
                    globalData.InMenu = false;
                } else if (item.Text === "Abbrechen") {
                    weaponMenu.Close();
                    globalData.InMenu = false;
                } else if (item.Text === "Waffentransport") {
                    mp.events.callRemote("startWeaponDeal");
                    weaponMenu.Close();
                    globalData.InMenu = false;
                }
            });

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