using System;
using GTANetworkAPI;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Factions.Medic;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.Wanted;

/**
* @overview Life of German Reallife - Events Death (Death.cs)
* @author VegaZ
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Events
{
    public class Death : Script
    {
        [ServerEvent(Event.PlayerDeath)]
        public void OnPlayerDeath(Client player, Client killer, uint reason)
        {
            if (!player.IsLoggedIn()) player.Kick();
            player.SetData("isDead", true);

            if (player.GetUser().IsAdmin(AdminLevel.ADMIN) == true)
            {
                player.TriggerEvent("startDeathTimer", true);
            }
            else
            {
                player.TriggerEvent("startDeathTimer", false);
            }

            //var dutyMedics = 0;            
            //var allPlayers = NAPI.Pools.GetAllPlayers();

            //foreach (Client medic in allPlayers)
            //{
            //    if (medic.GetUser()?.FactionId == 2)
            //    {
            //        dutyMedics++;
            //    }
            //}
            //player.TriggerEvent("medicInfo", dutyMedics);

            //TODO: Zum Full Release entfernen
            ChatService.SendMessage(player, "Du bist durch " + killer.Name + " gestorben: " + reason.ToString());

            int? killerId;
            float killerPosX;
            float killerPosY;
            float killerPosZ;
            float killerHeading;

            if (killer.IsNull)
            {
                killerId = null;
                killerPosX = -1;
                killerPosY = -1;
                killerPosZ = -1;
                killerHeading = -1;
            }
            else
            {
                killerId = killer.GetUser().Id;
                killerPosX = killer.Position.X;
                killerPosY = killer.Position.Y;
                killerPosZ = killer.Position.Z;
                killerHeading = killer.Heading;
                if (player != killer)
                {
                    string message = "~y~[HINWEIS]: " + killer.Name + " hat " + player.Name + " getötet (" + NAPI.Player.GetPlayerCurrentWeapon(killer) + ")";
                    ChatService.BroadcastAdmin(message, AdminLevel.ADMIN);
                }
            }

            //JailTime.cs
            Jail.Check_PutBehindBars(player);


            //MEDIC AUFTRAG
            MedicTask reviveTask = new MedicTask()
            {
                Victim = player.Name,
                Position = player.Position,
                CauseOfDeath = reason.ToString(),
                Caller = null,
                Description = "Gestorben",
                Time = DateTime.Now,
                Type = MedicTaskType.REVIVE,
                MedicName = "none"
            };
            Medic.AddTaskToList(reviveTask);

            //TODO PICTURE NOTIFICATION + SOUND für Medics

            using (var userDeath = new DatabaseContext())
            {
                var dead = new Entities.Logs.Death
                {
                    VictimId = player.GetUser().Id,
                    KillerId = killerId,
                    KillerPositionX = killerPosX,
                    KillerPositionY = killerPosY,
                    KillerPositionZ = killerPosZ,
                    KillerHeading = killerHeading,
                    VictimPositionX = player.Position.X,
                    VictimPositionY = player.Position.Y,
                    VictimPositionZ = player.Position.Z,
                    VictimHeading = player.Heading,
                    CauseOfDeath = reason.ToString()
                };
                userDeath.DeathLogs.Add(dead);
                userDeath.SaveChanges();
            }
        }
        [RemoteEvent("RespawnPlayerAtHospital")]
        public void RespawnPlayerAtHospital(Client player)
        {
            player.SetData("isDead", false);
            player.RemoveAllWeapons();
            NAPI.Player.SpawnPlayer(player, new Vector3(-495.45, -336.33, 34.5));
        }
    }
}
