import { Menu, Point, UIMenuItem, Color } from '../libs/NativeUI';
import moneyFormat from '../moneyformat';
import InputHelper from '../inputhelper';
import game from '..';
import { Key } from '../game';

var houseMenu: Menu;
var houseData: any;
var houseState: number;
var screenRes = mp.game.graphics.getScreenResolution(0, 0);

game.events.add("SERVER:ShowHouseMenu", () => {

    //game.ui.setHelpText('Drücke ~INPUT_CONTEXT~, um das Hausmenü öffnen');
    mp.events.call("renderHelpTextOnScreen", "~s~Drücke ~g~E~s~ um das ~b~Hausmenü ~s~öffnen");    
    game.events.bindKey(Key.E, false, keyPressHandler);
});

game.events.add("SERVER:SetHouseData", (dataStr, state) => {
    houseData = JSON.parse(dataStr);
    houseState = state;
});

function keyPressHandler() {
    if (game.ui.inMenu || game.ui.inChat) return;
    game.ui.clearHelpText();

    var subTitle = houseData.Type;
    if (houseData.OwnerName) {
        subTitle += " von " + houseData.OwnerName;
    }

    houseMenu = new Menu("Hausverwaltung", subTitle, new Point(0, screenRes.y / 3), null, null);

    game.ui.inMenu = true;

    var rentInItem: UIMenuItem;
    var cancelOwnRentalItem: UIMenuItem;

    var buyHouseItem: UIMenuItem;

    var setRentalFeeItem: UIMenuItem;
    var cancelRentalsItem: UIMenuItem;
    var sellHouseItem: UIMenuItem;
    var withdrawBankItem: UIMenuItem;

    var houseRentals = houseData.Rentals.length;

    if (houseState === -1) { // Keine Beziehung zum Haus
        rentInItem = new UIMenuItem("Einmieten", "Miete dich in das Haus ein");
        rentInItem.SetRightLabel(moneyFormat(houseData.RentalFee.toString(), 0) + "$");
        houseMenu.AddItem(rentInItem);
    } else if (houseState === 0) { // Haus hat keinen Eigentümer
        buyHouseItem = new UIMenuItem("Haus kaufen", "Kaufe das Haus");
        buyHouseItem.SetRightLabel(moneyFormat(houseData.Price, 0) + "$");
        houseMenu.AddItem(buyHouseItem);
    } else if (houseState === 1) { // Hausbesitzer
        setRentalFeeItem = new UIMenuItem("Miete setzen", "Setze den Mietpreis");
        setRentalFeeItem.SetRightLabel(moneyFormat(houseData.RentalFee, 0) + "$");
        houseMenu.AddItem(setRentalFeeItem);

        if (houseRentals > 0) {
            cancelRentalsItem = new UIMenuItem("Mieter", "Liste deine Mieter auf");
            cancelRentalsItem.SetRightLabel(houseRentals.toString());
            houseMenu.AddItem(cancelRentalsItem);

            var cancelRentalsMenu = new Menu("Mieter", "Kündige einen Mieter", new Point(0, screenRes.y / 3), null, null);
            cancelRentalsMenu.Visible = false;

            houseData.Rentals.forEach(rental => {
                var item = new UIMenuItem(rental, "");
                item.SetRightLabel("Kündigen");
                cancelRentalsMenu.AddItem(item);
            });

            cancelRentalsMenu.ItemSelect.on((item: UIMenuItem, index: number) => {
                game.events.callServer("House_CancelUserRental", item.Text);
                houseRentals--;
                cancelRentalsItem.SetRightLabel(houseRentals.toString());
                if (houseRentals === 0) {
                    cancelRentalsMenu.Visible = false;
                    houseMenu.Visible = true;
                    houseMenu.RemoveItem(cancelRentalsItem);
                }

                cancelRentalsMenu.RemoveItem(item);
            });

            houseMenu.BindMenuToItem(cancelRentalsMenu, cancelRentalsItem);
        }

        withdrawBankItem = new UIMenuItem("Konto", "Hebe Geld vom Konto ab");
        withdrawBankItem.SetRightLabel(moneyFormat(houseData.Balance) + "$");
        houseMenu.AddItem(withdrawBankItem);

        sellHouseItem = new UIMenuItem("Haus verkaufen");
        houseMenu.AddItem(sellHouseItem);


    } else if (houseState === 2) {
        cancelOwnRentalItem = new UIMenuItem("Mietvertrag kündigen", "Ziehe aus der Wohnung aus");
        houseMenu.AddItem(cancelOwnRentalItem);
    }

    var cancelItem = new UIMenuItem("Abbrechen");
    cancelItem.BackColor = new Color(213, 0, 0);
    cancelItem.HighlightedBackColor = new Color(229, 57, 53);
    houseMenu.AddItem(cancelItem);

    houseMenu.Open();

    houseMenu.ItemSelect.on((item, index) => {
        if (item === cancelItem) {
            houseMenu.Close();
        } else if (item === buyHouseItem) {
            game.events.callServer("House_BuyHouse");
            houseMenu.Close();
        } else if (item === setRentalFeeItem) {
            var rentalFeeInput = new InputHelper("Wie hoch soll die Miete sein?");
            rentalFeeInput.show();
            rentalFeeInput.getValue(data => {
                if (isNaN(data)) {
                    return;
                }

                var rentalFee = parseInt(data);

                game.events.callServer("House_SetRentalFee", rentalFee);
                houseMenu.Close();
            });
        } else if (item === rentInItem) {
            game.events.callServer("House_RentInHouse");
            houseMenu.Close();
        } else if (item === cancelOwnRentalItem) {
            game.events.callServer("House_CancelOwnRental");
            houseMenu.Close();
        } else if (item === sellHouseItem) {
            game.events.callServer("House_SellHouse");
            houseMenu.Close();
        } else if (item == withdrawBankItem) {
            var input = new InputHelper("Wie viel Geld möchtest du abheben?");
            input.show();
            input.getValue(data => {
                if (isNaN(data)) {
                    return;
                }

                var withdrawAmount = parseInt(data);

                game.events.callServer("House_WithdrawMoney", withdrawAmount);
                houseMenu.Close();
            });
        }
    });

    houseMenu.MenuClose.on(() => {
        game.ui.inMenu = false;
    });
}

game.events.add("SERVER:CloseHouseMenu", () => {
    game.ui.clearHelpText();
    if (houseMenu != null) {
        houseMenu.Close();
    }
    game.events.unbindKey(Key.E, false, keyPressHandler);
});