/**
 * @overview Life of German Reallife - Player DutyClothes dutyclothes.js
 * @author VegaZ
 * @copyright (c) 2008 - 2018 Life of German
 */

import * as NativeUI from '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("Speichern", "Speichert deine Dienstkleidung");
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 dutyCloth(globalData: GlobalData) {

    var dutyMenu;

    var hats;
    var tops;
    var legs;
    var shoes;

    var hat = -1;
    var top;
    var leg;
    var shoe;

    //Cloth Menu

    mp.events.add('showDutyClothMenu', (hatsArr, topsArr, legsArr, shoesArr) => {
        if (!globalData.InMenu) {

            globalData.InMenu = true;

            mp.gui.chat.activate(false);
            hats = hatsArr;
            tops = topsArr;
            legs = legsArr;
            shoes = shoesArr;

            dutyMenu = new Menu("Dienstkleidung", "Stelle deine Dienstkleidung zusammen", new Point(0, screenRes.y / 2), null, null);
                                                                                    
            dutyMenu.AddItem(new UIMenuListItem("Hut", "", new ItemsCollection(hats)));
            dutyMenu.AddItem(new UIMenuListItem("Top", "", new ItemsCollection(tops)));
            dutyMenu.AddItem(new UIMenuListItem("Hose", "", new ItemsCollection(legs)));
            dutyMenu.AddItem(new UIMenuListItem("Schuhe", "", new ItemsCollection(shoes)));
            dutyMenu.AddItem(saveItem);
            dutyMenu.AddItem(cancelItem);
            dutyMenu.Visible = true;

            dutyMenu.ListChange.on((item, index) => {
                switch (item.Text) {
                    case "Hut":
                        if (item.SelectedItem.DisplayText === "Keinen") {
                            hat = -1;
                            mp.events.callRemote("updateDutyProp", 0, -1);
                        } else {
                            hat = parseInt(item.SelectedItem.DisplayText);
                            mp.events.callRemote("updateDutyProp", 0, hat);
                        }
                        break;
                    case "Top":
                        top = parseInt(item.SelectedItem.DisplayText);
                        mp.events.callRemote("updateDutyCloth", 11, top);
                        break;
                    case "Hose":
                        leg = parseInt(item.SelectedItem.DisplayText);
                        mp.events.callRemote("updateDutyCloth", 4, leg);
                        break;
                    case "Schuhe":
                        shoe = parseInt(item.SelectedItem.DisplayText);
                        mp.events.callRemote("updateDutyCloth", 6, shoe);
                        break;
                }
            });

            dutyMenu.ItemSelect.on((item) => {
                if (item.Text === "Speichern") {
                    var slotType = [1, 0, 0, 0];
                    var slotId = [0, 11, 4, 6];
                    var clothId = [hat, top, leg, shoe];
                    mp.events.callRemote("saveCharacterCloth", JSON.stringify(slotType), JSON.stringify(slotId), JSON.stringify(clothId));
                    dutyMenu.Visible = false;
                    mp.gui.chat.activate(true);
                } else if (item.Text === "Abbrechen") {
                    dutyMenu.Visible = false;
                    mp.gui.chat.activate(true);
                    mp.events.callRemote("defaultCharacterCloth");
                    globalData.InMenu = false;
                }
            });


            dutyMenu.MenuClose.on(() => {
                dutyMenu.Visible = false;
                mp.gui.chat.activate(true);
                mp.events.callRemote("defaultCharacterCloth");
                globalData.InMenu = false;
            });
        }
    });
}