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 moneyFormat from '../moneyformat';

export default function carDealer(globalData: GlobalData) {

    var shopMenu: NativeUI.Menu;

    mp.events.add("entityStreamIn", (entity: EntityMp) => {
        if (!entity.isAVehicle()) return;
        var labelId = entity.getVariable("shopVehicleTextLabel");
        if (labelId === undefined) return;
        var label: TextLabelMp = mp.labels.atRemoteId(labelId);
        setShopVehicleTextLabel(entity as VehicleMp, label);
    });

    mp.events.addDataHandler("shopVehicleTextLabel", (entity: EntityMp, labelId: number) => {
        var label = mp.labels.atRemoteId(labelId);
        setShopVehicleTextLabel(entity as VehicleMp, label);
    });

    function setShopVehicleTextLabel(entity: VehicleMp, label: TextLabelMp) {
        if (label.text.indexOf("SHOPVEHICLE") === -1) return;
        var realName = mp.game.ui.getLabelText(mp.game.vehicle.getDisplayNameFromVehicleModel(entity.model))
        var text = label.text.replace("SHOPVEHICLE", realName);
        label.text = text;
    }
            

    mp.events.add('ShopVehicle_OpenMenu', (businessName, price, availableOptions: string[]) => {
        var veh = mp.players.local.vehicle;
        if (!veh) return;
        mp.gui.chat.show(false);
        shopMenu = new Menu("Fahrzeugkauf", "Kaufe ein neues Auto", new Point(50, 50), null, null);

        globalData.InMenu = true;

        var carItem = new UIMenuItem("Fahrzeugname");
        carItem.SetRightLabel(mp.game.ui.getLabelText(mp.game.vehicle.getDisplayNameFromVehicleModel(veh.model)));
        shopMenu.AddItem(carItem);

        var shopItem = new UIMenuItem("Autohaus");
        shopItem.SetRightLabel(businessName);
        shopMenu.AddItem(shopItem);

        var priceItem = new UIMenuItem("Preis");
        priceItem.SetRightLabel("~g~$~s~ " + moneyFormat(price));
        shopMenu.AddItem(priceItem);

        var targetsCollection = new ItemsCollection(availableOptions);
        var targetItem = new UIMenuListItem("Kaufen für", "Wähle den Besitzer aus", targetsCollection);
        shopMenu.AddItem(targetItem);

        var saveItem = new UIMenuItem("Kaufen");
        saveItem.BackColor = new Color(0, 100, 0);
        saveItem.HighlightedBackColor = new Color(0, 150, 0);
        shopMenu.AddItem(saveItem);

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

        shopMenu.ItemSelect.on((item, index) => {
            if (item === cancelItem) {
                shopMenu.Close();
            } else if (item === saveItem) {
                mp.events.callRemote("VehShop_BuyVehicle", targetItem.SelectedValue);
                shopMenu.Close();
            }
        });

        shopMenu.MenuClose.on(() => {
            mp.gui.chat.show(true);
            mp.players.local.taskLeaveVehicle(veh.handle, 0);
            globalData.InMenu = false;
        });

        shopMenu.Open();
    });
}