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

namespace ReallifeGamemode.Server.Events
{
  public class ExitVehicle : Script
  {
    [ServerEvent(Event.PlayerExitVehicleAttempt)]
    public void OnPlayerExitVehicle(Player client, GTANetworkAPI.Vehicle vehicle)
    {
      if (client.VehicleSeat != -1)
        return;
      if (vehicle.GetServerVehicle() is FactionVehicle veh)
      {
        if ((client.GetUser().FactionId != null) && (veh.FactionId == client.GetUser().FactionId) && ((VehicleHash)vehicle.Model == VehicleHash.Burrito3) && vehicle.HasData("weaponDeal") && vehicle.GetData<bool>("weaponDeal") == true)
        {
          CheckPointHandle.RemovePlayerHandlerFromList(client);
          client.TriggerEvent("destroyCP");
        }
      }
      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.Position = sVeh.Position;
          CheckPointHandle.DeleteCheckpoints(client);
        }
      }
    }
  }
}
