using System.Linq;
using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.Wanted;

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

namespace ReallifeGamemode.Server.Events
{
    public class Login : Script
    {
        [RemoteEvent("CLIENT:Login_LoginRequest")]
        public void OnPlayerLogin(Client player, string username, string password)
        {
            using (var dbContext = new DatabaseContext())
            {
                var user = dbContext.Users
                    .Include(u => u.Group)
                    .SingleOrDefault(b => b.Name == username);

                if (user == null)
                {
                    player.TriggerEvent("SERVER:Login_Error", "Benutzer existiert nicht! Registriere dich zuerst!");
                }
                else
                {
                    if (user.SocialClubName != player.SocialClubName && user.Password == NAPI.Util.GetHashSha256(password))
                    {
                        player.TriggerEvent("SERVER:Login_Error", "Dieser Benutzer gehört dir nicht!");
                        //TODO ?? Log einbauen für den bösen Bub.
                        player.Kick();
                    }
                    if (user.Password != NAPI.Util.GetHashSha256(password))
                    {
                        player.TriggerEvent("SERVER:Login_Error", "Passwort inkorrekt!");
                    }
                    else
                    {
                        player.Name = username;
                        player.TriggerEvent("SERVER:Login_Success");
                        player.SetData("isLoggedIn", true);
                        player.SetData("spec", true);
                        player.SetData("duty", false);
                        player.TriggerEvent("SERVER:SET_HANDMONEY", user.Handmoney, 0);

                        if (user.IsAdmin(AdminLevel.HEADADMIN) == true)
                        {
                            player.SetData("editmode", false);
                            player.SetData("quicksavemode", "none");
                        }

                        var userBankAccount = user.GetBankAccount();
                        userBankAccount.Balance = userBankAccount.Balance;

                        user.Wanteds = user.Wanteds;

                        if(user.Group != null)
                        {
                            string msg = $"{player.Name} ist wieder online.";
                            ChatService.BroadcastGroup(msg, user.Group);
                        }

                        var userItems = dbContext.UserItems.Where(u => u.UserId == user.Id).ToList();
                        player.SetData("items", userItems);

                        if (user.CharacterId == null)
                        {
                            var currentPlayerCreatorDimension = (uint)NAPI.Data.GetWorldData("playerCreatorDimension");
                            currentPlayerCreatorDimension++;
                            NAPI.Data.SetWorldData("playerCreatorDimension", currentPlayerCreatorDimension);
                            player.Dimension = NAPI.Data.GetWorldData("playerCreatorDimension");
                            player.Position = new Vector3(402.8664, -996.4108, -99.00027);
                            player.TriggerEvent("toggleCreator");
                        }
                        else
                        {
                            CharacterCreator.ApplyCharacter(player);
                            UpdateCharacterCloth.LoadCharacterDefaults(player);
                            if(user.JailTime == 0)
                            {
                                NAPI.Player.SpawnPlayer(player, new Vector3(user.PositionX, user.PositionY, user.PositionZ), 0);
                            }
                            else
                            {
                                Jail.Check_PutBehindBars(player);
                            }
                            
                            
                        }

                        player.TriggerEvent("draw", player.Name, player.Handle.Value);
                        if (user.Dead == true)
                        {
                            if (user.IsAdmin(AdminLevel.ADMIN) == true)
                            {
                                player.TriggerEvent("startDeathTimer", true);
                                player.Health = 0;
                            }
                            else
                            {
                                player.TriggerEvent("startDeathTimer", false);
                                player.Health = 0;
                            }
                            player.SetData("isDead", true);
                        }
                        else
                        {
                            player.SetData("isDead", false);
                        }
                    }
                }
            }
        }
    }
}
