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

let WeaponStockItem = new UIMenuItem("Bestand", "Waffenbestand anzeigen");
WeaponStockItem.BackColor = new Color(94, 94, 94);
WeaponStockItem.HighlightedBackColor = new Color(105, 105, 105);

export default function weaponList(globalData: IGlobalData) {

    var weaponMenu: NativeUI.Menu;

    var primaries;
    var secondaries;
    var melees;
    var specialsWep;
    var time;
    var armor;
    
    //Weapon Menu

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

            globalData.InMenu = true;
            var primary = "";
            var secondary = "";
            var melee = "";
            var specialWep = "";
            var timer = "";
            primaries = primariesArr;
            secondaries = secondariesArr;
            melees = meleesArr;
            specialsWep = specialsArr;
            time = JSON.parse(jsonTime);
            armor = armorArr;

            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(0, screenRes.y / 3), 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(new UIMenuListItem("Schutzweste", "", new ItemsCollection(armor)));
            weaponMenu.AddItem(saveItem);
            weaponMenu.AddItem(WeaponStockItem);
            weaponMenu.BindMenuToItem(getWeaponStockMenu(weaponMenu), WeaponStockItem);
            weaponMenu.AddItem(dealItem);
            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;
                    case "Schutzweste":
                        if (item.SelectedItem.DisplayText === "Keine") {
                            armor = "";
                        } else {
                            armor = String(item.SelectedItem.DisplayText);
                        }
                        break;
                }
            });

            function getWeaponStockMenu(parentMenu: NativeUI.Menu, ): NativeUI.Menu {
                var weaponStockMenu = new NativeUI.Menu("Bestand","", new Point(0, screenRes.y / 3), null, null);

                var amountInfos: { WeaponModel: string, Amount: number }[] = JSON.parse(jsonAmountInfos);
                amountInfos.forEach(amount => {
                    var amountItem = new NativeUI.UIMenuItem(amount.WeaponModel);
                    amountItem.SetRightLabel(amount.Amount.toString());
                    weaponStockMenu.AddItem(amountItem);
                });
                
                weaponStockMenu.Visible = false;
                return weaponStockMenu;
            }


            weaponMenu.ItemSelect.on((item) => {
                if (item.Text === "Waffen nehmen") {
                    mp.events.callRemote("saveWeaponSelection", primary, secondary, melee, specialWep, armor);
                    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;
            });
        }
    });
}
