using System;
using System.Collections.Generic;
using GTANetworkAPI;
using Newtonsoft.Json;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Types;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Services;

namespace ReallifeGamemode.Server.Report
{
  internal class Report : Script
  {
    public static List<ReportManage> listReports = new List<ReportManage>();

    [Command("report", "~m~Benutzung: ~s~/report")]
    public void CmdUserReport(Player client)
    {
      User user = client.GetUser();

      client.TriggerEvent("showReportType");
      //ChatService.BroadcastAdmin("Eingehendes Ticket von " + client.Name + ": " + message + " (Benutze /showtickets)", AdminLevel.ADMIN);
    }

    [Command("showtickets", "~m~Benutzung: ~s~/showtickets")]
    public void CmdAdminShowtickets(Player client)
    {
      if (!client.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true)
      {
        ChatService.NotAuthorized(client);
        return;
      }

      if (listReports.Count == 0)
      {
        return;
      }
      User user = client.GetUser();
      List<string> listPlayers = new List<string>();
      List<string> listTicketnames = new List<string>();

      for (int a = 0; a < listReports.Count; a++)
      {
        ReportManage temp = listReports[a];

        if (!temp.isAssigned())
        {
          listPlayers.Add(temp.getUser());
          listTicketnames.Add(temp.getReporttext());
        }
      }
      //ChatService.SendMessage(client, JsonConvert.SerializeObject(listPlayers));

      if (listPlayers.Count == 0 || listTicketnames.Count == 0)
      {
        return;
      }
      client.TriggerEvent("showReportMenu", JsonConvert.SerializeObject(listPlayers), JsonConvert.SerializeObject(listTicketnames));
    }

    [RemoteEvent("acceptReport")]
    public void AcceptReport(Player admin, string playerName)
    {
      Player player = PlayerService.GetPlayerByNameOrId(playerName);
      if (player == null)
        return;

      for (int a = 0; a < listReports.Count; a++)
      {
        ReportManage temp = listReports[a];

        if (temp.getAdmin().Equals(admin.Name))
        {
          admin.SendChatMessage("Du hast bereits ein Ticket angenommen");
          return;
        }

        if (temp.getUser().Equals(player.Name))
        {
          if (listReports[a].isAssigned())
          {
            admin.SendChatMessage("Dieses Ticket wurde bereits angenommen");
            return;
          }

          listReports[a].assign(admin.Name);
          break;
        }
      }

      ChatService.SendMessage(player, "!{#008fff}[REPORT]!{#FFFFFF} Admin " + admin.Name + " hat dein Ticket angenommen! (Benutze /rc zum Schreiben)");
      ChatService.BroadcastDutyAdmin("!{#008fff}[REPORT]!{#FFFFFF} " + admin.Name + " hat das Ticket von " + player.Name + " angenommen");
    }

    [Command("rc", "~m~Benutzung: ~s~/rc text", GreedyArg = true)]
    public void CmdUserRc(Player client, String text)
    {
      bool cont = false;
      String username = "N/A";
      String adminname = "N/A";
      ReportManage temp = null;
      bool userIsAdmin = false;

      //Für Admin
      if (client.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true)
      {
        for (int a = 0; a < listReports.Count; a++)
        {
          temp = listReports[a];
          if (temp.getAdmin().Equals(client.Name))
          {
            username = listReports[a].getUser();
            cont = true;
            break;
          }
        }
        Player user = null;
        if (cont)
        {
          user = PlayerService.GetPlayerByNameOrId(username);

          if (text.Equals("quit"))
          {
            ChatService.SendMessage(user, "!{#008fff}[REPORT]!{#FFFFFF} Chat vom Admin beendet");
            ChatService.SendMessage(client, "!{#008fff}[REPORT]!{#FFFFFF} Chat beendet");

            for (int a = 0; a < listReports.Count; a++)
            {
              temp = listReports[a];
              if (temp.getAdmin().Equals(client.Name))
              {
                listReports.Remove(temp);
                break;
              }
            }
          }
          else
          {
            ChatService.SendMessage(client, "!{#008fff}[REPORT]!{#FFFFFF} " + client.Name + ": " + text);
            ChatService.SendMessage(user, "!{#008fff}[REPORT]!{#FFFFFF} " + client.Name + ": " + text);
          }
        }
        else
        {
          userIsAdmin = true;
        }
      }
      //Für User
      if ((!client.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true) || userIsAdmin)
      {
        cont = false;
        for (int a = 0; a < listReports.Count; a++)
        {
          temp = listReports[a];
          if (temp.getUser().Equals(client.Name) && !temp.getAdmin().Equals("N/A"))
          {
            cont = true;
            adminname = listReports[a].getAdmin();
            break;
          }
        }
        if (!cont)
        {
          return;
        }
        Player admin = PlayerService.GetPlayerByNameOrId(adminname);
        ChatService.SendMessage(admin, "!{#008fff}[REPORT]!{#FFFFFF} " + client.Name + ": " + text);
        ChatService.SendMessage(client, "!{#008fff}[REPORT]!{#FFFFFF} " + client.Name + ": " + text);
      }
    }

    [RemoteEvent("requestReport")]
    public void RequestReport(Player user, string type, string data)
    {
      string text = JsonConvert.DeserializeObject<string>(data);
      if (type == "Ticket Report")
      {
        ReportManage temp = null;
        for (int a = 0; a < listReports.Count; a++)
        {
          temp = listReports[a];
          if (temp.getUser().Equals(user.Name))
          {
            ChatService.SendMessage(user, "!{#addc8d}Du hast bereits ein Ticket erstellt");
            return;
          }
        }
        int aod = 0;
        NAPI.Pools.GetAllPlayers().ForEach(p =>
        {
          if (p.IsAdminDuty())
          {
            aod++;
          }
        });
        if (aod == 0)
        {
          ChatService.SendMessage(user, "!{#addc8d}Momentan ist kein Administrator verfügbar");
          return;
        }
        ChatService.SendMessage(user, "!{#addc8d}Dein Ticket wurde abgeschickt");
        ChatService.SendMessage(user, "!{#addc8d}Momentan sind " + aod + " Admins verfügbar");
        ChatService.BroadcastDutyAdmin("!{#008fff}[TICKET]!{#FFFFFF} Eingehendes Ticket von~w~ " + user.Name + " (" + user.Handle.Value + ")~w~: " + text + " (Benutze /showtickets)");
        ReportManage ticket = new ReportManage(user.Name, text);
        //ChatService.Broadcast("ADDTICKET:" + user.Name + ", " + text);
        listReports.Add(ticket);
        //ChatService.SendMessage(user, "!{#addc8d}In Bearbeitung");
      }
      if (type == "Quick Report")
        ChatService.BroadcastAdmin("!{#008fff}[QR]~w~ " + user.Name + " (" + user.Handle.Value + ")~w~: " + text, AdminLevel.SUPPORTER);
    }

    [RemoteEvent("deleteReport")]
    public void DeleteReport(Player user, string name)
    {
      Player player = PlayerService.GetPlayerByNameOrId(name);
      if (player == null)
        return;

      for (int a = 0; a < listReports.Count; a++)
      {
        ReportManage temp = listReports[a];

        if (temp.getUser().Equals(player.Name))
        {
          ChatService.SendMessage(player, "!{#addc8d} " + user.Name + " hat dein Ticket gelöscht!");
          ChatService.BroadcastDutyAdmin("!{#008fff}[TICKET]!{#FFFFFF} " + user.Name + " hat das Ticket von " + player.Name + " gelöscht");
          listReports.Remove(temp);
          break;
        }
      }
    }
  }
}
