using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Types;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.WeaponDeal;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Job;

namespace ReallifeGamemode.Server.Events
{
  public class EnterVehicleAttempt : Script
  {
    public static GTANetworkAPI.Vehicle Roller;

    [ServerEvent(Event.PlayerEnterVehicleAttempt)]
    public void OnPlayerEnterVehicleAttempt(Player player, GTANetworkAPI.Vehicle vehicle, sbyte seat)
    {
      if (seat != 0) return;

      if (player.HasAnimation())
        player.StopAnimation();

      User u = player.GetUser();

      if (vehicle.GetServerVehicle() is FactionVehicle veh)
      {
        if (!veh.GetOwners().Contains(player.GetUser().FactionId ?? 0) && !(u.IsAdmin(AdminLevel.HEADADMIN) && player.IsAdminDuty()) && !JobBase.GetPlayerInJob().Contains(player))
        {
          if ((VehicleHash)vehicle.Model == WeaponDealManager.WEAPON_DEAL_GANG_VEHICLE_HASH)
          {
            if (WeaponDealManager.checkWeaponDbyVehicle(vehicle))
              return;
          }
          player.StopAnimation();
          player.SendNotification("~r~Du darfst dieses Fahrzeug nicht benutzen!", true);
          return;
        }
      }
      if (vehicle.GetServerVehicle() is JobVehicle jveh)
      {
        if (jveh.JobId != player.GetUser().JobId && !(u.IsAdmin(AdminLevel.HEADADMIN) && player.IsAdminDuty()))
        {
          player.StopAnimation();
          player.SendNotification("~r~Du darfst dieses Fahrzeug nicht benutzen!", true);
          return;
        }
        else if (u.IsAdmin(AdminLevel.HEADADMIN) && player.IsAdminDuty())
        {
        }
        else
        {
          if ((VehicleHash)vehicle.Model == VehicleHash.Velum && player.GetUser().PilotSkill < 300)
          {
            player.SetData("JobVehicleFrei", true);
            player.StopAnimation();
            player.SendNotification("~y~[JOB] ~r~Du besitzt nicht das nötige Skill Level(2)!");
            return;
          }
          if ((VehicleHash)vehicle.Model == VehicleHash.Velum2 && player.GetUser().PilotSkill < 300)
          {
            player.SetData("JobVehicleFrei", true);
            player.StopAnimation();
            player.SendNotification("~y~[JOB] ~r~Du besitzt nicht das nötige Skill Level(2)!");
            return;
          }
          if ((VehicleHash)vehicle.Model == VehicleHash.Coach && player.GetUser().BusSkill < 300)
          {
            player.SetData("JobVehicleFrei", true);
            player.StopAnimation();
            player.SendNotification("~y~[JOB] ~r~Du besitzt nicht das nötige Skill Level(2)!");
            return;
          }
          if ((VehicleHash)vehicle.Model == VehicleHash.Tourbus && player.GetUser().BusSkill < 800)
          {
            player.SetData("JobVehicleFrei", true);
            player.StopAnimation();
            player.SendNotification("~y~[JOB] ~r~Du besitzt nicht das nötige Skill Level(3)!");
            return;
          }
        }
      }
      if (vehicle.GetServerVehicle() is SchoolVehicle sVeh)
      {
        if (!player.HasData("ActiveSchool") && !(u.IsAdmin(AdminLevel.HEADADMIN) && player.IsAdminDuty()))
        {
          player.StopAnimation();
          player.SendNotification("~r~Du darfst dieses Fahrzeug nicht benutzen!", true);
          return;
        }
        if (sVeh.SchoolId != player.GetData<int>("ActiveSchool"))
        {
          player.StopAnimation();
          player.SendNotification("~r~Du darfst dieses Fahrzeug nicht benutzen!", true);
          return;
        }
      }
      if (vehicle.GetServerVehicle() is NoobVehicle nVeh)
      {
        if (!player.IsAdminDuty())
        {
          if (player.GetUser().PlayedMinutes > 1800)
          {
            player.StopAnimation();
            player.SendNotification("~r~Du hast schon über 30 Spielstunden!", true);
            return;
          }
        }
        else if (player.IsAdminDuty())
        {
        }
      }
      if (vehicle.GetServerVehicle() is NoobSpawnVehicle nsVeh)
      {
        if (!player.IsAdminDuty())
        {
          if (player.GetUser().PlayedMinutes > 1800)
          {
            player.StopAnimation();
            player.SendNotification("~r~Du hast schon über 30 Spielstunden!", true);
            return;
          }
          if (player.GetData<bool>("HatNoobSpawnVehicle") == true)
          {
            player.StopAnimation();
            player.SendNotification("~r~Du hast bereits einen Roller!", true);
            return;
          }
          else
          {
            Random rnd = new Random();
            int spawn = rnd.Next(1, 3);
            ServerVehicle nsSVeh = VehicleManager.GetServerVehicleFromVehicle(vehicle);
            player.WarpOutOfVehicle();
            ServerVehicleExtensions.Spawn(nsSVeh, vehicle);
            var model = vehicle.Model;
            //player.SendChatMessage($"{model}");
            switch (spawn)
            {
              case 1:
                {
                  if (model == 2452219115) //Faggio
                  {
                    Roller = NAPI.Vehicle.CreateVehicle(2452219115, new Vector3(-1024.4227294921875, -2697.9541015625, 13.218425750732422), 106.366f, 130, 130, engine: true);
                    Roller.SetData("NoobSpawnVehicle", true);
                    player.SetData("HatNoobSpawnVehicle", true);
                    player.SetIntoVehicle(Roller.Handle, 0);
                    Roller.EngineStatus = true;
                    VehicleStreaming.SetEngineState(Roller, true);
                    VehicleStreaming.SetLockStatus(Roller, false);
                  }
                  if (model == 55628203) //Faggio2
                  {
                    Roller = NAPI.Vehicle.CreateVehicle(55628203, new Vector3(-1024.4227294921875, -2697.9541015625, 13.218425750732422), 106.366f, 130, 130, engine: true);
                    Roller.SetData("NoobSpawnVehicle", true);
                    player.SetData("HatNoobSpawnVehicle", true);
                    player.SetIntoVehicle(Roller.Handle, 0);
                    Roller.EngineStatus = true;
                    VehicleStreaming.SetEngineState(Roller, true);
                    VehicleStreaming.SetLockStatus(Roller, false);
                  }
                  if (model == 3005788552) //Faggio3
                  {
                    Roller = NAPI.Vehicle.CreateVehicle(3005788552, new Vector3(-1024.4227294921875, -2697.9541015625, 13.218425750732422), 106.366f, 130, 130, engine: true);
                    Roller.SetData("NoobSpawnVehicle", true);
                    player.SetData("HatNoobSpawnVehicle", true);
                    player.SetIntoVehicle(Roller.Handle, 0);
                    Roller.EngineStatus = true;
                    VehicleStreaming.SetEngineState(Roller, true);
                    VehicleStreaming.SetLockStatus(Roller, false);
                  }
                  break;
                }
              case 2:
                {
                  if (model == 2452219115) //Faggio
                  {
                    Roller = NAPI.Vehicle.CreateVehicle(2452219115, new Vector3(-1013.8352661132812, -2704.135498046875, 13.218762397766113), -164.867f, 130, 130, engine: true);
                    Roller.SetData("NoobSpawnVehicle", true);
                    player.SetData("HatNoobSpawnVehicle", true);
                    player.SetIntoVehicle(Roller.Handle, 0);
                    Roller.EngineStatus = true;
                    VehicleStreaming.SetEngineState(Roller, true);
                    VehicleStreaming.SetLockStatus(Roller, false);
                  }
                  if (model == 55628203) //Faggio2
                  {
                    Roller = NAPI.Vehicle.CreateVehicle(55628203, new Vector3(-1013.8352661132812, -2704.135498046875, 13.218762397766113), -164.867f, 130, 130, engine: true);
                    Roller.SetData("NoobSpawnVehicle", true);
                    player.SetData("HatNoobSpawnVehicle", true);
                    player.SetIntoVehicle(Roller.Handle, 0);
                    Roller.EngineStatus = true;
                    VehicleStreaming.SetEngineState(Roller, true);
                    VehicleStreaming.SetLockStatus(Roller, false);
                  }
                  if (model == 3005788552) //Faggio3
                  {
                    Roller = NAPI.Vehicle.CreateVehicle(3005788552, new Vector3(-1013.8352661132812, -2704.135498046875, 13.218762397766113), -164.867f, 130, 130, engine: true);
                    Roller.SetData("NoobSpawnVehicle", true);
                    player.SetData("HatNoobSpawnVehicle", true);
                    player.SetIntoVehicle(Roller.Handle, 0);
                    Roller.EngineStatus = true;
                    VehicleStreaming.SetEngineState(Roller, true);
                    VehicleStreaming.SetLockStatus(Roller, false);
                  }
                  break;
                }
            }
          }
        }
        else if (player.IsAdminDuty())
        {
        }
      }
      if (vehicle.HasData("NoobSpawnVehicle"))
      {
        if (!player.IsAdminDuty())
        {
          if (player.GetUser().PlayedMinutes > 1800)
          {
            player.StopAnimation();
            player.SendNotification("~r~Du hast schon über 30 Spielstunden!", true);
            return;
          }
          else
          {
            ExitVehicle.timerNoobRollerRespawn.Stop();
            vehicle.SetData("NoobSpawnVehicleTimer", false);
          }
        }
      }
    }
  }
}
