import * as NativeUI from 'NativeUI';
import InputHelper from '../inputhelper';

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


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

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

        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.Visible = true;
        mp.gui.chat.show(false);
        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;
            mp.gui.chat.show(true);
        })
    });

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