/**
 * @overview Life of German Reallife - Gui Infobox infobox.js
 * @author VegaZ
 * @copyright (c) 2008 - 2018 Life of German
 */

export default function deathScreen() {

    var playerName;
    var playerId;
    var playerMoney;
    var dutyMedics = 0;

    var isDeath = false;
    var deathTime;
    var respawnTime;
    var deathSeconds;
    var fade;
    var deathDate: Date;

    mp.game.gameplay.setFadeOutAfterDeath(false);

    mp.events.add("startDeathTimer", (isAdmin) => {
        if (isDeath === false) {
            isDeath = true;
            if (isAdmin) {
                mp.gui.chat.activate(true);
            }
            else {
                mp.gui.chat.activate(false);
            }
            mp.game.audio.playSoundFrontend(-1, "Bed", "WastedSounds", true);
            deathDate = new Date();
            respawnTime = Math.floor(deathDate.getTime() / 1000 + 120);
            fade = 255 - 120;
            mp.game.graphics.requestStreamedTextureDict("Mptattoos", true);
        }
    });

    mp.events.add("onPlayerRevived", () => {
        isDeath = false;
        mp.gui.chat.activate(true);
        mp.game.gameplay.setFadeOutAfterDeath(false);
        mp.game.graphics.setStreamedTextureDictAsNoLongerNeeded("Mptattoos");
    });

    mp.events.add("respawnDeathPlayer", () => {
        isDeath = false;
        mp.gui.chat.activate(true);
        mp.game.gameplay.setFadeOutAfterDeath(false);
        mp.events.callRemote('RespawnPlayerAtHospital');
        mp.game.graphics.setStreamedTextureDictAsNoLongerNeeded("Mptattoos");
    });

    mp.events.add("updateDutyMedics", (count) => {
        dutyMedics = count;
    });



    mp.events.add("render", () => {
        var currentDate = new Date();

        if (isDeath === true) {

            var medicString;
            if (dutyMedics > 0) {
                medicString = "Derzeit ";
                if (dutyMedics === 1) {
                    medicString += "ist ~g~" + dutyMedics + " Medic";
                } else {
                    medicString = "sind ~g~" + dutyMedics + " Medics";
                }
                medicString += " ~s~im Dienst ~c~und versuchen dich wiederzubeleben...";
            } else {
                medicString = "Derzeit sind ~r~keine Medics ~s~im Dienst.";
            }

            deathSeconds = respawnTime - Math.floor(currentDate.getTime() / 1000);
            var alpha = fade + Math.floor(currentDate.getTime() / 1000 - deathDate.getTime() / 1000);
            if (deathSeconds >= 0) {
                mp.game.graphics.set2dLayer(2);
                mp.game.graphics.drawSprite("Mptattoos", "clearout", 0.625, 0.52, 0.1, 0.1, 0, 255, 255, 255, 236);
                mp.game.graphics.drawText("Respawn in: ~y~" + deathSeconds, [0.5, 0.5],
                    {
                        font: 7,
                        color: [255, 255, 255, 255],
                        scale: [0.8, 0.8],
                        outline: true,
                        centre: false
                    });
                mp.game.graphics.drawText(medicString, [0.5, 0.975],
                    {
                        font: 4,
                        color: [255, 255, 255, 255],
                        scale: [0.4, 0.4],
                        outline: true,
                        centre: false
                    });
                mp.game.graphics.set2dLayer(1);
                mp.game.graphics.drawRect(0.5, 0.5, 1, 1, 0, 0, 0, alpha);

            } else {
                mp.events.call("respawnDeathPlayer");
            }
        }
    });
}