using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Managers;
using System.Linq;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;

namespace ReallifeGamemode.Server.WeaponDeal
{
  public class WeaponDealManager : Script
  {
    public static bool checkWeaponDbyVehicle(Vehicle vehicle)
    {
      if (!vehicle.HasData("WeaponDealLoad") || vehicle.GetData<bool>("WeaponDealLoad") == false)
        return false;

      return true;
    }

    [RemoteEvent("startWeaponDeal")]
    public void SrvEVENT_startWeaponDeal(Player client)
    {
      var user = client.GetUser();
      using (var context = new DatabaseContext())
      {
        FactionVehicle factionVehicle = context.FactionVehicles.Where(f => f.FactionId == user.FactionId && f.Model == VehicleHash.Burrito3).FirstOrDefault();
        Vehicle fVeh = VehicleManager.GetVehicleFromServerVehicle(factionVehicle);

        if (fVeh.Position.DistanceTo(client.Position) > 50)
        {
          ChatService.ErrorMessage(client, "Der Transporter ist zu weit entfernt");
          return;
        }
        if (user.Faction.WeaponDealTime <= 0)
        {
          ChatService.BroadcastFaction("Die albanische Mafia hat einen Deal mit deiner Fraktion abgemacht!", user.Faction);
          ChatService.SendMessage(client, "Steige in den Burrito ein und fahre zum Waffendeal.");
          fVeh.SetData("weaponDeal", true);
          WeaponDealPoints.factionWeaponDeal[user.FactionId.Value] = -1;
          Faction fac = context.Factions.Where(f => f.Id == user.FactionId).FirstOrDefault();
          fac.WeaponDealTime = 60;
          context.SaveChanges();
        }
        else
        {
          ChatService.ErrorMessage(client, "Du kannst noch kein Waffendeal starten");
        }
      }
    }

    public static void WeaponDealTimer()
    {
      System.Timers.Timer timer = new System.Timers.Timer(60000);
      timer.Start();
      timer.Elapsed += Timer_Elapsed;
    }

    private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      using (var context = new DatabaseContext())
      {
        List<Faction> fac = context.Factions.ToList<Faction>();
        foreach (var faction in fac)
        {
          if (faction.WeaponDealTime > 0)
            faction.WeaponDealTime -= 1;
        }
        context.SaveChanges();
      }

    }


    [RemoteEvent("loadWeaponTransport")]
    public void SrvEVENT_loadWeaponTransport(Player client)
    {
      if ((VehicleHash)client.Vehicle.Model != VehicleHash.Burrito3)
        return;

      User user = client.GetUser();
      using (var context = new DatabaseContext())
      {
        FactionVehicle factionVehicle = context.FactionVehicles.Where(f => f.FactionId == user.FactionId && f.Model == VehicleHash.Burrito3).FirstOrDefault();
        Vehicle fVeh = VehicleManager.GetVehicleFromServerVehicle(factionVehicle);
        fVeh.SetData("weaponDeal", false);
        fVeh.SetData("WeaponDealLoad", true);
        InventoryManager.RemoveAllItemsfromVehicleInventory(fVeh);
        Random rnd = new Random();
        if (factionVehicle.FactionId == 8 || factionVehicle.FactionId == 7)
        {
          VehicleItem item = new VehicleItem() { ItemId = 11, VehicleId = factionVehicle.Id, Amount = rnd.Next(45, 75) }; //pistole
          InventoryManager.AddItemToVehicleInventory(client, item, fVeh);
          VehicleItem item3 = new VehicleItem() { ItemId = 14, VehicleId = factionVehicle.Id, Amount = rnd.Next(30, 40) }; //Pumpe
          InventoryManager.AddItemToVehicleInventory(client, item3, fVeh);
          VehicleItem item2 = new VehicleItem() { ItemId = 16, VehicleId = factionVehicle.Id, Amount = rnd.Next(5, 15) }; //Schutzweste
          InventoryManager.AddItemToVehicleInventory(client, item2, fVeh);
        }

      }

    }
  }
}
