import * as NativeUI from '../../libs/NativeUI';

const Menu = NativeUI.Menu;
const MenuItem = NativeUI.UIMenuItem;
const MenuListItem = NativeUI.UIMenuListItem;
const MenuCheckboxItem = NativeUI.UIMenuCheckboxItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;

export default function ammunation(globalData: IGlobalData) {

    var screenRes = mp.game.graphics.getScreenResolution(0, 0);
    var weapons: Weapon[];

    mp.events.add("AmmunationShop:LoadNativeUI", (weaponList: string) => {
        
        weapons = JSON.parse(weaponList);
        var menu = getAmmunationMenu();

        //Schaut nach ob mindestens eine Waffe in weapons existiert welche die CategoryId beinhaltet
        for (var c = 1; c < 10; c++) {
            switch (c) {
                case 1: { //Meelee
                    
                    break;
                }

                case 2: { //Pistolen
                    if (weapons.find(w => w.CategoryId == c)) {
                        var pistolItem = new MenuItem("Pistolen", "Das kleine Kaliber",);
                        menu.AddItem(pistolItem);
                        menu.BindMenuToItem(getWeaponsFromCategory(menu, c, "Pistolen"),pistolItem);
                    }
                    break;
                }
                /*case 9: { //Sonstiges (Armor / Fallschirm)
                    var otherItem = new MenuItem("Sonstiges", "Schutzwesten, Fallschirme...",);
                    menu.AddItem(otherItem);
                    menu.BindMenuToItem(getWeaponsFromCategory(menu, c, "Sonstiges"), otherItem);
                    break;
                }*/
            }          
        }       
        menu.Open();
        mp.gui.chat.activate(false);
        globalData.InMenu = true;

        menu.MenuClose.on(() => {
            globalData.InMenu = false;
            mp.gui.chat.show(true);
        })
    });

    function getAmmunationMenu(): NativeUI.Menu {
        return new Menu("", "AMMU NATION - Waffenladen", new Point(0, screenRes.y / 3), "shopui_title_gunclub", "shopui_title_gunclub");
    }

    function getWeaponsFromCategory(parentMenu: NativeUI.Menu, category: number, categoryName: string): NativeUI.Menu {
        var categoryMenu = new Menu("", categoryName, new Point(0, screenRes.y / 3), "shopui_title_gunclub", "shopui_title_gunclub");

        categoryMenu.Visible = false;

        var cWeapons: Weapon[] = weapons.filter(w => w.CategoryId == category);

        if (category < 9) { //Weapons aus Datenbank
            cWeapons.forEach(weapon => {
                var weaponItem: NativeUI.UIMenuItem = new MenuItem(weapon.WeaponModel, "Du erhältst " + weapon.Ammo + " Munition", [weapon.WeaponModel, weapon.Ammo, weapon.Price]);
                categoryMenu.AddItem(weaponItem);
                weaponItem.SetRightLabel("~g~$" + weapon.Price.toString());
            })            
        } else { //Sonstiges = Schutzwesten / Fallschirme
            var armorItem = new MenuItem("Schutzweste", "Sch\u00fctzt dich","armor",);
            categoryMenu.AddItem(armorItem);
            armorItem.SetRightLabel("~g~$3000");

            var parachuteItem = new MenuItem("Fallschirm", "Flieg damit wohin du willst");
            categoryMenu.AddItem(parachuteItem);
            parachuteItem.SetRightLabel("~g~$500");           
        }

        categoryMenu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => {
            mp.events.callRemote("CLIENT:Ammunation_BuyWeapon", item.Data[0], item.Data[1], item.Data[2]); //weaponmodel / ammo / price
        });

        return categoryMenu;
    };
};