import * as NativeUI from '../libs/NativeUI';
import InputHelper from '../inputhelper';
import moneyFormat from '../moneyformat';

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;

import { getStreetName, getZoneName } from '../streetnames';

export default function (globalData: GlobalData) {

    var menuClose = false;

    var accountItem = new UIMenuItem("Account", "Account Informationen");

    var serviceItem = new UIMenuItem("Service rufen", "Rufe einen Dienstleister an");

    var factionItem = new UIMenuItem("Fraktion", "Verwalte deine Fraktion");

    var groupItem = new UIMenuItem("Gruppe", "Verwalte deine Gruppe");

    var paycheckItem = new UIMenuItem("Gehaltsscheck", "Schaue dir deinen Verdienst der letzten Stunde an");

    var licenseItem = new UIMenuItem("Lizenzen", "Lizenzen Informationen");


    mp.events.add("SERVER:InteractionMenu_OpenMenu", (accountDataJson: string, faction: string, group: string, factionInvite: boolean, groupInvite: boolean) => {
        var accountData: AccountData = JSON.parse(accountDataJson);

        var menu = getInteractionMenu();

        var serviceMenu = getServicesMenu(menu);
        menu.AddItem(serviceItem);
        menu.BindMenuToItem(serviceMenu, serviceItem);

        menu.AddItem(accountItem);
        menu.BindMenuToItem(getAccountMenu(accountData), accountItem);

        menu.AddItem(licenseItem);
        menu.BindMenuToItem(getLicenseMenu(accountData.licenses, menu), licenseItem);

        if (faction) {
            factionItem.SetRightLabel(faction);
            menu.AddItem(factionItem);
            menu.BindMenuToItem(getFactionMenu(faction, menu), factionItem);
        }

        if (group) {
            groupItem.SetRightLabel(group);
            menu.AddItem(groupItem);
            menu.BindMenuToItem(getGroupMenu(group, menu), groupItem);
        }

        if (accountData.paycheck) {
            var p = accountData.paycheck;
            var paycheckMenu = new Menu("Gehaltsscheck", "Dein Verdienst der letzten Stunde", new Point(50, 50), null, null);
            paycheckMenu.Visible = false;

            var item: NativeUI.UIMenuItem = new UIMenuItem("Lohn");
            item.SetRightLabel("~g~+ $" + moneyFormat(p.wage));
            paycheckMenu.AddItem(item);

            item = new UIMenuItem("Finanzhilfe");
            item.SetRightLabel("~g~+ $" + moneyFormat(p.financialHelp));
            paycheckMenu.AddItem(item);

            item = new UIMenuItem("Fahrzeugsteuer");
            item.SetRightLabel("~r~- $" + moneyFormat(p.vehicleTaxation));
            paycheckMenu.AddItem(item);

            item = new UIMenuItem("Grundsteuer");
            item.SetRightLabel("~r~- $" + moneyFormat(p.propertyTaxation));
            paycheckMenu.AddItem(item);

            item = new UIMenuItem("Mietkosten");
            item.SetRightLabel("~r~- $" + moneyFormat(p.rentalFees));
            paycheckMenu.AddItem(item);

            item = new UIMenuItem("Einkommensteuer");
            item.SetRightLabel((p.financialInterest * 100).toString() + "%");
            paycheckMenu.AddItem(item);

            menu.AddItem(paycheckItem);
            menu.BindMenuToItem(paycheckMenu, paycheckItem);
        }

        var items: Array<string> = new Array<string>();
        if (factionInvite) items.push("Fraktion");
        if (groupInvite) items.push("Gruppe");
        var acceptItem: NativeUI.UIMenuListItem;

        if (items.length !== 0) {
            acceptItem = new UIMenuListItem("Einladung annehmen", "", new ItemsCollection(items));
            menu.AddItem(acceptItem);
        }

        menu.Open();
        globalData.InMenu = true;

        menu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => {
            if (item === acceptItem) {
                mp.events.callRemote("CLIENT:InteractionMenu_AcceptInvite", acceptItem.SelectedValue);
                menu.Close();
            }
        });

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

    function getInteractionMenu(): NativeUI.Menu {
        return new Menu("Hauptmenü", "Interaktionen | " + mp.players.local.name, new Point(50, 50), null, null);
    }

    function getAccountMenu(data: AccountData): NativeUI.Menu {
        var accountMenu = new NativeUI.Menu("Account", mp.players.local.name, new Point(50, 50), null, null);

        var menuItem = new UIMenuItem("Fraktion");
        menuItem.SetRightLabel(data.faction);
        accountMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Fraktionsrang");
        menuItem.SetRightLabel(data.factionRank);
        accountMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Job");
        menuItem.SetRightLabel(data.job);
        accountMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Gruppe");
        menuItem.SetRightLabel(data.group);
        accountMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Gruppenrang");
        menuItem.SetRightLabel(data.groupRank);
        accountMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Registrierungsdatum");
        menuItem.SetRightLabel(data.regDate);
        accountMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Adminrang");
        menuItem.SetRightLabel(data.adminLevel);
        accountMenu.AddItem(menuItem);

        accountMenu.Visible = false;
        return accountMenu;
    }

    function getLicenseMenu(data: Licenses, parentMenu: NativeUI.Menu): NativeUI.Menu {
        var licenseMenu = new NativeUI.Menu("Lizenzen", mp.players.local.name, new Point(50, 50), null, null);

        var menuItem = new UIMenuItem("Auto Führerschein");
        if (data.drivingLicenseCar == false) {
            menuItem.SetRightLabel("~r~nicht erhalten");
        } else if (data.drivingLicenseCar == true) {
            menuItem.SetRightLabel("~g~erhalten");
        }
        licenseMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Motorrad Führerschein");
        if (data.drivingLicenseBike == false) {
            menuItem.SetRightLabel("~r~nicht erhalten");
        } else if (data.drivingLicenseBike == true) {
            menuItem.SetRightLabel("~g~erhalten");
        }
        licenseMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Flugschein Flugzeug");
        if (data.flightLicensePlane == false) {
            menuItem.SetRightLabel("~r~nicht erhalten");
        } else if (data.flightLicensePlane == true) {
            menuItem.SetRightLabel("~g~erhalten");
        }
        licenseMenu.AddItem(menuItem);

        menuItem = new UIMenuItem("Waffenschein");
        if (data.weaponLicense == false) {
            menuItem.SetRightLabel("~r~nicht erhalten");
        } else if (data.weaponLicense == true) {
            menuItem.SetRightLabel("~g~erhalten");
        }
        licenseMenu.AddItem(menuItem);

        licenseMenu.AddItem(new UIMenuItem("Lizenzen an Spieler zeigen"));

        licenseMenu.Visible = false;

        licenseMenu.ItemSelect.on((item, index) => {
            if (index === 4) {
                var input = new InputHelper("Welchen Spieler möchtest du deine Lizenzen zeigen (Name / ID)?", globalData);
                input.show();
                input.getValue(name => {
                    mp.events.callRemote("CLIENT:InteractionMenu_Show", "License", name);
                    parentMenu.Close();
                });
            }

        });

        return licenseMenu;
    }

    function getFactionMenu(faction: string, parentMenu: NativeUI.Menu): NativeUI.Menu {
        var factionMenu = new NativeUI.Menu("Fraktion", faction, new Point(50, 50), null, null);
        factionMenu.AddItem(new UIMenuItem("Spieler einladen"));
        factionMenu.AddItem(new UIMenuItem("Spieler rauswerfen"));
        factionMenu.Visible = false;

        factionMenu.ItemSelect.on((item, index) => {
            if (index === 0) { // Inviten
                var input = new InputHelper("Welchen Spieler möchtest du inviten (Name / ID)?", globalData);
                input.show();
                input.getValue(name => {
                    mp.events.callRemote("CLIENT:InteractionMenu_Invite", "Fraktion", name);
                    parentMenu.Close();
                });
            } else if (index === 1) { // Uninviten
                var input = new InputHelper("Welchen Spieler möchtest du uninviten (Name / ID)?", globalData);
                input.show();
                input.getValue(name => {
                    mp.events.callRemote("CLIENT:InteractionMenu_Uninvite", "Fraktion", name);
                    parentMenu.Close();
                });
            }
        });

        return factionMenu;
    }

    function getGroupMenu(group: string, parentMenu: NativeUI.Menu): NativeUI.Menu {
        var groupMenu = new NativeUI.Menu("Gruppe", group, new Point(50, 50), null, null);
        groupMenu.AddItem(new UIMenuItem("Spieler einladen"));
        groupMenu.AddItem(new UIMenuItem("Spieler rauswerfen"));
        groupMenu.Visible = false;

        groupMenu.ItemSelect.on((item, index) => {
            if (index === 0) { // Inviten
                var input = new InputHelper("Welchen Spieler möchtest du inviten (Name / ID)?", globalData);
                input.show();
                input.getValue(name => {
                    mp.events.callRemote("CLIENT:InteractionMenu_Invite", "Gruppe", name);
                    parentMenu.Close();
                });
            } else if (index === 1) { // Uninviten
                var input = new InputHelper("Welchen Spieler möchtest du uninviten (Name / ID)?", globalData);
                input.show();
                input.getValue(name => {
                    mp.events.callRemote("CLIENT:InteractionMenu_Uninvite", "Gruppe", name);
                    parentMenu.Close();
                });
            }
        });

        return groupMenu;
    }

    function getServicesMenu(parentMenu: NativeUI.Menu): NativeUI.Menu {
        var servicesMenu = new Menu("Services", "Rufe einen Service", new Point(50, 50), null, null);
        servicesMenu.AddItem(new UIMenuItem("Taxi rufen", "Rufe ein Taxi zu deiner Position"));

        servicesMenu.Visible = false;

        servicesMenu.ItemSelect.on((item, index) => {
            if (index === 0) { // Taxi
                mp.events.callRemote("CLIENT:InteractionMenu_CallService_Taxi", getStreetName(), getZoneName());
            }
            parentMenu.Close();
        });

        return servicesMenu;
    }

}
