import * as NativeUI from 'NativeUI';
const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const UIMenuCheckboxItem = NativeUI.UIMenuCheckboxItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;

export default function factionInteraction(globalData: GlobalData) {
    var screenRes = mp.game.graphics.getScreenResolution(0, 0);
    var player = mp.players.local;
    var tasks;
    var initTasks;
    var newTasks;
    var sorting = 0;
    var firstSorting = true;
    var activeTask = false;
    var activeCheckpoint;
    var taskStart;
    var taskFinish;
    var taskRange;
    var ambulanceImagePos;
    var rangeLeft;
    var sortText;

    var factionInteractionMenu;
    var reviveTaskMenu;

    mp.events.add("updateFactionBlips", (type, taskList) => {

    });

    mp.events.add("showFactionInteraction", (userFactionId, isDuty, userFactionName, isFactionLeader, reviveTaskCount, healTaskCount, fireTaskCount) => {
        mp.gui.chat.activate(false);
        globalData.InMenu = true;

        var rP = ((reviveTaskCount === "0") ? "~r~" : "~g~");
        var hP = ((healTaskCount === "0") ? "~r~" : "~g~");
        var fP = ((fireTaskCount === "0") ? "~r~" : "~g~");

        factionInteractionMenu = new Menu("Fraktionsinteraktion", userFactionName, new Point(0, screenRes.y / 2), null, null);

        if (isFactionLeader) {
            let leaderMenu = new UIMenuItem("Leadermen\u00fc", "Verwaltung der Fraktion");
            leaderMenu.SetRightBadge(BadgeStyle.Star);
            factionInteractionMenu.AddItem(leaderMenu);
        }
        let reviveTaskMenu;
        let healTaskMenu;
        let fireTaskMenu;
        if (isDuty) {
            switch (userFactionId) {
                case 2:
                    reviveTaskMenu = new UIMenuItem("Reviveauftr\u00e4ge");
                    reviveTaskMenu.SetRightLabel(rP + reviveTaskCount)
                    factionInteractionMenu.AddItem(reviveTaskMenu);

                    healTaskMenu = new UIMenuItem("Healauftr\u00e4ge");
                    healTaskMenu.SetRightLabel(hP + healTaskCount)
                    factionInteractionMenu.AddItem(healTaskMenu);

                    fireTaskMenu = new UIMenuItem("Feuerauftr\u00e4ge");
                    fireTaskMenu.SetRightLabel(fP + fireTaskCount)
                    factionInteractionMenu.AddItem(fireTaskMenu);
                    break;
            }
        }

        let cancelItem = new UIMenuItem("Schlie\u00dfen", "Schlie\u00dft die Fraktionsinteraktion");
        cancelItem.BackColor = new Color(213, 0, 0);
        cancelItem.HighlightedBackColor = new Color(229, 57, 53);
        factionInteractionMenu.AddItem(cancelItem);

        factionInteractionMenu.Visible = true;

        factionInteractionMenu.ItemSelect.on((item) => {
            switch (item) {
                case cancelItem:
                    factionInteractionMenu.Visible = false;
                    mp.gui.chat.activate(true);
                    globalData.InMenu = false;
                    break;
                case reviveTaskMenu:
                    mp.events.callRemote("loadMedicTasks", 0)
                    factionInteractionMenu.Visible = false;
                    mp.gui.chat.activate(true);
                    globalData.InMenu = false;
                    break;
            }
        });

        factionInteractionMenu.MenuClose.on(() => {
            mp.gui.chat.activate(true);
            globalData.InMenu = false;
        });
    });


    mp.events.add("showMedicTasks", (type, taskList) => {
        tasks = JSON.parse(taskList);
        if (sorting === 0) {
            initTasks = tasks;
        }

        mp.events.call("sortFactionTasks", false);

        mp.gui.chat.activate(false);
        globalData.InMenu = true;
        switch (type) {
            case 0:
                reviveTaskMenu = new Menu("Revives", "\u00c4lteste zu erst", new Point(0, screenRes.y / 2), null, null);
                let aTask;
                for (var i = 0; i < tasks.length; i++) {
                    if (tasks[i].MedicName === "none") {
                        aTask = new UIMenuItem("~g~" + tasks[i].Victim);
                    } else {
                        aTask = new UIMenuItem("~r~" + tasks[i].Victim);
                    }

                    aTask.SetRightLabel(mp.game.gameplay.getDistanceBetweenCoords(player.position.x, player.position.y, player.position.z, tasks[i].Position.x, tasks[i].Position.y, tasks[i].Position.z, true).toFixed(2).toString() + " ~g~m");
                    reviveTaskMenu.AddItem(aTask);
                }

                let backItem = new UIMenuItem("Zur\u00fcck", "Zur\u00fcck zur Fraktionsinteraktion");
                backItem.BackColor = new Color(213, 0, 0);
                backItem.HighlightedBackColor = new Color(229, 57, 53);
                reviveTaskMenu.AddItem(backItem);

                reviveTaskMenu.ItemSelect.on((item, index) => {
                    if (item === backItem) {
                        reviveTaskMenu.Visible = false;
                        factionInteractionMenu.Visible = true;
                    } else {
                        if (tasks[index].MedicName === "none") {
                            mp.game.graphics.requestStreamedTextureDict("medicimages", true);
                            mp.events.callRemote("updateMedicTask", 0, index, player.name);
                            mp.game.ui.setNewWaypoint(tasks[index].Position.x, tasks[index].Position.y);
                            activeCheckpoint = mp.markers.new(1, new mp.Vector3(tasks[index].Position.x, tasks[index].Position.y, tasks[index].Position.z - 2), 3.0, {
                                color: [255, 0, 0, 150],
                                visible: true,
                                dimension: 0
                            });
                            reviveTaskMenu.Visible = false;
                            mp.gui.chat.activate(true);
                            globalData.InMenu = false;
                            ambulanceImagePos = 0.325
                            taskStart = player.position;
                            taskFinish = tasks[index].Position;
                            taskRange = mp.game.gameplay.getDistanceBetweenCoords(player.position.x, player.position.y, player.position.z, tasks[index].Position.x, tasks[index].Position.y, tasks[index].Position.z, true);
                            activeTask = true;
                        }
                    }
                });

                reviveTaskMenu.MenuClose.on(() => {
                    reviveTaskMenu.Visible = false;
                    factionInteractionMenu.Visible = true;
                });
                break;
            case 1:
                break;
            case 2:
                break;
        }
    });

    mp.events.add("sortFactionTasks", (sortByKey) => {
        if (firstSorting) {
            sortText = "Nach Uhrzeit";
            firstSorting = false;
            //mp.gui.chat.push("Init Sort");
            return;
        } else {
            if (sortByKey) {
                if (sorting < 1) {
                    sorting++;
                } else {
                    sorting = 0;
                }

                switch (sorting) {
                    case 0: //Standartsortierung

                        reviveTaskMenu.Close();
                        factionInteractionMenu.Close();
                        sortText = "Nach Uhrzeit";
                        mp.events.call("showMedicTasks", 0, JSON.stringify(initTasks));
                        break;

                    case 1: //Sortierung nach Metern (aufsteigend)                    
                        for (var d = 0; d < tasks.length; d++) {
                            for (var e = 0; e < tasks.length - 1; e++) {
                                if (getDistance1(e) > getDistance2(e + 1)) {
                                    var tempTask = tasks[e];
                                    tasks[e] = tasks[e + 1];
                                    tasks[e + 1] = tempTask;
                                    mp.gui.chat.push("SWITCH");
                                }
                            }
                        }
                        reviveTaskMenu.Close();
                        factionInteractionMenu.Close();
                        sortText = "Entfernung aufsteigend";
                        mp.events.call("showMedicTasks", 0, JSON.stringify(tasks));
                        break;

                    //case 2: //Sortierung nach Metern (absteigend)   
                    //    mp.gui.chat.push("Sorting 2");
                    //    sortText = "Entfernung absteigend";
                    //    break;
                    //case 3: //Sortierung nach Zeit (aufsteigend)
                    //    mp.gui.chat.push("Sorting 3");
                    //    sortText = "Restzeit aufsteigend";
                    //    break;
                    //case 4: //Sortierung nach Zeit (absteigend)
                    //    mp.gui.chat.push("Sorting 4");
                    //    sortText = "Restzeit absteigend";
                    //    break;
                }
            }
        }
    });

    function getDistance1(index) {
        return mp.game.gameplay.getDistanceBetweenCoords(player.position.x, player.position.y, player.position.z, newTasks[index].Position.x, newTasks[index].Position.y, newTasks[index].Position.z, true).toFixed(2);
    }
    function getDistance2(index) {
        return mp.game.gameplay.getDistanceBetweenCoords(player.position.x, player.position.y, player.position.z, newTasks[index].Position.x, newTasks[index].Position.y, newTasks[index].Position.z, true).toFixed(2);
    }

    mp.events.add('render', () => {
        if (activeTask) {
            rangeLeft = mp.game.gameplay.getDistanceBetweenCoords(player.position.x, player.position.y, player.position.z, taskFinish.x, taskFinish.y, taskFinish.z, true).toFixed(2);
            if (rangeLeft > 1.9) {
                ambulanceImagePos = 0.655 - (mp.game.gameplay.getDistanceBetweenCoords(player.position.x, player.position.y, player.position.z, taskFinish.x, taskFinish.y, taskFinish.z, true) / taskRange * 0.35);
                mp.game.graphics.drawRect(0.5, 0.945, 0.35, 0.025, 150, 0, 0, 200);
                mp.game.graphics.drawText(rangeLeft.toString() + "m", [0.5, 0.93], {
                    font: 0,
                    color: [255, 255, 255, 255],
                    scale: [0.35, 0.35],
                    outline: true,
                    centre: true
                });
                if (taskRange < rangeLeft) ambulanceImagePos = 0.3;
                mp.game.graphics.drawSprite("medicimages", "finish", 0.655, 0.898, 0.04, 0.07, 0, 255, 255, 255, 255);
                if (player.isInAnyVehicle(false)) {
                    mp.game.graphics.drawSprite("medicimages", "ambulance", ambulanceImagePos + 0.01, 0.915, 0.04, 0.07, 0, 255, 255, 255, 255);
                } else {
                    mp.game.graphics.drawSprite("medicimages", "running", ambulanceImagePos + 0.02, 0.915, 0.02, 0.035, 0, 255, 0, 0, 255);
                }
            } else {
                activeTask = false;
                activeCheckpoint.destroy();
            }
        }
    });
}