/*********************************************************************************************************************/
/*** DO NOT edit this file! Edit the files under `umod/config` and/or `umod/lang`, created once plugin has loaded. ***/
/*** Please note, support cannot be provided if the plugin has been modified. Please use a fresh copy if modified. ***/
/*********************************************************************************************************************/

using uMod.Common;
using uMod.Common.Command;
using uMod.Configuration.Toml;

namespace uMod.Plugins
{
    [Info("Position", "Wulf", "1.0.1")]
    [Description("Show the current location of yourself or other players")]
    public class Position : Plugin
    {
        #region Configuration

        private DefaultConfig config;

        [Config, Toml]
        private class DefaultConfig
        {
            [TomlProperty("use_permissions", Comment = "Enable/disable permission system usage")]
            public bool UsePermissions = false;
        }

        #endregion Configuration

        #region Initialization

        private const string permSelf = "position.self";
        private const string permOthers = "position.others";

        private void Loaded(DefaultConfig defaultConfig)
        {
            config = defaultConfig;

            permission.RegisterPermission(permSelf, this);
            permission.RegisterPermission(permOthers, this);
        }

        #endregion Initialization

        #region Localization

        [Localization, Toml]
        private interface IPluginLocale : ILocale
        {
            [TomlProperty(Comment = "The reply given if a player does not have permission to use a command")]
            string NoPermission { get; }

            [TomlProperty(Comment = "The reply given if a player is not found")]
            string PlayerNotFound { get; }

            [TomlProperty(Comment = "The reply given for another player's position")]
            string PositionOfPlayer { get; }

            [TomlProperty(Comment = "The reply given for a player's own position")]
            string PositionSelf { get; }

            [TomlProperty(Comment = "The command used to get positions")]
            string PositionCommand { get; }
        }

        [Locale]
        private class DefaultLocale : IPluginLocale
        {
            [TomlProperty("no_permission")]
            public string NoPermission => "Sorry, the '{command}' requires permission '{permission}'";

            [TomlProperty("player_not_found")]
            public string PlayerNotFound => "Player '{nameOrId}' was not found";

            [TomlProperty("position_of_player")]
            public string PositionOfPlayer => "{name}'s position is {position}";

            [TomlProperty("position_self")]
            public string PositionSelf => "Your position is {position}";

            [TomlProperty("position_command")]
            public string PositionCommand => "position";
        }

        #endregion Localization

        #region Commands

        [Locale(typeof(IPluginLocale))]
        [Command("PositionCommand {player?}")]
        private void PositionCommand(IPlayer player, IArgs args)
        {
            // Check if a player is specified
            if (args.Length > 0)
            {
                // Check if player has permission to use for others
                if (config.UsePermissions && !player.HasPermission(permOthers))
                {
                    player.Reply(_<IPluginLocale>(player).NoPermission.Interpolate(("command", args.Command), ("permission", permOthers)));
                    return;
                }

                // Find a player matching that name or ID
                IPlayer target = Players.FindPlayer(args.GetString("player"), PlayerFilter.Connected);
                if (target != null)
                {
                    // Show player's location
                    player.Reply(_<IPluginLocale>(player).PositionOfPlayer.Interpolate(("name", target.Name), ("position", target.Position())));
                }
                else
                {
                    // Sorry, player was not found
                    player.Reply(_<IPluginLocale>(player).PlayerNotFound.Interpolate("nameOrId", args[0]));
                }

                return;
            }

            // Check if player has permission to use for self
            if (config.UsePermissions && !player.HasPermission(permSelf))
            {
                player.Reply(_<IPluginLocale>(player).NoPermission.Interpolate(("command", args.Command), ("permission", permSelf)));
                return;
            }

            // Show player's own location
            player.Reply(_<IPluginLocale>(player).PositionSelf.Interpolate("position", player.Position()));
        }

        #endregion Commands
    }
}
