using System.Collections.Generic;
using System.Linq;
using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;

namespace ReallifeGamemode.Server.Events
{
  public class UpdateCharacterWeapon : Script
  {
    [RemoteEvent("updateWeaponSelection")]
    public void UpdateWeaponSelection(Player client, string weaponModel, int slot)
    {
      if (weaponModel == "Keine")
      {
        client.RemoveAllWeapons();
        return;
      }

      WeaponHash weaponHash = NAPI.Util.WeaponNameToModel(weaponModel);

      if (slot == 1)
      {
        client.RemoveAllWeapons();
        client.GiveWeapon(weaponHash, 0);
      }
      if (slot == 2)
      {
        client.RemoveAllWeapons();
        client.GiveWeapon(weaponHash, 0);
      }
      if (slot == 3)
      {
        client.RemoveAllWeapons();
        client.GiveWeapon(weaponHash, 0);
      }
      if (slot == 4)
      {
        client.RemoveAllWeapons();
        client.GiveWeapon(weaponHash, 0);
      }
    }

    [RemoteEvent("cancelWeaponSelection")]
    public void CancelWeaponSelection(Player client)
    {
      client.RemoveAllWeapons();
    }


    [RemoteEvent("saveWeaponSelection")]
    public void SaveWeaponSelection(Player client, string primaryModel, string secondaryModel, string meleeModel, string specialModel)
    {
      client.RemoveAllWeapons();
      if (!uint.TryParse(primaryModel, out uint primary))
      {
        if (primaryModel.Contains("mk2") && !primaryModel.Contains("_mk2")) primaryModel = primaryModel.Replace("mk2", "_mk2");
        primary = NAPI.Util.GetHashKey($"weapon_{primaryModel}");
      }
      if (!uint.TryParse(secondaryModel, out uint secondary))
      {
        if (secondaryModel.Contains("mk2") && !secondaryModel.Contains("_mk2")) secondaryModel = secondaryModel.Replace("mk2", "_mk2");
        secondary = NAPI.Util.GetHashKey($"weapon_{secondaryModel}");
      }
      if (!uint.TryParse(meleeModel, out uint melee))
      {
        if (meleeModel.Contains("mk2") && !meleeModel.Contains("_mk2")) meleeModel = meleeModel.Replace("mk2", "_mk2");
        melee = NAPI.Util.GetHashKey($"weapon_{meleeModel}");
      }


      client.GiveWeapon((WeaponHash)primary, 50);
      client.GiveWeapon((WeaponHash)secondary, 150);
      client.GiveWeapon((WeaponHash)melee, 1);
      if (specialModel != "Schutzweste")
      {
        client.Armor = 0;
        if (!uint.TryParse(specialModel, out uint special))
        {
          if (specialModel.Contains("mk2") && !specialModel.Contains("_mk2")) specialModel = specialModel.Replace("mk2", "_mk2");
          special = NAPI.Util.GetHashKey($"weapon_{specialModel}");
        }
        client.GiveWeapon((WeaponHash)special, 50);
      }
      else
      {
        client.Armor = 50;
      }

      using (var context = new DatabaseContext())
      {
        User user = client.GetUser();
        FactionWeapon slot1 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == primaryModel).FirstOrDefault();
        if (slot1 != null)
          slot1.Ammount -= 1;
        FactionWeapon slot2 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == secondaryModel).FirstOrDefault();
        if (slot2 != null)
          slot2.Ammount -= 1;
        FactionWeapon slot3 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == meleeModel).FirstOrDefault();
        if (slot3 != null)
          slot3.Ammount -= 1;
        FactionWeapon slot4 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == specialModel).FirstOrDefault();
        if (slot4 != null)
          slot4.Ammount -= 1;
        context.SaveChanges();
      }
    }
  }
}
