using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Entities.Logs.Chat;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Admin;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Factions.Medic;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Types;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Services;

/**
* @overview Life of German Reallife - Faction Commands (Faction.cs)
* @author VegaZ, hydrant, aviate
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Commands
{
  internal class FactionCommands : Script
  {
    #region Chat Commands

    [Command("f", "~m~Benutzung: ~s~/f [Nachricht]", GreedyArg = true)]
    public void CmdFactionF(Player player, string message)
    {
      if (!player.IsLoggedIn())
      {
        return;
      }

      using var dbContext = new DatabaseContext();
      User user = player.GetUser();

      Faction f = user?.Faction;
      if (f == null || f.StateOwned)
      {
        ChatService.NotAuthorized(player);
        return;
      }

      message = Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");

      var logEntry = new FactionChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
        FactionId = user.Faction.Id,
      };

      dbContext.FactionChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      string broadcastMessage = "!{02FCFF}** " + user.FactionRank.RankName + " " + player.Name + ": " + message + " **";
      ChatService.BroadcastFaction(broadcastMessage, f);
    }

    [Command("ga", "~m~Benutzung: ~s~/ga [Nachricht]", GreedyArg = true)]
    public void CmdFactionGA(Player player, string message)
    {
      if (!player.IsLoggedIn())
      {
        return;
      }

      using var dbContext = new DatabaseContext();

      User user = player.GetUser(dbContext);
      Faction f = user?.Faction;
      if ((f == null || !f.GangOwned) && (!user.IsAdmin(AdminLevel.ADMIN) || !player.HasData("togga")))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      message = Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");

      string factionName = string.Empty;

      if (f?.GangOwned == true)
      {
        factionName = user.Faction.Name + " – " + user.FactionRank.RankName;
      }
      else
      {
        factionName = "ADMIN";
      }

      var logEntry = new GangChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
      };

      dbContext.GangChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      string broadcastMessage = "!{E52222}** [" + factionName + "] " + player.Name + ": " + message + " **";
      using (var context = new DatabaseContext())
      {
        ChatService.BroadcastFaction(broadcastMessage, context.Factions.ToList().FindAll(c => c.GangOwned), true, (admin) => admin.HasData("togga"));
      }
    }

    [Command("r", "~m~Benutzung: ~s~/r [Nachricht]", GreedyArg = true)]
    public void CmdFactionR(Player player, string message)
    {
      if (!player.IsLoggedIn())
      {
        return;
      }
      using var dbContext = new DatabaseContext();

      User user = player.GetUser(dbContext);
      Faction f = user?.Faction;
      if (f == null || !f.StateOwned)
      {
        ChatService.NotAuthorized(player);
        return;
      }

      message = Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");

      var logEntry = new FactionChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
        Faction = user.Faction,
      };

      dbContext.FactionChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      string broadcastMessage = "!{33AA33}** " + player.GetUser().GetFactionRank().RankName + " " + player.Name + ": " + message + ", over **";
      ChatService.BroadcastFaction(broadcastMessage, f);
    }

    [Command("d", "~m~Benutzung: ~s~/d [Nachricht]", GreedyArg = true)]
    public void CmdFactionD(Player player, string message)
    {
      if (!player.IsLoggedIn())
      {
        return;
      }

      using var dbContext = new DatabaseContext();

      User user = player.GetUser(dbContext);
      Faction f = user?.Faction;
      if ((f == null || !f.StateOwned) && (!user.IsAdmin(AdminLevel.ADMIN) || !player.HasData("togd")))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      message = Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");

      string factionName = string.Empty;

      if (f?.StateOwned ?? false)
      {
        factionName = user.Faction.Name + " – " + user.FactionRank.RankName;
      }
      else
      {
        factionName = "ADMIN";
      }

      var logEntry = new DepartmentChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
      };

      dbContext.DepartmentChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      string broadcastMessage = "!{CC3333}** [" + factionName + "] " + player.Name + ": " + message + ", over **";
      using (var context = new DatabaseContext())
      {
        ChatService.BroadcastFaction(broadcastMessage, context.Factions.ToList().FindAll(c => c.StateOwned), true, (admin) => admin.HasData("togd"));
      }
    }

    #endregion Chat Commands

    #region NewsReporter commands

    [Command("news", "~m~Benutzung: ~s~/news [Nachricht]", GreedyArg = true)]
    public void CmdFactionNR(Player player, string message)
    {
      if (!player.IsLoggedIn())
      {
        return;
      }

      using var dbContext = new DatabaseContext();

      User user = player.GetUser(dbContext);

      Faction f = user?.Faction;
      if (f == null || f.Id != 9)
      {
        ChatService.NotAuthorized(player);
        return;
      }
      message = Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");

      var logEntry = new NewsChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
      };

      dbContext.NewsChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      string broadcastMessage = "!{ff9531}** News Reporter" + " " + player.Name + ": " + message + " **";
      ChatService.Broadcast(broadcastMessage);
    }

    #endregion NewsReporter commands

    #region Leader Commands

    [Command("giverank", "~m~Benutzung: ~s~/giverank [Name] [Rang]", GreedyArg = true)]
    public void CmdFactionGiverank(Player player, string name, string rank)
    {
      if (player.GetUser()?.FactionId == null || player.GetUser().FactionLeader == false)
      {
        ChatService.NotAuthorized(player);
        return;
      }

      Player target = PlayerService.GetPlayerByNameOrId(name);
      if (target == null || !target.IsLoggedIn())
      {
        ChatService.PlayerNotFound(player);
        return;
      }

      if (target.GetUser()?.FactionId != player.GetUser()?.FactionId)
      {
        ChatService.ErrorMessage(player, "Dieser Spieler ist nicht in deiner Fraktion");
        return;
      }

      using (var dbContext = new DatabaseContext())
      {
        FactionRank fr = dbContext.FactionRanks.FirstOrDefault(r => r.Order == int.Parse(rank) && r.FactionId == player.GetUser(dbContext).FactionId);
        if (fr == null)
        {
          ChatService.ErrorMessage(player, "Dieser Rang existiert nicht");
          return;
        }

        target.GetUser(dbContext).FactionRankId = fr.Id;

        ChatService.SendMessage(player, "!{02FCFF}Du hast " + target.Name + " den Rang '" + fr.RankName + "' (" + fr.Order + ") gegeben.");
        ChatService.SendMessage(target, "!{02FCFF}Du hast von " + player.Name + " den Rang '" + fr.RankName + "' (" + fr.Order + ")' erhalten.");

        dbContext.SaveChanges();
      }
    }

    [Command("lc", "~m~Benutzung: ~s~/lc [Nachricht]", GreedyArg = true)]
    public void CmdFactionLc(Player player, string message)
    {
      if (!player.IsLoggedIn())
      {
        return;
      }
      using var dbContext = new DatabaseContext();

      User user = player.GetUser(dbContext);
      if ((user?.FactionId == null || user.FactionLeader == false) && (!user.IsAdmin(AdminLevel.ADMIN) || !player.HasData("toglc")))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      message = Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");

      var logEntry = new LeaderChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
      };

      dbContext.LeaderChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      string factionName = string.Empty;
      if (user.FactionLeader)
      {
        factionName = user.Faction.Name;
      }
      else
      {
        factionName = "ADMIN";
      }

      string broadcastMsg = "~y~[" + factionName + "] " + player.Name + ": " + message;

      NAPI.Pools.GetAllPlayers().ForEach(p =>
      {
        User pUser = p.GetUser();
        if ((pUser?.FactionLeader ?? false) || (pUser.IsAdmin(AdminLevel.ADMIN) && p.HasData("toglc")))
        {
          ChatService.SendMessage(p, broadcastMsg);
        }
      });
    }

    [Command("setweaponrank", "~m~Benutzung: ~s~/setweaponrank [Waffen Name] [Rank]")]
    public void CmdFactionWeaponRank(Player player, string weaponModel, int rank)
    {
      User user = player.GetUser();
      if (user == null || user.FactionId == null || user.FactionLeader == false)
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (rank > 12 || rank < 1)
      {
        ChatService.ErrorMessage(player, "Gebe einen gültigen Rang ein");
        return;
      }

      WeaponPoint nearestWeapon = PositionManager.WeaponPoints.Find(w => w.Position.DistanceTo(player.Position) <= 1.5 && w.FactionId == user.FactionId);
      if (nearestWeapon == null)
      {
        ChatService.ErrorMessage(player, "Du bist nicht in der nähe vom Waffenspind");
        return;
      }

      using (var context = new DatabaseContext())
      {
        FactionWeapon fw2 = context.FactionWeapons.FirstOrDefault(w => w.FactionId == user.FactionId && w.WeaponModel == weaponModel);

        if (fw2 != null)
        {
          fw2.Rank = rank;
          context.SaveChanges();
          ChatService.SendMessage(player, "Du hast die " + weaponModel + " als Rang " + rank + " Waffe eingestellt.");
          return;
        }
        ChatService.ErrorMessage(player, "Diese Waffe ist nicht im Waffenlager");
        return;
      }
    }

    #endregion Leader Commands

    #region Sanitäter Commands

    [Command("revive", "~m~Benutzung: ~s~/revive")]
    public void CmdFactionMedicRevive(Player player)
    {
      if (player.GetUser()?.FactionId == null || player.GetUser().FactionId != 2)
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (!player.IsDuty())
      {
        ChatService.ErrorMessage(player, "Du bist nicht im Dienst");
        return;
      }

      if (player.IsInVehicle)
      {
        ChatService.ErrorMessage(player, "Du kannst in einem Auto nicht wiederbeleben");
        return;
      }

      var nearPlayers = NAPI.Player.GetPlayersInRadiusOfPlayer(4, player);
      var deadPlayer = nearPlayers.Where(i => i.GetData<bool>("isDead") == true).FirstOrDefault();
      if (player == deadPlayer)
      {
        ChatService.ErrorMessage(player, "Du kannst dich nicht selbst wiederbeleben");
        return;
      }

      if (deadPlayer == null)
      {
        ChatService.ErrorMessage(player, "Es wurde kein toter Spieler gefunden");
        return;
      }

      if (deadPlayer.HasData("reviveSperre"))
      {
        ChatService.ErrorMessage(player, "Der Spieler hat eine Revivesperre");
        return;
      }

      using (var dbContext = new DatabaseContext())
      {
        var deadPlayerUser = deadPlayer.GetUser(dbContext);

        if (deadPlayerUser.Wanteds > 0)
        {
          ChatService.ErrorMessage(player, "Dieser Spieler hat Wanteds");
          return;
        }

        if (deadPlayerUser.Handmoney >= 100)
        {
          deadPlayerUser.Handmoney -= 100;
        }
        else
        {
          int bankMoney = 100 - deadPlayerUser.Handmoney;
          deadPlayerUser.Handmoney = 0;
          deadPlayerUser.BankAccount.Balance -= bankMoney;
        }

        player.PlayAnimation("amb@medic@standing@kneel@enter", "enter", 0);

        deadPlayer.TriggerEvent("onPlayerRevived");
        player.TriggerEvent("destroyMedicTaskCheckpoint");
        deadPlayer.SendNotification($"Du wurdest von ~y~{player.Name}~s~ für ~g~{100.ToMoneyString()} ~s~wiederbelebt.");
        player.SendNotification($"Du hast ~y~{deadPlayer.Name}~s~ wiederbelebt und ~g~{Medic.ReviveIncome}$ ~s~für die Fraktion verdient.");
        deadPlayer.SetData("isDead", false);
        deadPlayer.SetData("reviveSperre", 5);

        deadPlayer.TriggerEvent("enableSpawnschutz");

        dbContext.Factions.Include(f => f.BankAccount).Where(f => f.Id == 2).First().BankAccount.Balance += Medic.ReviveIncome;

        User u;
        u = deadPlayer.GetUser(dbContext);
        u.Dead = false;
        dbContext.SaveChanges();
      }
      deadPlayer.SafeTeleport(deadPlayer.Position, 0, true);
      deadPlayer.SafeSetHealth(50);

      MedicTask task = Medic.ReviveTasks.FirstOrDefault(t => t.Victim == deadPlayer.Name);
      Medic.RemoveTaskFromList(task);
    }

    [Command("heal", "~m~Benutzung: ~s~/heal [Spieler] (Preis)")] //TODO Eventuell noch mit Geldbetrag wie bei SA:MP
    public void CmdFactionMedicHealive(Player player, string receiver, int price = 10)
    {
      Player target = PlayerService.GetPlayerByNameOrId(receiver);
      if (target == null || !target.IsLoggedIn())
      {
        ChatService.PlayerNotFound(player);
        return;
      }

      if (player.GetUser()?.FactionId != 2)
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (price < 10 || price > 100)
      {
        ChatService.ErrorMessage(player, "Der Preis muss zwischen 10$ und 100$ liegen");
        return;
      }

      if (!player.IsDuty())
      {
        ChatService.ErrorMessage(player, "Du bist nicht im Dienst");
        return;
      }

      if (player.IsInVehicle)
      {
        ChatService.ErrorMessage(player, "Du kannst in einem Auto nicht heilen");
        return;
      }
      if (player == target)
      {
        ChatService.ErrorMessage(player, "Du kannst dich nicht selbst heilen");
        return;
      }

      if (player.Position.DistanceTo(target.Position) > 5)
      {
        ChatService.ErrorMessage(player, "Der Patient ist zu weit entfernt");
        return;
      }

      if (target.Health == 100)
      {
        ChatService.ErrorMessage(player, "Der Spieler ist bereits voll geheilt");
        return;
      }

      ChatService.SendMessage(target, "~g~" + player.Name + " ~s~möchte dich für ~g~$" + price + " ~s~heilen. Drücke ~g~J~s~ zum Erlauben oder ~r~N ~s~zum Verbieten");
      player.SendNotification("Du hast " + target.Name + " einen Heal angeboten", false);
      target.SetData<bool>("healDecision", true);
      Medic.delHealTask(target);
      Medic.HealDecisions.Add(new Medic.HealDecision { dMedic = player, dTarget = target, dPrice = price });
    }

    #endregion Sanitäter Commands

    #region Staatsfraktionen (LSPD / FBI) Commands

    [Command("m", "~m~Benutzung: ~s~/m [Nachricht]", GreedyArg = true)]
    public void CmdFactionMegaphone(Player player, string message)
    {
      User user = player.GetUser();
      if (user == null || (user.FactionId != 1 && user.FactionId != 3))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (!player.IsDuty())
      {
        ChatService.ErrorMessage(player, "Du bist nicht im Dienst");
        return;
      }

      if (!player.IsInVehicle)
      {
        ChatService.ErrorMessage(player, "Du bist nicht in einem Fahrzeug");
        return;
      }

      ChatService.SendInRange(player.Position, 50, "!{#FFFF00}Megafon (" + player.Name + "): " + message);
    }

    [Command("cuff", "~m~Benutzung: ~s~/cuff")]
    public void CmdFactionCuff(Player player)
    {
      User user = player.GetUser();
      if (user == null || (user.FactionId != 1 && user.FactionId != 3))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (!player.IsDuty())
      {
        ChatService.ErrorMessage(player, "Du bist nicht im Dienst");
        return;
      }

      Player nearestCuffPlayer = PositionManager.cuffPoints.Find(s => s.Position.DistanceTo(player.Position) <= 1.6 && user.GetData<bool>("duty"));

      if (nearestCuffPlayer is null)
        return;

      player.CuffPlayer(nearestCuffPlayer);
    }

    [Command("wanted", "~m~Benutzung: ~s~/wa(nted) [Spieler] [Anzahl] [Grund]", Alias = "wa", GreedyArg = true)]
    public void CmdFactionWanted(Player player, string nameOrId, int amount, string reason)
    {
      User user = player.GetUser();
      if (user == null || (user.FactionId != 1 && user.FactionId != 3))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (!player.IsDuty())
      {
        ChatService.ErrorMessage(player, "Du bist nicht im Dienst");
        return;
      }

      if (amount <= 0)
      {
        ChatService.ErrorMessage(player, "Es muss eine positive Wantedanzahl vergeben werden");
        return;
      }

      Player target = PlayerService.GetPlayerByNameOrId(nameOrId);
      if (target == null || !target.IsLoggedIn())
      {
        ChatService.PlayerNotFound(player);
        return;
      }

      User targetCop = target.GetUser();
      if (targetCop.FactionId == 1 || targetCop.FactionId == 3)
      {
        ChatService.ErrorMessage(player, "Du kannst dieser Person keine Wanteds geben");
        return;
      }
      User targetUser = target.GetUser();
      targetUser.GiveWanteds(player, amount, reason);
    }

    [Command("clear", "~m~Benutzung: ~s~/clear [Spieler] [Grund]", GreedyArg = true)]
    public void CmdFactionClear(Player player, string nameOrId, string reason)
    {
      User user = player.GetUser();
      if (user == null || (user.FactionId != 1 && user.FactionId != 3))
      {
        ChatService.NotAuthorized(player);
        return;
      }

      if (!player.IsDuty())
      {
        ChatService.ErrorMessage(player, "Du bist nicht im Dienst");
        return;
      }

      Player target = PlayerService.GetPlayerByNameOrId(nameOrId);
      if (target == null || !target.IsLoggedIn())
      {
        ChatService.PlayerNotFound(player);
        return;
      }

      using (var dbContext = new DatabaseContext())
      {
        User targetUser = target.GetUser(dbContext);
        if (targetUser.Wanteds == 0)
        {
          ChatService.ErrorMessage(player, "Der Spieler hat keine Wanteds");
          return;
        }
        ChatService.SendMessage(target, "!{#8181E9}Deine Akte wurde von " + player.Name + " gelöscht. Grund: " + reason);
        ChatService.BroadcastFaction("!{#8181E9}HQ: Die Akte von " + target.Name + " wurde von " + player.Name + " gelöscht. Grund: " + reason + ".", new List<int>() { 1, 3 });

        target.TriggerEvent("jailTime", 0);
        targetUser.Wanteds = 0;

        if (PositionManager.cuffPoints.Contains(player))
        {
          PositionManager.cuffPoints.Remove(player);
        }

        targetUser.SetBlipAndNametagColor();

        dbContext.SaveChanges();
      }
    }

    #endregion Staatsfraktionen (LSPD / FBI) Commands

    #region Weazel News

    [Command("eventchat", "~m~Benutzung: ~s~/eventchat [Typ = ~g~on~s~, ~g~off~s~]", GreedyArg = true)]
    public void CmdFactionNREventchat(Player player, string onoff)
    {
      switch (onoff.ToLower())
      {
        case "on":
          NAPI.Pools.GetAllPlayers().ForEach(p =>
          {
            p.SetData("eventAllowed", true);
          });
          player.SendNotification("Der Eventchat ist nun an.");
          break;

        case "off":
          NAPI.Pools.GetAllPlayers().ForEach(p =>
          {
            p.ResetData("eventAllowed");
          });
          player.SendNotification("Der Eventchat ist nun aus.");
          break;
      }
    }

    #endregion Weazel News
  }
}
