using System;
using System.Collections.Generic;
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 EnterVehicle : Script
  {
    [ServerEvent(Event.PlayerEnterVehicle)]
    public void OnPlayerEnterVehicle(Player client, GTANetworkAPI.Vehicle vehicle, sbyte seat)
    {
      if (seat != 0)
      {
        return;
      }

      if (vehicle.HasMarkerBehind())
        vehicle.RemoveMarkerBehind();

      if(vehicle != null && !string.IsNullOrEmpty(client.Name))
      {
        if (!VehicleManager.lastDriversInVehicle.ContainsKey(vehicle))
        {
          VehicleManager.lastDriversInVehicle.Add(vehicle, new Dictionary<string, DateTime>());
        }

        VehicleManager.lastDriversInVehicle[vehicle][client.Name] = DateTime.Now;
      }

      if (vehicle.GetServerVehicle() is FactionVehicle veh)
      {
        User u = client.GetUser();
        if (u.FactionId != null
          && veh.GetOwners().Contains(u.FactionId.Value)
          && (veh.Model == WeaponDealManager.WEAPON_DEAL_GANG_VEHICLE_HASH || veh.Model == WeaponDealManager.WEAPON_DEAL_STAATSFRAK_VEHICLE_HASH)
          && vehicle.HasData("weaponDeal")
          && vehicle.GetData<bool>("weaponDeal") == true
          && (!vehicle.HasData("WeaponDealLoad") || vehicle.GetData<bool>("WeaponDealLoad") == false))
        {
          List<Vector3> dealPoint = new List<Vector3>();
          dealPoint.Add(vehicle.GetData<Vector3>("weaponDealPoint"));
          CheckPointHandle.StartCheckPointRoute(client, dealPoint, 5000, 1, 7, 3, true, "loadWeaponTransport");
        }
        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)
        {
          BasePoints basePoints = new BasePoints();
          basePoints.getbase(u.FactionId, client);        
        }


      }
    }
  }
}
