using System;
using System.Linq;
using GTANetworkAPI;
using ReallifeGamemode.Server.Core.API;
using ReallifeGamemode.Server.Core.API.API;

namespace ReallifeGamemode.Server.Core.RageMP
{
  public class RageAPI : IAPI
  {
    public IColShapeAPI ColShape => new RageColShapeAPI();

    public IVehicleAPI Vehicle => new RageVehicleAPI();

    public IMarkerAPI Marker => new RageMarkerAPI();

    public ITextLabelAPI TextLabel => new RageTextLabelAPI();

    public void DisableDefaultCommandErrorMessages()
    {
      NAPI.Server.SetCommandErrorMessage(null);
      //NAPI.Server.SetGlobalDefaultCommandMessages(false);
    }

    public void SetGlobalChatEnabled(bool enable)
    {
      NAPI.Server.SetGlobalServerChat(enable);
    }

    public void DisableDefaultSpawnBehavior()
    {
      NAPI.Server.SetAutoSpawnOnConnect(false);
    }

    public IPlayer GetPlayerFromNameOrId(string nameOrId)
    {
      Player player;
      if (int.TryParse(nameOrId, out var playerId))
      {
        player = NAPI.Pools.GetAllPlayers().Where(u => u.Handle.Value == playerId).FirstOrDefault();
      }
      else
      {
        player = NAPI.Player.GetPlayerFromName(nameOrId);
      }

      if (player == null || player.Handle == null)
      {
        return null;
      }

      return new RagePlayer(player);
    }

    public void SetTime(int hour, int minute, int second)
    {
      NAPI.World.SetTime(hour, minute, second);
    }

    public void TriggerClientEventForAll(string eventName, params object[] args)
    {
      NAPI.ClientEvent.TriggerClientEventForAll("SERVER:" + eventName, args);
    }
  }
}
