import * as NativeUI from '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;

import InputHelper from '../inputhelper';

export default function business(globalData: GlobalData) {

    var keyBound = false;

    var closeMenu = false;

    var businessName;
    var businessMoney;
    var mainMenu;
    var bankMenu;

    mp.events.add('business_showHelp', (bizName, bizMoney) => {
        mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um dein Business zu verwalten');
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);

        businessName = bizName;
        businessMoney = bizMoney;

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

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

        if (keyBound && unbind) {
            if (typeof mainMenu !== "undefined") mainMenu.Close();
            if (typeof bankMenu !== "undefined") {
                closeMenu = true;
                bankMenu.Close();
            }

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

    mp.events.add('business_updateMoney', (newMoney) => {
        businessMoney = newMoney;
    });

    function keyPressHandler() {
        mp.events.call('business_removeHelp', false);
        mp.gui.chat.show(false);

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

        if (typeof mainMenu !== "undefined" && mainMenu.Visible) {
            return;
        }

        if (typeof bankMenu !== "undefined" && bankMenu.Visible) {
            return;
        }
        globalData.InMenu = true;

        mainMenu = new Menu("Businessverwaltung", businessName, new Point(50, 50), null, null);

        var bankAccountItem = new UIMenuItem("Businesskasse", "Verwalte die Businesskasse");
        bankAccountItem.SetRightLabel("~g~~h~" + businessMoney);
        mainMenu.AddItem(bankAccountItem);

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

        mainMenu.Open();

        mainMenu.ItemSelect.on((item, index) => {
            if (item === bankAccountItem) {
                // manage bank account

                bankMenu = new Menu("Bankkonto", businessName, new Point(50, 50), null, null);

                var infoItem = new UIMenuItem("Aktueller Kontostand");
                infoItem.SetRightLabel("~g~~h~" + businessMoney);
                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);

                bankMenu.ItemSelect.on((item, index) => {
                    if (item === depositItem) {
                        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;
                            }

                            mp.events.callRemote('Business_DepositMoney', amount);
                        });
                    } else if (item === withdrawItem) {
                        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;
                            }

                            mp.events.callRemote('Business_WithdrawMoney', amount);
                        });
                    }
                });

                bankMenu.MenuClose.on(() => {
                    if (closeMenu) {
                        closeMenu = false;
                        return;
                    }
                    mainMenu.Visible = true;
                });

                bankMenu.Visible = true;
                mainMenu.Visible = false;

            }
            //else if (item === partnerItem) {
            //    // manage partner
            //}
        });

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