/**
 * @overview Life of German Reallife - Tuning tuning.js
 * @author hydrant
 * @copyright (c) 2008 - 2018 Life of German
 */

import * as NativeUI from 'NativeUI';
const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;

export default function tuning(globalData: GlobalData) {
    var keyBound = false;

    const disableInput = [75, 278, 279, 280, 281, 23, 59, 60, 71, 72, 74];

    const carModTypes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 22, 25, 27, 28, 33, 34, 35, 38, 39, 40, 41, 42, 43, 44, 46, 48, 69];

    const carModSlotName = [
        { Slot: 0, Name: "Spoiler" },
        { Slot: 1, Name: "Frontstoßstange" },
        { Slot: 2, Name: "Heckstoßstange" },
        { Slot: 3, Name: "Seitenschweller" },
        { Slot: 4, Name: "Auspuff" },
        { Slot: 5, Name: "Rahmen" },
        { Slot: 6, Name: "Kühlergrill" },
        { Slot: 7, Name: "Motorhaube" },
        { Slot: 8, Name: "Extra 1" },
        { Slot: 9, Name: "Extra 2" },
        { Slot: 10, Name: "Dach" },
        { Slot: 11, Name: "Motor" },
        { Slot: 12, Name: "Bremsen" },
        { Slot: 13, Name: "Getriebe" },
        { Slot: 14, Name: "Hupe" },
        { Slot: 15, Name: "Federung" },
        { Slot: 18, Name: "Turbo" },
        { Slot: 22, Name: "Licht" },
        { Slot: 23, Name: "Reifen" },
        { Slot: 25, Name: "Nummernschildhalter" },
        { Slot: 27, Name: "Innenausstatung" },
        { Slot: 28, Name: "Wackelkopf" },
        { Slot: 33, Name: "Lenkrad" },
        { Slot: 34, Name: "Schalthebel" },
        { Slot: 35, Name: "Schild" },
        { Slot: 38, Name: "Hydraulik" },
        { Slot: 39, Name: "Motorabdeckung" },
        { Slot: 40, Name: "Luftfilter" },
        { Slot: 46, Name: "Fenster" },
        { Slot: 48, Name: "Design" }
    ];

    const customPartNames = [
        {
            Model: 1093792632,
            Names: [
                {
                    Slot: 8,
                    Name: "Lüftungsschlitze"
                },
                {
                    Slot: 43,
                    Name: "Motorhaubenstifte"
                }
            ]
        },
        {
            Model: 223240013,
            Names: [
                {
                    Slot: 9,
                    Name: "Heckkotflügel"
                }
            ]
        },
        {
            Model: 1031562256,
            Names: [
                {
                    Slot: 5,
                    Name: "Stoßdämpfer"
                }
            ]
        },
        {
            Model: 3308022675,
            Names: [
                {
                    Slot: 9,
                    Name: "Kotflügel"
                }
            ]
        }
    ];

    const doorOpeners = [
        {
            Model: 3308022675,
            Doors: [
                {
                    Slot: 5,
                    Door: 4
                }
            ]
        }
    ];

    var mainMenu: NativeUI.Menu;

    mp.events.add('showTuningInfo', () => {
        mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um dein Fahrzeug zu modifizieren');
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);

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

    mp.events.add('hideTuningInfo', (unbind) => {
        mp.game.ui.clearHelp(true);
        mp.gui.chat.show(true);

        if (mp.players.local.vehicle) {
            mp.players.local.vehicle.setLights(0);
        }

        if (typeof mainMenu !== "undefined" && unbind) {
            mainMenu.Close();
            globalData.InTuning = false;
        }

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

    function keyPressHandler() {
        mp.events.callRemote("startPlayerTuning");
    }

    mp.events.add("render", () => {
        if (globalData.InTuning) {
            disableInput.forEach((input) => {
                mp.game.controls.disableControlAction(1, input, true);
            });
        }
    });

    mp.events.add("showTuningMenu", () => {
        mp.events.call("hideTuningInfo", false);
        mp.gui.chat.show(false);

        var localPlayer = mp.players.local;
        var localVehicle = localPlayer.vehicle;

        localVehicle.setHalt(1.0, 1, false);

        globalData.InMenu = true;
        globalData.InTuning = true;

        localVehicle.setLights(1);
        var carName = mp.game.ui.getLabelText(mp.game.vehicle.getDisplayNameFromVehicleModel(localVehicle.model));

        mainMenu = new Menu("Fahrzeugwerkstatt", carName, new Point(50, 50), null, null);

        var currentMod = new Array<number>();
        var currentActiveModItem = new Array<NativeUI.UIMenuItem>();

        carModTypes.forEach((modType) => {

            var num = localVehicle.getNumMods(modType);

            if (num === 0 && modType !== 18 && modType !== 22) return;

            var modName = localVehicle.getModSlotName(modType) || "nix gefunden";
            modName = mp.game.ui.getLabelText(modName);

            if (modName === "NULL") {
                for (var x = 0; x < carModSlotName.length; x++) {
                    var tmpSlot = carModSlotName[x];
                    if (tmpSlot.Slot == modType) {
                        modName = tmpSlot.Name;
                        break;
                    }
                }
            }

            var modMenu = new Menu(modName, carName, new Point(50, 50), null, null);

            var modNum = localVehicle.getNumMods(modType);
            if (modType === 18) modNum = 1;
            if (modType === 22) modNum = 14;
            
            var mod = localVehicle.getMod(modType);

            if (modType === 22) {
                var hlColor = localVehicle.getVariable("headlightColor");
                if (typeof hlColor !== "number" || isNaN(hlColor) || hlColor < 0 || hlColor === 255) {
                    mod = -1;
                } else if (hlColor === 13) mod = 0;
                else mod = hlColor + 1;
            }

            if (modType === 18) {
                var toggle = localVehicle.isToggleModOn(modType);
                mod = toggle ? 0 : -1;
            }

            currentMod[modType] = mod;

            for (var x = -1; x < modNum; x++) {
                var modText = "";
                if (x === -1) {
                    modText = "Serie";
                } else {
                    modText = mp.game.ui.getLabelText(localVehicle.getModTextLabel(modType, x));
                    if (modText === "NULL") modText = getModName(localVehicle, modType, x);
                }
                var item = new UIMenuItem(modText);
                modMenu.AddItem(item);

                if (x === mod) {
                    item.SetRightBadge(BadgeStyle.Car);
                    currentActiveModItem[modType] = item;
                    modMenu.CurrentSelection = x;
                }
            }

            modMenu.IndexChange.on((index: number) => {
                if (modType === 18) return;
                if (modType === 22) {
                    setHeadlightsColor(localVehicle, index);
                    return;
                }
                localVehicle.setMod(modType, index - 1);
            });

            modMenu.MenuOpen.on(() => {
                if (modType === 22) {
                    localVehicle.setLights(2);
                }
            });

            modMenu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => {
                currentMod[modType] = index - 1;
                currentActiveModItem[modType].SetRightBadge(BadgeStyle.None);
                item.SetRightBadge(BadgeStyle.Car);
                currentActiveModItem[modType] = item;

                mp.events.callRemote("setVehicleMod", modType, index);
            });

            modMenu.MenuClose.on(() => {
                if (modType === 18) return;
                if (modType === 22) {
                    localVehicle.setLights(1);
                    setHeadlightsColor(localVehicle, currentMod[22] + 1);
                    return;
                }
                localVehicle.setMod(modType, currentMod[modType]);
            });

            modMenu.Visible = false;

            var menuItem = new UIMenuItem(modName);
            mainMenu.AddItem(menuItem);
            mainMenu.BindMenuToItem(modMenu, menuItem);
        });

        mainMenu.Visible = true;

        mainMenu.MenuClose.on(() => {
            localVehicle.setLights(0);
            globalData.InTuning = false;
            globalData.InMenu = false;
            mp.events.call("hideTuningInfo", false);
        });

    });

    mp.events.add("playerLeaveVehicle", () => {
        if (keyBound) {
            mp.events.call("hideTuningInfo", true);
        }
    });

    function getModName(vehicle: VehicleMp, slot: number, mod: number) {
        var modName = vehicle.getModTextLabel(slot, mod);
        var realModName = mp.game.ui.getLabelText(modName);

        if (slot === 11) {
            switch (mod) {
                case 0:
                    realModName = "EMS Verbesserung 1";
                    break;
                case 1:
                    realModName = "EMS Verbesserung 2";
                    break;
                case 2:
                    realModName = "EMS Verbesserung 3";
                    break;
                case 3:
                    realModName = "EMS Verbesserung 4";
                    break;
            }
        } else if (slot === 12) {
            switch (mod) {
                case 0:
                    realModName = "Straßenbremsen";
                    break;
                case 1:
                    realModName = "Sportbremsen";
                    break;
                case 2:
                    realModName = "Rennbremsen";
                    break;
            }
        } else if (slot === 13) {
            switch (mod) {
                case 0:
                    realModName = "Straßengetriebe";
                    break;
                case 1:
                    realModName = "Sportgetriebe";
                    break;
                case 2:
                    realModName = "Renngetriebe";
                    break;
                case 3:
                    realModName = "Wettkampfgetriebe";
            }
        } else if (slot === 15) {
            switch (mod) {
                case 0:
                    realModName = "Tiefere Federung";
                    break;
                case 1:
                    realModName = "Straßenfederung";
                    break;
                case 2:
                    realModName = "Sportfederung";
                    break;
                case 3:
                    realModName = "Rennfederung";
                    break;
                case 4:
                    realModName = "Wettkampffederung";
                    break;
            }
        } else if (slot === 18) {
            realModName = "Turbotuning";
        } else if (slot === 22) {
            switch (mod) {
                case 0:
                    realModName = "Xenon Licht";
                    break;
                case 1:
                    realModName = "Weißes Licht";
                    break;
                case 2:
                    realModName = "Blaues Licht";
                    break;
                case 3:
                    realModName = "Hell-blaues Licht";
                    break;
                case 4:
                    realModName = "Grünes Licht";
                    break;
                case 5:
                    realModName = "Hell-grünes Licht";
                    break;
                case 6:
                    realModName = "Hell-gelbes Licht";
                    break;
                case 7:
                    realModName = "Gelbes Licht";
                    break;
                case 8:
                    realModName = "Orangenes Licht";
                    break;
                case 9:
                    realModName = "Rotes Licht";
                    break;
                case 10:
                    realModName = "Hell-pinkes Licht";
                    break;
                case 11:
                    realModName = "Pinkes Licht";
                    break;
                case 12:
                    realModName = "Lila Licht";
                    break;
                case 13:
                    realModName = "Schwarzlicht";
                    break;
            }
        }

        return realModName;
    }

    function setHeadlightsColor(vehicle, index) {
        var color = index - 2;
        if (index === 0) color = -1;
        if (index === 1) color = 13;
        if (typeof color !== "number" || isNaN(color) || color < 0 || color === 255) {
            // Disable
            vehicle.toggleMod(22, false);
            mp.game.invoke("0xE41033B25D003A07", vehicle.handle, 255);
        } else {
            // Enable
            vehicle.toggleMod(22, true);
            mp.game.invoke("0xE41033B25D003A07", vehicle.handle, color);
        }
    }
}