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.Managers;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.WeaponDeal;

namespace ReallifeGamemode.Server.Events
{
  public class ExitVehicle : Script
  {
    object LastVehicle;
    public static Timer timerNoobRollerRespawn = new Timer(600000); //10 Minuten Timer NoobRollerRespawn
    [ServerEvent(Event.PlayerExitVehicleAttempt)]
    public void OnPlayerExitVehicle(Player client, GTANetworkAPI.Vehicle vehicle)
    {
      LastVehicle = vehicle;
      if (client.VehicleSeat != 0)
        return;

      if (vehicle.GetServerVehicle() is FactionVehicle veh)
      {
        User u = client.GetUser();
        if ((u.FactionId != null)
            && (veh.GetOwners().Contains(u.FactionId ?? 0))
            && (veh.Model == WeaponDealManager.WEAPON_DEAL_GANG_VEHICLE_HASH || veh.Model == WeaponDealManager.WEAPON_DEAL_STAATSFRAK_VEHICLE_HASH)
            && vehicle.HasData("weaponDeal")
            && vehicle.GetData<bool>("weaponDeal") == true)
        {
          CheckPointHandle.RemovePlayerHandlerFromList(client);
          client.TriggerEvent("destroyCP");
        }
        else if ((u.FactionId != null)
                 && (veh.Model == WeaponDealManager.WEAPON_DEAL_GANG_VEHICLE_HASH || veh.Model == WeaponDealManager.WEAPON_DEAL_STAATSFRAK_VEHICLE_HASH)
                 && vehicle.HasData("WeaponDealLoad")
                 && vehicle.GetData<bool>("WeaponDealLoad") == true)
        {
          client.TriggerEvent("SERVER:setMarkerBehindVehicle", vehicle);
        }
      }
      if (vehicle.GetServerVehicle() is SchoolVehicle vehS)
      {
        if (client.HasData("ActiveSchool"))
        {
          client.SendNotification("~r~Du hast deine Prüfung unterbrochen!", true);
          client.ResetData("ActiveSchool");
          client.WarpOutOfVehicle();
          ServerVehicle sVeh = VehicleManager.GetServerVehicleFromVehicle(vehicle);
          ServerVehicleExtensions.Spawn(sVeh, vehicle);
          client.SafeTeleport(sVeh.Position);
          CheckPointHandle.DeleteCheckpoints(client);
        }
      }
      if (vehicle.GetData<bool>("NoobSpawnVehicle") == true)
      {
        timerNoobRollerRespawn.Start();
        vehicle.SetData("NoobSpawnVehicleTimer", true);
        timerNoobRollerRespawn.AutoReset = false;
        timerNoobRollerRespawn.Elapsed += Timer600000_Elapsed;
        //LastVehicle = vehicle;
        //timerNoobRollerRespawn.Elapsed += async (sender, e) => {await
      }
    }
    private static void Timer600000_Elapsed(object sender, ElapsedEventArgs e)
    {
      //ExitVehicle c = new ExitVehicle();
      //Vehicle veh = c.LastVehicle;
      NAPI.Task.Run(() =>
      {
        NAPI.Pools.GetAllVehicles().ForEach(v =>
        {
          if (v.GetData<bool>("NoobSpawnVehicle") == true)
          {
            if (v.GetData<bool>("NoobSpawnVehicleTimer") == true)
            {
              v.ResetData("NoobSpawnVehicleTimer");
              v.ResetData("NoobSpawnVehicle");
              VehicleManager.DeleteVehicle(v);
            }
          }
        });
        NAPI.Pools.GetAllPlayers().ForEach(p =>
        {
          //if (!p.IsInVehicle)
          //{
          if (p.GetData<bool>("HatNoobSpawnVehicle") == true)
          {
            p.ResetData("HatNoobSpawnVehicle");
          }
          //}
        });
      });
    }
  }
}
