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: IGlobalData) {

    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", "Fraktionsfahrzeug finden");

    var factionleaderItem = 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");

    var vehiclesItem = new UIMenuItem("Fahrzeuge", "Zeige deine Fahrzeuge an");

    var ticketItem = new UIMenuItem("Strafticket", "Strafticket annehmen/ablehnen");

    var payName: string;

    mp.events.add("SERVER:InteractionMenu_OpenMenu", (accountDataJson: string, factionleader: string, faction: string, group: string, factionInvite: boolean, groupInvite: boolean, ticket_boolean: boolean, ticket_amount) => {


        var accountData: AccountData = JSON.parse(accountDataJson);
        //var ticket_amount = JSON.parse(ticket_amountJson);

        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, accountData.licenses, menu), licenseItem);

        var vehicles = accountData.vehicles;
        if (vehicles.length > 0) {
            menu.AddItem(vehiclesItem);
            menu.BindMenuToItem(getVehiclesMenu(vehicles, "CLIENT:InteractionMenu_PlayerVehicleInteraction", true, menu), vehiclesItem);
        }

        if (faction && !factionleader) {
            factionItem.SetRightLabel(faction);
            menu.AddItem(factionItem);
            menu.BindMenuToItem(getFactionMenu(faction, accountData.factionVehicles, accountData.stateFaction, menu), factionItem);
        }
        if (faction && factionleader) {
            factionleaderItem.SetRightLabel(factionleader);
            menu.AddItem(factionleaderItem);
            menu.BindMenuToItem(getFactionLeaderMenu(faction, accountData.factionVehicles, accountData.stateFaction, menu), factionleaderItem);
        }

        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("Sonstige Einkünfte");
            item.SetRightLabel("~g~+ $" + moneyFormat(p.otheramount));
            paycheckMenu.AddItem(item);

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

            if (p.factionMoney) {
                item = new UIMenuItem("Fraktionsgeld");
                item.SetRightLabel("~g~+ $" + moneyFormat(p.factionMoney));
                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("Krankenkasse");
            item.SetRightLabel("~r~- $" + moneyFormat(p.healthInsurance));
            paycheckMenu.AddItem(item);

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

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

        var payItem = new UIMenuItem("Geld geben");
        menu.AddItem(payItem);

        var nextPaydayItem = new UIMenuItem("Nächster PayDay");
        nextPaydayItem.SetRightLabel(`${accountData.nextPayday} Minuten`);
        menu.AddItem(nextPaydayItem);

        if (ticket_boolean == true) {
            menu.AddItem(ticketItem);
            menu.BindMenuToItem(getTicketMenu(ticket_amount, menu), ticketItem);
        }

        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(true);
                globalData.InMenu = false;
            } else if (item === payItem) {
                mp.gui.chat.activate(true);
                mp.gui.cursor.show(true, true);
                globalData.InMenu = false;
                globalData.InInput = true;
                var input = new InputHelper("Welchem Spieler möchtest du Geld geben (Name / ID)?", globalData);
                input.show();
                input.getValue(name => {
                    mp.events.call("CLIENT:InteractionMenu_Pay_Name", name);
                    mp.gui.cursor.show(false, false);
                    menu.Close(true);     
                    globalData.InInput = false;
                });
            }
        });

        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(accdata: AccountData, 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);

        menuItem = new UIMenuItem("Strafpunkte");
        if (accdata.points == 0) {
            menuItem.SetRightLabel("~g~" + accdata.points);
        }
        else if (accdata.points >= 1 && accdata.points <= 5) {
            menuItem.SetRightLabel("~y~" + accdata.points);
        }
        if (accdata.points > 5) {
            menuItem.SetRightLabel("~r~" + accdata.points);
        }
        licenseMenu.AddItem(menuItem);

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

        licenseMenu.Visible = false;

        licenseMenu.ItemSelect.on((item, index) => {
            if (item === showItem) {
                globalData.InInput = true;
                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(true);
                    globalData.InInput = false;
                });
            }

        });

        return licenseMenu;
    }

    function getFactionMenu(faction: string, vehData: VehicleData[], stateFaction: boolean, parentMenu: NativeUI.Menu): NativeUI.Menu {

        var factionMenu = new NativeUI.Menu("Fraktion", faction, new Point(50, 50), null, null);
 
     
        factionMenu.Visible = false;

        if (vehData.length > 0) {
            var factionVehicleItem = new UIMenuItem("Fahrzeuge Finden", "Finden");
            factionMenu.AddItem(factionVehicleItem);
            factionMenu.BindMenuToItem(getVehiclesMenuFaction(vehData, "CLIENT:InteractionMenu_FactionVehicleInteraction", !stateFaction, parentMenu), factionVehicleItem);
        }

        return factionMenu;
    }
    function getFactionLeaderMenu(faction: string, vehData: VehicleData[], stateFaction: boolean, 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;

        if (vehData.length > 0) {
            var factionVehicleItem = new UIMenuItem("Fahrzeuge", "Finden oder Verkaufen");
            factionMenu.AddItem(factionVehicleItem);
            factionMenu.BindMenuToItem(getVehiclesMenu(vehData, "CLIENT:InteractionMenu_FactionVehicleInteraction", !stateFaction, parentMenu), factionVehicleItem);
        }


        factionMenu.ItemSelect.on((item, index) => {
            if (index === 0) { // Inviten
                globalData.InInput = true;
                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();
                    globalData.InInput = false;
                });
            } else if (index === 1) { // Uninviten
                globalData.InInput == true;
                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();
                    globalData.InInput = false;
                });
            }
        });

        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
                globalData.InInput = true;
                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();
                    globalData.InInput = false;
                });
            } else if (index === 1) { // Uninviten
                globalData.InInput == true;
                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();
                    globalData.InInput = false;
                });
            }
        });

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

    function getVehiclesMenu(vehicles: VehicleData[], eventName: string, allowSell: boolean, parentMenu: NativeUI.Menu): NativeUI.Menu {
        var desc = "Finden";
        if (allowSell) {
            desc += " oder Verkaufen";
        }

        var menu = new Menu("Fahrzeuge", desc, new Point(50, 50), null, null);

        var items: string[] = ["Finden", "Verkaufen"];

        var collection = new ItemsCollection(items);

        vehicles.forEach(veh => {
            var name: string = mp.game.ui.getLabelText(mp.game.vehicle.getDisplayNameFromVehicleModel(veh.Model));

            var item = new UIMenuListItem(`${veh.Id.toString().padEnd(5)} | ${name}`, "", collection, null, { id: veh.Id });

            menu.AddItem(item);
        });

        menu.ItemSelect.on((item: NativeUI.UIMenuListItem, index: number) => {
            var id = item.Data.id;
            var selection = item.SelectedValue;

            mp.events.callRemote(eventName, id, selection);

            parentMenu.Close(true);
        });

        return menu;
    }
    function getVehiclesMenuFaction(vehicles: VehicleData[], eventName: string, allowSell: boolean, parentMenu: NativeUI.Menu): NativeUI.Menu {
        var desc = "Finden";


        var menu = new Menu("Fahrzeuge", desc, new Point(50, 50), null, null);

        var items: string[] = ["Finden"];

        var collection = new ItemsCollection(items);

        vehicles.forEach(veh => {
            var name: string = mp.game.ui.getLabelText(mp.game.vehicle.getDisplayNameFromVehicleModel(veh.Model));

            var item = new UIMenuListItem(`${veh.Id.toString().padEnd(5)} | ${name}`, "", collection, null, { id: veh.Id });

            menu.AddItem(item);
        });

        menu.ItemSelect.on((item: NativeUI.UIMenuListItem, index: number) => {
            var id = item.Data.id;
            var selection = item.SelectedValue;

            mp.events.callRemote(eventName, id, selection);

            parentMenu.Close(true);
        });

        return menu;
    }
   
    function getTicketMenu(ticket_amount: number, parentMenu: NativeUI.Menu): NativeUI.Menu {
        var ticketMenu = new NativeUI.Menu("Ticket", `Ticket in Höhe von ${ticket_amount}$`, new Point(50, 50), null, null);
        ticketMenu.AddItem(new UIMenuItem("Ticket annehmen"));
        ticketMenu.AddItem(new UIMenuItem("Ticket ablehnen"));
        ticketMenu.Visible = false;

        ticketMenu.ItemSelect.on((item, index) => {
            if (index === 0) // Ticket annehmen
            {
                mp.events.callRemote("CLIENT:InteractionMenu_Ticket", "Angenommen", ticket_amount);
                parentMenu.Close(true);
            }
            else if (index === 1) // Ticket ablehnen
            {
                mp.events.callRemote("CLIENT:InteractionMenu_Ticket", "Abgelehnt", ticket_amount);
                parentMenu.Close(true);
            }
        });

        return ticketMenu;
    }
    
    mp.events.add("CLIENT:InteractionMenu_Pay_Name", (name) => {
        payName = name;
        globalData.InInput = true;
        var input = new InputHelper("Wie viel?", globalData);
        mp.gui.cursor.show(true, true);
        input.show();
        input.getValue(amount => {
            mp.events.callRemote("CLIENT:InteractionMenu_Pay", JSON.stringify(payName), amount);
            mp.gui.cursor.show(false, false);
            globalData.InInput = false;
        });
    });
}
