import { IGame, IEntity } from "../game";
import game from "..";


const maxDistance = 25 * 25;
const width = 0.03;
const height = 0.0065;
const border = 0.001;

const alpha = 250;

const colors = [
    { id: -2, color: [0, 255, 255, alpha] }, //support
    { id: -1, color: [255, 100, 0, alpha] }, //wanted

    { id: 0, color: [255, 255, 255, alpha] }, //civil

    //1 to n
    { id: 1, color: [0, 95, 190, alpha] }, //pd
    { id: 2, color: [200, 0, 0, alpha] }, //lsed
    { id: 3, color: [0, 0, 170, alpha] }, //fib
    { id: 4, color: [255, 180, 0, alpha] }, //trucker
    { id: 5, color: [255, 255, 255, alpha] }, //terroristen
    { id: 6, color: [255, 255, 255, alpha] }, //hitman
    { id: 7, color: [0, 54, 0, alpha] }, //grove
    { id: 8, color: [171, 0, 207, alpha] }, //ballas
    { id: 9, color: [0, 166, 133, alpha] }, //news
];


export default function customNametags() {

    mp.nametags.enabled = false;

    mp.events.add('render', (nametags) => {
        const graphics = mp.game.graphics;
        const screenRes = graphics.getScreenResolution(0, 0);

        nametags.forEach(nametag => {
            let [player, x, y, distance] = nametag;

            if (distance <= maxDistance) {
                let scale = distance / maxDistance;
                if (scale < 0.6) scale = 0.6;

                var health = player.getHealth() / 100;
                //health = health < 100 ? 0 : (health - 100) / 100;

                var armour = player.getArmour() / 100;

                y -= scale * (0.005 * (screenRes.y / 1080));

                let colorId = game.players.at(player.remoteId).nametagColor;

                let color = colors.find(c => c.id === colorId).color;

                mp.game.graphics.drawText(player.name + " (" + player.remoteId + ")", [x, y],
                    {
                        font: 4,
                        color: [color[0], color[1], color[2], color[3]],
                        scale: [0.4, 0.4],
                        outline: true,
                        centre: false
                    });

                if (true /*mp.game.player.isPlayerFreeAimingAtEntity(player.handle)*/) {
                    let y2 = y + 0.042;

                    if (armour > 0) {
                        graphics.drawRect(x, y2, width + border * 2, height + border * 2, 0 , 0, 0, 200);
                        graphics.drawRect(x, y2, width, height, 150, 150, 150, 255);
                        graphics.drawRect(x - width / 2 * (1 - health), y2, width * health, height, 200, 0, 0, 200);

                        var x2 = x + width / 2 + border / 2;

                        graphics.drawRect(x, y2 + height, width + border * 2, height + border * 2, 0, 0, 0, 200);
                        graphics.drawRect(x, y2 + height, width, height, 200, 200, 200, 255);
                        graphics.drawRect(x - width / 2 * (1 - armour), y2 + height, width * armour, height, 255, 255, 255, 200);
                    }
                    else {
                        graphics.drawRect(x, y2, width + border * 2, height + border * 2, 0, 0, 0, 200);
                        graphics.drawRect(x, y2, width, height, 150, 150, 150, 255);
                        graphics.drawRect(x - width / 2 * (1 - health), y2, width * health, height, 200, 0, 0, 200);
                    }
                }
            }
        });
    });
}