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

namespace ReallifeGamemode.Server.Commands
{
  internal class UserCommands : Script
  {

    [Command("skill")]
    public void CmdUserSkill(Player player)
    {
      User user = player.GetUser(new DatabaseContext());

      player.SendChatMessage($"~c~BusSkill = {user.BusSkill}/800, PilotSkill = {user.PilotSkill}/300.");
      
    }

    [Command("id", "~m~Benutzung: ~s~/id [Name]")]
    public void CmdUserId(Player player, String targetname)
    {
      Player target = PlayerService.GetPlayerByNameOrId(targetname);

      if (target == null)
      {
        player.SendChatMessage("~c~Spieler ist nicht online.");
      } else
      {
        player.SendChatMessage("~c~ID: (" + target.Id + ") " + target.Name);
      }
    }

    [Command("dice", "~m~Benutzung: ~s~/dice")]
    public void CmdUserDice(Player player)
    {
      if (!player.IsLoggedIn()) return;

      Random rnd = new Random();
      int number = rnd.Next(1, 7);

      var peopleInRange = NAPI.Player.GetPlayersInRadiusOfPlayer(7, player);

      foreach (var managedPlayer in peopleInRange)
      {
        if (!managedPlayer.IsLoggedIn()) return;
        ChatService.SendMessage(managedPlayer, "* " + player.Name + " hat eine " + number + " gewürfelt.");
      }
    }

    [Command("car", "~m~Benutzung: ~s~/car")]
    public void CmdUserCar(Player player)
    {
      if (!player.IsLoggedIn()) return;

      Vehicle pVeh = null;

      if (player.IsInVehicle)
      {
        pVeh = player.Vehicle;
      }
      else
      {
        pVeh = NAPI.Pools.GetAllVehicles().Where(v => v.Position.DistanceTo(player.Position) <= 5).FirstOrDefault();
      }

      if (pVeh == null)
      {
        return;
      }

      if (pVeh.GetServerVehicle() is ServerVehicle veh)
      {
        ChatService.SendMessage(player, "~m~" + ((VehicleHash)pVeh.Model) + " | " + veh.ToString() + " | Farbe 1: " + pVeh.PrimaryColor + " | Farbe 2: " + pVeh.SecondaryColor + " | ID: " + pVeh.Handle.Value);
      }
      else
      {
        ChatService.SendMessage(player, "~m~" + ((VehicleHash)pVeh.Model) + " | Farbe 1: " + pVeh.PrimaryColor + " | Farbe 2: " + pVeh.SecondaryColor + " | ID: " + pVeh.Handle.Value);
      }
    }
    [Command("help", "~m~Benutzung: ~s~/help")]
    public void CmdHelp(Player player)
    {
      player.SendChatMessage("E - Interagieren (ATM, Türen, Shops, Friseur etc.");
      player.SendChatMessage("M - Interaktionsmenü öffnen und schließen");
      player.SendChatMessage("X - Fahrzeug auf -/ abschließen sowie im Fahrzeuginteraktionsmenü öffnen");
      player.SendChatMessage("N - Fahrzeugmotor Starten");
      player.SendChatMessage("O - Onlineliste öffnen / schließen");
      player.SendChatMessage("I - Inventar öffnen / schließen");
      player.SendChatMessage("F4 - Mikrofon an/aus schalten");
      player.SendChatMessage("/skill - Skillpunkte anzeigen");
    }

    [Command("hup")]
    public void CmdAnim(Player player)
    {
      player.StopAnimation();
      player.PlayAnimation("mp_am_hold_up", "handsup_base", (int)(AnimationFlags.Loop | AnimationFlags.OnlyAnimateUpperBody | AnimationFlags.AllowPlayerControl | AnimationFlags.Cancellable));
    }

    [Command("event", "~m~Benutzung: ~s~/event [Nachricht]", GreedyArg = true)]
    public void CmdFactionNREventchat(Player player, string msg)
    {
      if (player.HasData("eventAllowed") == false)
      {
        ChatService.NotAuthorized(player);
        return;
      }
      ChatService.BroadcastFaction("~b~[Event] " + player.Name + ": " + msg, new List<int>() { 9 }) ;

    }
  }
}

