/**
 * @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;
    var taskTimeout = false;
    const maxDeathTime = 120;
    const medicJobTime = maxDeathTime * -1;

    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 + maxDeathTime);
            fade = 255 - maxDeathTime;
            mp.game.graphics.requestStreamedTextureDict("Mptattoos", true);          
            mp.events.call("GetDutyMedics");
            taskTimeout = false;
        }
    });

    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 = "Das L.S.E.D. wurde benachrichtigt\nDerzeit ";
                if (dutyMedics === 1) {
                    medicString += "befindet sich ~g~" + dutyMedics + " Mitglied";
                } else {
                    medicString += "befinden sich ~g~" + dutyMedics + " Mitglieder";
                }
                medicString += " ~s~im Dienst";
            } else {
                medicString = "Derzeit befinden sich ~r~keine Einsatzkräfte ~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.5, 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.945],
                    {
                        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");
            }
        }
    });
}
