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;

const { x: screenX, y: screenY } = mp.game.graphics.getScreenActiveResolution(0, 0);
let rxC = 0.5;
let ryC = 0.4;
let rWidth = 0.4;
let sX = (screenX * 0.1 / 1000);
let sY = (screenY * 0.1 / 1000);
let eX = sX - 0.1;
let eY = sY - 0.1;
let sizeMul = 0.08;
let yMul = 1.8;

export default function playerInteraction(globalData: GlobalData) {

    var nearbyPlayers = [];
    let screenRes = mp.game.graphics.getScreenResolution(0, 0);
    var tradeRequest;
    var tradeRequester;
    var timerBarSize = 0.5;
    var tradeTimeLeft;
    var tradeTime;
    var tradeTimeEnd;
    var tradeSelection = "none";
    var tradeSelectHover = "none";
    var acceptColor = 100;
    var declineColor = 100;

    var hoverItem;

    var tempSlot2;

    var receivedTradePrize;
    var receivedTradeItems = [[]];
    var receivedTradeGrid = [];
    receivedTradeGrid[0] = [0.3375, 0.5]
    receivedTradeGrid[1] = [0.4195, 0.5]
    receivedTradeGrid[2] = [0.5015, 0.5]
    receivedTradeGrid[3] = [0.5835, 0.5]
    receivedTradeGrid[4] = [0.6655, 0.5]

    mp.events.add("showPlayerInteraction", (nearPlayerArr) => {
        mp.gui.chat.activate(false);
        nearbyPlayers = JSON.parse(nearPlayerArr);
        globalData.InMenu = true;
        let playerInteractionMenu = new Menu("Spielerinteraktion", "", new Point(0, screenRes.y / 2), null, null);
        let playerSelect = new UIMenuListItem("Mit", "~y~W\u00e4hle den Spieler aus.", new ItemsCollection(nearbyPlayers));
        playerInteractionMenu.AddItem(playerSelect);
        playerInteractionMenu.AddItem(new UIMenuItem("Handeln", "Sende dem Spieler eine Handlungsanfrage"));

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

        playerInteractionMenu.Visible = true;

        playerInteractionMenu.ItemSelect.on((item) => {
            if (item.Text === "Handeln") {
                mp.events.callRemote("openTradeInventory", playerSelect.SelectedValue);
                playerInteractionMenu.Visible = false;
                mp.gui.chat.activate(true);
                globalData.InMenu = false;
            } else if (item.Text === "Schlie\u00dfen") {
                playerInteractionMenu.Visible = false;
                mp.gui.chat.activate(true);
                globalData.InMenu = false;
            }
        });

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

    mp.events.add("showTradeRequest", (tradeRequesterName, tradeItemsArr, tradePrizeSent) => {
        receivedTradeItems = tradeItemsArr;
        receivedTradePrize = tradePrizeSent;
        mp.gui.cursor.show(true, true);
        tradeRequester = tradeRequesterName;
        tradeRequest = true;
        tradeTime = new Date().getTime();
        tradeTimeEnd = tradeTime + 30000;
    });

    mp.events.add("render", () => {
        if (tradeRequest) {
            var pos = mp.gui.cursor.position;
            var actualTime = new Date().getTime();

            tradeTimeLeft = (tradeTimeEnd - actualTime) / 1000;

            mp.game.graphics.set2dLayer(1);
            mp.game.graphics.drawRect(0.5, 0.5, 0.5, 0.3, 255, 255, 255, 200);

            mp.game.graphics.drawText("Handelsanfrage von ~y~" + tradeRequester + " ~s~ für ~g~" + receivedTradePrize + "$", [0.5, 0.5 - 0.13], {
                font: 7,
                color: [112, 128, 144, 254],
                scale: [0.7, 0.7],
                outline: true,
                centre: false
            });

            mp.game.graphics.set2dLayer(2);
            for (var slots = 0; slots < receivedTradeGrid.length; slots++) {
                mp.game.graphics.drawRect(receivedTradeGrid[slots][0], receivedTradeGrid[slots][1], sizeMul, sizeMul * yMul, 112, 128, 144, 254);
                mp.game.graphics.drawText((tempSlot2 = slots + 1).toString(), [receivedTradeGrid[slots][0] - (sizeMul / 2) + 0.006, receivedTradeGrid[slots][1] - (sizeMul / 2 * yMul)], {
                    font: 7,
                    color: [69, 255, 0, 254],
                    scale: [0.3, 0.3],
                    outline: true,
                    centre: false
                });
            }

            mp.game.graphics.set2dLayer(3);
            //SPRITES + ITEMANZAHL
            if (receivedTradeItems.length !== 0) {
                for (var currentItem = 0; currentItem < receivedTradeItems.length; currentItem++) {
                    if (receivedTradeItems[currentItem][4] !== "-1") {
                        mp.game.graphics.drawSprite("itemimages", receivedTradeItems[currentItem][0].toLowerCase(), receivedTradeGrid[receivedTradeItems[currentItem][4] - 1][0], receivedTradeGrid[receivedTradeItems[currentItem][4] - 1][1], rWidth / 8, rWidth / 5, 0, 255, 255, 255, 255);
                        mp.game.graphics.drawText("(~y~" + receivedTradeItems[currentItem][3] + "~s~)", [receivedTradeGrid[receivedTradeItems[currentItem][4] - 1][0] + (sizeMul / 2) - 0.008, receivedTradeGrid[receivedTradeItems[currentItem][4] - 1][1] + 0.05], {
                            font: 0,
                            color: [255, 255, 255, 255],
                            scale: [0.25, 0.25],
                            outline: true,
                            centre: false
                        });
                    }
                }
            }
            mp.game.graphics.set2dLayer(4);
            isMouseOverButton(pos[0], pos[1]);
            mp.game.graphics.drawRect(0.5 - 0.07, 0.5 + 0.1, 0.06, 0.04, 100, acceptColor, 100, 250);
            mp.game.graphics.drawRect(0.5 + 0.07, 0.5 + 0.1, 0.06, 0.04, declineColor, 100, 100, 250);

            mp.game.graphics.set2dLayer(5);
            mp.game.graphics.drawText("~g~Annehmen", [0.5 - 0.07, 0.5 + 0.08], {
                font: 4,
                color: [112, 128, 144, 254],
                scale: [0.6, 0.6],
                outline: true,
                centre: false
            });
            mp.game.graphics.drawText("~r~Ablehnen", [0.5 + 0.07, 0.5 + 0.08], {
                font: 4,
                color: [112, 128, 144, 254],
                scale: [0.6, 0.6],
                outline: true,
                centre: false
            });

            mp.game.graphics.set2dLayer(6);
            if (isMouseOverTradeItem(pos[0], pos[1])) {
                mp.game.graphics.drawRect(pos[0] / screenX, pos[1] / screenY + 0.055, 0.14, 0.08, 0, 0, 0, 220);

                var hoverItemP = [receivedTradeItems[hoverItem][0], receivedTradeItems[hoverItem][1], receivedTradeItems[hoverItem][2], receivedTradeItems[hoverItem][3]];

                //NAME
                mp.game.graphics.drawText("~g~" + hoverItemP[0], [pos[0] / screenX, pos[1] / screenY + 0.02], {
                    font: 4,
                    color: [255, 255, 255, 255],
                    scale: [0.4, 0.4],
                    outline: true,
                    centre: false
                });

                //BESCHREIBUNG
                mp.game.graphics.drawText(hoverItemP[1], [pos[0] / screenX, pos[1] / screenY + 0.04], {
                    font: 4,
                    color: [255, 255, 255, 255],
                    scale: [0.35, 0.35],
                    outline: true,
                    centre: false
                });

                //EINZELGEWICHT
                mp.game.graphics.drawText("~y~" + hoverItemP[2] + "g~s~ * " + hoverItemP[3] + "~s~ = ~y~" + hoverItemP[2] * hoverItemP[3] + "g", [pos[0] / screenX, pos[1] / screenY + 0.06], {
                    font: 4,
                    color: [255, 255, 255, 255],
                    scale: [0.35, 0.35],
                    outline: true,
                    centre: false
                });
            }

            mp.game.graphics.set2dLayer(7);

            timerBarSize = tradeTimeLeft / 30 * 0.5;
            if (tradeTimeLeft > 0) {
                mp.game.graphics.drawText(tradeTimeLeft.toFixed(0), [0.5, 0.5 + 0.1312], {
                    font: 7,
                    color: [255, 255, 255, 255],
                    scale: [0.35, 0.35],
                    outline: true,
                    centre: false
                });
                mp.game.graphics.drawRect(0.5, 0.5 + 0.1412, timerBarSize, 0.018, 255, 0, 0, 250);
            } else {
                tradeSelection = "timeup";
                mp.events.callRemote('tradeDecision', tradeSelection, JSON.stringify(receivedTradeItems), tradeRequester);
                tradeRequest = false;
                mp.gui.cursor.show(false, false);
            }
        }
    });
    function isMouseOverButton(cX, cY) {

        if (tradeRequest) {
            var x = cX / screenX;
            var y = cY / screenY;

            if (x > 0.5 - 0.07 - 0.03 && x < 0.5 - 0.07 + 0.03 && y > 0.5 + 0.1 - 0.02 && y < 0.5 + 0.1 + 0.02) {
                acceptColor = 255;
                tradeSelectHover = "accept";
                return true;
            } else if (x > 0.5 + 0.07 - 0.03 && x < 0.5 + 0.07 + 0.03 && y > 0.5 + 0.1 - 0.02 && y < 0.5 + 0.1 + 0.02) {
                declineColor = 255;
                tradeSelectHover = "decline";
                return true;
            } else {
                acceptColor = 100;
                declineColor = 100;
                return false;
            }
        }
    }

    function isMouseOverTradeItem(cX, cY) {

        if (tradeRequest) {

            var x = cX / screenX;
            var y = cY / screenY;
            var a;
            var b;
            var c;

            for (var s = 0; s < 5; s++) {
                a = receivedTradeGrid[s][0] - x;
                b = receivedTradeGrid[s][1] - y;
                c = Math.sqrt(a * a + b * b);

                var invSlot = (s + 1);

                if (c < sizeMul / 2) {
                    for (var i = 0; i < receivedTradeItems.length; i++) {
                        if (parseInt(receivedTradeItems[i][4]) === invSlot) {
                            hoverItem = i;
                            return true;
                        }
                    }
                }
            }
        }
    }

    mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, worldPosition, hitEntity) => {

        var cX = x / screenX;
        var cY = y / screenY;

        if (tradeRequest) {
            //LINKE MAUSTASTE
            //RUNTER
            if (upOrDown === "down" && leftOrRight === "left") {
                if (isMouseOverButton(x, y)) {
                    if (tradeSelectHover === "accept") {
                        tradeSelection = "accept";
                    } else if (tradeSelectHover === "decline") {
                        tradeSelection = "decline";
                    }
                    mp.events.callRemote('tradeDecision', tradeSelection, JSON.stringify(receivedTradeItems), tradeRequester, receivedTradePrize);
                    tradeRequest = false;
                    receivedTradePrize = null;
                    mp.gui.cursor.show(false, false);
                }
            }
        }
    });
}