import * as NativeUI from '../libs/NativeUI';
const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const UIMenuCheckboxItem = NativeUI.UIMenuCheckboxItem;
const UIMenuSliderItem = NativeUI.UIMenuSliderItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;
const ListItem = NativeUI.ListItem;
var screenRes = mp.game.graphics.getScreenResolution(0, 0);

import InputHelper from '../inputhelper';

export default function business(globalData: IGlobalData) {

    var keyBound = false;

    var closeMenu = false;

    var mainMenu: NativeUI.Menu;

    var businessData: any;
    var businessState: number;

    mp.events.add('SERVER:Business_ShowMenuHelp', (bizName, bizMoney) => {
        /*mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um das Businessmenü zu öffnen');
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);
        */
        mp.events.call("renderHelpTextOnScreen", "~s~Drücke ~g~E~s~ um das ~b~Businessmenü ~s~zu öffnen")
        mp.keys.bind(0x45, false, keyPressHandler);
        keyBound = true;
    });

    mp.events.add('SERVER:Business_SetData', (data, state: number) => {
        businessData = JSON.parse(data);
        businessState = state;
    });

    mp.events.add('SERVER:Business_RemoveHelp', (unbind) => {
        mp.game.ui.clearHelp(true);
        mp.gui.chat.show(true);

        if (keyBound && unbind) {
            if (mainMenu) mainMenu.Close();

            mp.keys.unbind(0x45, false, keyPressHandler);
            keyBound = false;
        }
    });

    function keyPressHandler() {
        mp.events.call('SERVER:Business_RemoveHelp', false);

        if (globalData.InChat || globalData.InMenu || globalData.InMenu) return;

        globalData.InMenu = true;

        mainMenu = new Menu("Businessverwaltung", businessData.Name, new Point(0, screenRes.y / 3), null, null);

        var bankAccountItem: NativeUI.UIMenuItem;

        var buyItem: NativeUI.UIMenuItem;

        var sellItem: NativeUI.UIMenuItem;

        //var partnerItem = new UIMenuItem("Inteilhaber", "Verwalte den Inteilhaber");
        //partnerItem.SetRightLabel("Niemand");
        //mainMenu.AddItem(partnerItem);

        if (businessState == 1) { // Besitzer
            bankAccountItem = new UIMenuItem("Businesskasse", "Verwalte die Businesskasse");
            bankAccountItem.SetRightLabel("~s~" + businessData.Balance);
            mainMenu.AddItem(bankAccountItem);

            var bankMenu = new Menu("Bankkonto", businessData.Name, new Point(0, screenRes.y / 3), null, null);
            bankMenu.Visible = false;

            var infoItem = new UIMenuItem("Aktueller Kontostand");
            infoItem.SetRightLabel("~s~" + businessData.Balance);
            bankMenu.AddItem(infoItem);

            var depositItem = new UIMenuItem("Einzahlen", "Zahle Geld auf die Businesskasse ein");
            bankMenu.AddItem(depositItem);

            var withdrawItem = new UIMenuItem("Auszahlen", "Zahle Geld von der Businesskasse aus");
            bankMenu.AddItem(withdrawItem);

            sellItem = new UIMenuItem("Business verkaufen");
            mainMenu.AddItem(sellItem);

            bankMenu.ItemSelect.on((item, index) => {
                if (item === depositItem) {
                    globalData.InMenu = true;
                    var depositInput = new InputHelper("Wie viel Geld möchtest du auf deine Businesskasse einzahlen?", globalData);
                    depositInput.show();
                    depositInput.getValue((data) => {
                        var amount = parseInt(data);
                        if (isNaN(amount)) {
                            mp.game.graphics.notify('~r~Du musst eine Nummer eingeben!');
                            return;
                        }

                        globalData.InMenu = false;
                        mp.events.callRemote('Business_DepositMoney', amount);

                        mainMenu.Close();
                    });
                } else if (item === withdrawItem) {
                    globalData.InMenu = true;
                    var withdrawInput = new InputHelper("Wie viel Geld möchtest du von deiner Businesskasse abheben?", globalData);
                    withdrawInput.show();
                    withdrawInput.getValue((data) => {
                        var amount = parseInt(data);
                        if (isNaN(amount)) {
                            mp.game.graphics.notify('~r~Du musst eine Nummer eingeben!');
                            return;
                        }
                        globalData.InMenu = false;
                        mp.events.callRemote('Business_WithdrawMoney', amount);

                        mainMenu.Close();
                    });
                }
            });

            mainMenu.BindMenuToItem(bankMenu, bankAccountItem);
        } else if (businessState == 0) { // Kein Besitzer
            buyItem = new UIMenuItem("Kaufen", businessData.Price);
            mainMenu.AddItem(buyItem);
        } else if (businessState == -1) {
            mp.game.graphics.notify("Hier gibt es nichts zu sehen");
        }

        mainMenu.ItemSelect.on((item, index) => {
            if (item === buyItem) {
                mainMenu.Close();
                mp.events.callRemote("CLIENT:Business_BuyBusiness");
            } else if (item === sellItem) {
                mainMenu.Close();
                mp.events.callRemote("CLIENT:Business_SellBusiness");
            }
        });

        mainMenu.Open();

        mainMenu.MenuClose.on(() => {
            mp.events.call('SERVER:Business_RemoveHelp', false);
            globalData.InMenu = false;
        });
    }
}