using System.Linq;
using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Factions.Medic;
using ReallifeGamemode.Server.Managers;

namespace ReallifeGamemode.Server.Util
{
  internal class FactionHelper
  {
    public static void CheckFactionBankAccounts()
    {
      NAPI.Util.ConsoleOutput("Checking faction bank accounts...");
      using (var dbContext = new DatabaseContext())
      {
        foreach (Faction faction in dbContext.Factions.Include(f => f.BankAccount))
        {
          if (faction.BankAccount == null)
          {
            NAPI.Util.ConsoleOutput("Adding bank account for faction: " + faction.Name);
            faction.BankAccount = new FactionBankAccount()
            {
              Balance = 100000,
              Bic = "",
              Iban = "",
              Active = true
            };
          }
        }
        dbContext.SaveChanges();
      }
    }

    public static void ResetPlayer(Player player, User user, DatabaseContext dbContext)
    {
      if (user.GetData<bool>("duty") == true)
      {
        user.SetData("duty", false);
        Medic.UpdateDutyMedics();
      }

      var userClothes = dbContext.CharacterClothes.Where(c => c.UserId == user.Id && c.Duty == true);
      dbContext.CharacterClothes.RemoveRange(userClothes);

      CharacterCreator.ApplyCharacter(player);
      Events.UpdateCharacterCloth.LoadCharacterDefaults(player);
 
      player.TriggerEvent("toggleDutyMode", false);
    }
  }
}
