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

export default function house(globalData: GlobalData) {

    var houseMenu: Menu;
    var houseData: any;
    var houseState: number;

    var keyBound = false;

    mp.events.add("SERVER:ShowHouseMenu", () => {
        mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um das Hausmenü öffnen');
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);

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

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

    function keyPressHandler() {
        if (globalData.InMenu || globalData.InChat || globalData.InInput || !globalData.LoggedIn) return;
        mp.game.ui.clearHelp(true);

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

        houseMenu = new Menu("Hausverwaltung", subTitle, new Point(50, 50), null, null);

        globalData.InMenu = true;

        var rentInItem: UIMenuItem;
        var cancelOwnRentalItem: UIMenuItem;

        var buyHouseItem: UIMenuItem;

        var setRentalFeeItem: UIMenuItem;
        var cancelRentalsItem: UIMenuItem;
        var sellHouseItem: 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(50, 50), 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) => {
                    mp.events.callRemote("CLIENT: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);
            }

            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) {
                mp.events.call("SERVER:CloseHouseMenu");
                houseMenu.Close();
            } else if (item === buyHouseItem) {
                mp.events.callRemote("CLIENT:House_BuyHouse");
                mp.events.call("SERVER:CloseHouseMenu");
                houseMenu.Close();
            } else if (item === setRentalFeeItem) {
                var rentalFeeInput = new InputHelper("Wie viel soll die Miete betragen?", globalData);
                rentalFeeInput.show();
                rentalFeeInput.getValue(data => {
                    if (isNaN(data)) {
                        return;
                    }

                    var rentalFee = parseInt(data);

                    mp.events.callRemote("CLIENT:House_SetRentalFee", rentalFee);
                    mp.events.call("SERVER:CloseHouseMenu");
                    houseMenu.Close();
                });
            } else if (item === rentInItem) {
                mp.events.callRemote("CLIENT:House_RentInHouse");
                houseMenu.Close();
            } else if (item === cancelOwnRentalItem) {
                mp.events.callRemote("CLIENT:House_CancelOwnRental");
                houseMenu.Close();
            } else if (item === sellHouseItem) {
                mp.events.callRemote("CLIENT:House_SellHouse");
                houseMenu.Close();
            }
        });

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

    mp.events.add("SERVER:CloseHouseMenu", () => {
        mp.game.ui.clearHelp(true);
        if (houseMenu != null) {
            houseMenu.Close();
        }

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