/**
* @overview Life of German Reallife - Managers Interaction (InteractionManager.cs)
* @author MichaPlays
* @copyright (c) 2008 - 2018 Life of German
*/

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

let screenRes = mp.game.graphics.getScreenResolution(0, 0);
let saveItem = new UIMenuItem("Bestätigen", "");
saveItem.BackColor = new Color(13, 71, 161);
saveItem.HighlightedBackColor = new Color(25, 118, 210);

let cancelItem = new UIMenuItem("Schließen", "");
cancelItem.BackColor = new Color(213, 0, 0);
cancelItem.HighlightedBackColor = new Color(229, 57, 53);

export default function bankMenuHandle(globalData: IGlobalData) {

    var factionBankmenu: NativeUI.Menu;
    var bankmenu: NativeUI.Menu;


    var paymentItem = new UIMenuItem("Überweisen", "Spieler Geld überweisen (+5% Steuern)");

    var payInItem = new UIMenuItem("Einzahlen", "Geld einzahlen");

    var payOutItem = new UIMenuItem("Auszahlen", "Geld auszahlen");

    var factionPaymentItem = new UIMenuItem("Überweisen", "Spieler Geld überweisen (+5% Steuern)");

    var factionPayInItem = new UIMenuItem("Einzahlen", "Geld auf die Fraktionskasse zahlen");

    var factionPayOutItem = new UIMenuItem("Überweisen", "Geld von der Fraktionskasse abheben");

    var factionBankBalance = new UIMenuItem("Kontostand", "");

    var paymentname: string;

    var factionname: string;

    mp.events.add('showBankMenu', () => {
        if (!globalData.InMenu) {

            globalData.InMenu = true;

            bankmenu = new Menu("Bank", "", new Point(50, 50), null, null);

            bankmenu.AddItem(payInItem);
            bankmenu.AddItem(payOutItem);
            bankmenu.AddItem(paymentItem);
            bankmenu.AddItem(cancelItem);
            bankmenu.Visible = true;

            bankmenu.ItemSelect.on((item) => {
                if (item.Text === "Einzahlen") {
                    mp.gui.chat.activate(true);
                    mp.gui.cursor.show(true, true);
                    globalData.InMenu = false;
                    globalData.InInput = true;
                    var input = new InputHelper("Wie viel möchtest du einzahlen?", globalData);
                    input.show();
                    input.getValue(amount => {
                        mp.events.callRemote("CLIENT:payIn", amount);
                        mp.gui.cursor.show(false, false);
                        bankmenu.Close(true);
                        globalData.InInput = false;
                    });
                }else if (item.Text === "Auszahlen") {
                    mp.gui.chat.activate(true);
                    mp.gui.cursor.show(true, true);
                    globalData.InMenu = false;
                    globalData.InInput = true;
                    var input = new InputHelper("Wie viel möchtest du Auszahlen?", globalData);
                    input.show();
                    input.getValue(amount => {
                        mp.events.callRemote("CLIENT:payOut", amount);
                        mp.gui.cursor.show(false, false);
                        bankmenu.Close(true);
                        globalData.InInput = false;
                    });
                } else if (item.Text === "Überweisen" ) {
                    mp.gui.chat.activate(true);
                    mp.gui.cursor.show(true, true);
                    globalData.InMenu = false;
                    globalData.InInput = true;
                    var input = new InputHelper("Wem möchtest du Geld überweisen? (Name/ID)", globalData);
                    input.show();
                    input.getValue(name => {
                        mp.events.call("CLIENT:bank_name", name);
                        mp.gui.cursor.show(false, false);
                        bankmenu.Close(true);
                        globalData.InInput = false;
                    });

                } else if (item.Text === "Schließen") {
                    mp.gui.cursor.show(false, false);
                    bankmenu.Close(true);
                    globalData.InInput = false;
                }
            });

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

    mp.events.add('removeBankmenu', () => {
        mp.gui.cursor.show(false, false);
        bankmenu.Close(true);
        globalData.InInput = false;
    });

    mp.events.add("CLIENT:bank_name", (name) => {
        paymentname = name;
        globalData.InInput = true;
        var input = new InputHelper("Wie viel möchtest du Überweisen??", globalData);
        mp.gui.cursor.show(true, true);
        input.show();
        input.getValue(amount => {
            mp.events.callRemote("CLIENT:payment", JSON.stringify(paymentname), amount);
            mp.gui.cursor.show(false, false);
            globalData.InInput = false;
        });
    });


    mp.events.add('removeFactionBankmenu', () => {
        mp.gui.cursor.show(false, false);
        factionBankmenu.Close(true);
        globalData.InInput = false;
    });



    mp.events.add('showFactionBankMenu', (factionMoney) => {
        if (!globalData.InMenu) {

            globalData.InMenu = true;

            factionBankmenu = new Menu("Fraktionskasse", "", new Point(50, 50), null, null);
            factionBankmenu.AddItem(factionBankBalance);
            factionBankBalance.SetRightLabel("$" + factionMoney);
            factionBankmenu.AddItem(factionPayInItem);
            factionBankmenu.AddItem(cancelItem);
            factionBankmenu.Visible = true;

            factionBankmenu.ItemSelect.on((item) => {
                if (item.Text === "Einzahlen") {
                    mp.gui.chat.activate(true);
                    mp.gui.cursor.show(true, true);
                    globalData.InMenu = false;
                    globalData.InInput = true;
                    var input = new InputHelper("Wie viel möchtest du einzahlen?", globalData);
                    input.show();
                    input.getValue(amount => {
                        mp.events.callRemote("CLIENT:factionPayIn", amount);
                        mp.gui.cursor.show(false, false);
                        factionBankmenu.Close(true);
                        globalData.InInput = false;
                    });
                }else if (item.Text === "Schließen") {
                    mp.gui.cursor.show(false, false);
                    factionBankmenu.Close(true);
                    globalData.InInput = false;
                }
            });

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

    mp.events.add('showFactionBankMenuLeader', (factionMoney) => {
        if (!globalData.InMenu) {

            globalData.InMenu = true;

            factionBankmenu = new Menu("Fraktionskasse", "", new Point(50, 50), null, null);
            factionBankmenu.AddItem(factionBankBalance);
            factionBankBalance.SetRightLabel("$" + factionMoney);
            factionBankmenu.AddItem(factionPayInItem);
            factionBankmenu.AddItem(factionPaymentItem);
            factionBankmenu.AddItem(cancelItem);
            factionBankmenu.Visible = true;

            factionBankmenu.ItemSelect.on((item) => {
                if (item.Text === "Einzahlen") {
                    mp.gui.chat.activate(true);
                    mp.gui.cursor.show(true, true);
                    globalData.InMenu = false;
                    globalData.InInput = true;
                    var input = new InputHelper("Wie viel möchtest du einzahlen?", globalData);
                    input.show();
                    input.getValue(amount => {
                        mp.events.callRemote("CLIENT:factionPayIn", amount);
                        mp.gui.cursor.show(false, false);
                        factionBankmenu.Close(true);
                        globalData.InInput = false;
                    });
                }else if (item.Text === "Überweisen") {
                    mp.gui.chat.activate(true);
                    mp.gui.cursor.show(true, true);
                    globalData.InMenu = false;
                    globalData.InInput = true;
                    var input = new InputHelper("Wem möchtest du Geld überweisen? (Name/ID)", globalData);
                    input.show();
                    input.getValue(name => {
                        mp.events.call("CLIENT:bank_factionname", name);
                        mp.gui.cursor.show(false, false);
                        factionBankmenu.Close(true);
                        globalData.InInput = false;
                    });
                }else if (item.Text === "Schließen") {
                    mp.gui.cursor.show(false, false);
                    factionBankmenu.Close(true);
                    globalData.InInput = false;
                }
            });

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

    mp.events.add("CLIENT:bank_factionname", (name) => {
        paymentname = name;
        globalData.InInput = true;
        var input = new InputHelper("Wie viel möchtest du Überweisen?", globalData);
        mp.gui.cursor.show(true, true);
        input.show();
        input.getValue(amount => {
            mp.events.callRemote("CLIENT:factionPayment", JSON.stringify(paymentname), amount);
            mp.gui.cursor.show(false, false);
            globalData.InInput = false;
        });
    });


}