import * as NativeUI from '../libs/NativeUI';

const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;


let screenRes = mp.game.graphics.getScreenResolution(0, 0);

let sendItem = new UIMenuItem("Mieten", "Fahrzeug Mieten");
sendItem.BackColor = new Color(13, 71, 161);
sendItem.HighlightedBackColor = new Color(25, 118, 210);

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


export default function rentCar(globalData: IGlobalData) {

    var rentcarMenu: NativeUI.Menu;
    var vehiclesToRent: RentcarProperty[] = [];
    var vehicleNames: string[] = [];
    var vehiclePrices: number[] = [];
    var selectedIndex: number;

    var payTimer;
    var totalTime: number = 0;
    var totalBill: number = 0;

    mp.events.add('showRentcarMenu', (vehiclestring, rentcarLocation) => {
        if (globalData.InMenu) {
            return;
        }

        globalData.InMenu = true;

        vehiclesToRent = JSON.parse(vehiclestring);

        vehicleNames = [];
        vehiclePrices = [];
        for (let entry of vehiclesToRent) {
            vehicleNames.push(entry.Name);
            vehiclePrices.push(entry.Price);
        }

        rentcarMenu = new Menu("Fahrzeugverleih", "", new Point(0, screenRes.y / 3), null, null);
        rentcarMenu.AddItem(new UIMenuListItem("Fahrzeug", "$" + vehiclePrices[0] + " alle 3 Minuten", new ItemsCollection(vehicleNames)));

        rentcarMenu.AddItem(sendItem);
        rentcarMenu.AddItem(cancelItem);
        rentcarMenu.Visible = true;
        //outeText = "Route 1";

        selectedIndex = 0;

        rentcarMenu.ListChange.on((item: NativeUI.UIMenuListItem, index) => {
            switch (item.Text) {
                case "Fahrzeug":
                    item.Description = "$" + vehiclePrices[index] + " alle 3 Minuten";
                    selectedIndex = index;
                    //item.Description = item.SelectedValue;
                    break;
            }
        });

        rentcarMenu.ItemSelect.on((item) => {
            if (item.Text === "Mieten") {
                mp.events.callRemote("SERVER:rentcarBooked", vehicleNames[selectedIndex], vehiclePrices[selectedIndex], rentcarLocation);
                rentcarMenu.Close();
                globalData.InMenu = false;
            } else if (item.Text === "Abbrechen") {
                rentcarMenu.Close();
                globalData.InMenu = false;
            }
        });

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

    mp.events.add('triggerRentcarTimer', (interval, pricePerInterval) => {
        totalBill = pricePerInterval;
        mp.events.callRemote("SERVER:updateRentCarBill", totalBill, totalTime);
        clearInterval(payTimer);

        payTimer = setInterval(() => {
            totalTime += interval;
            totalBill += pricePerInterval;

            mp.events.callRemote("SERVER:updateRentCarBill", totalBill, totalTime);
        }, interval * 1000);
    });

    mp.events.add('abortRentcarTimer', () => {
        clearInterval(payTimer);
        totalBill = 0;
        totalTime = 0;
    });
}