﻿/***********************************************************************************************************************/
/*** DO NOT edit this file! Edit the files under `oxide/config` and/or `oxide/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 System.Collections.Generic;
using Facepunch;
using Oxide.Core.Libraries.Covalence;

namespace Oxide.Plugins
{
    [Info("Server Ban Check", "Wulf", "0.0.3")]
    [Description("Checks if the server IP address has been banned with Facepunch")]
    public class ServerBanCheck : CovalencePlugin
    {
        // https://api.facepunch.com/api/public/serverlist/banned

        #region Localization

        protected override void LoadDefaultMessages()
        {
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["CommandBanCheck"] = "bancheck",
                ["IPAddressesListed"] = "{0} IP addresses for banned servers listed in Facepunch manifest",
                ["ServerIsBanned"] = "Your server IP address {0} is BANNED with Facepunch, and will not appear in the Rust server list",
                ["ServerNotBanned"] = "Your server IP address {0} is NOT BANNED with Facepunch"
            }, this);
        }

        #endregion Localization

        #region Initialization

        private void OnServerInitialized()
        {
            AddLocalizedCommand(nameof(CommandBanCheck));

            Log(GetLang("IPAddressesListed", null, BannedServers.Banned.Length));

            if (!IsServerBanned())
            {
                Log(GetLang("ServerNotBanned", null, server.Address));
            }

            timer.Every(10f, () =>
            {
                if (IsServerBanned())
                {
                    LogWarning(GetLang("ServerIsBanned", null, server.Address));
                }
            });
        }

        private void CommandBanCheck(IPlayer player, string command, string[] args)
        {
            if (player.IsAdmin)
            {
                player.Reply(GetLang(IsServerBanned() ? "ServerIsBanned" : "ServerNotBanned", player.Id, server.Address));
            }
        }

        #endregion Ban Checking

        #region Helpers

        private void AddLocalizedCommand(string command)
        {
            foreach (string language in lang.GetLanguages(this))
            {
                foreach (KeyValuePair<string, string> message in lang.GetMessages(language, this))
                {
                    if (message.Key.Equals(command) && !string.IsNullOrEmpty(message.Value))
                    {
                        AddCovalenceCommand(message.Value, command);
                    }
                }
            }
        }

        private string GetLang(string langKey, string playerId = null, params object[] args)
        {
            return string.Format(lang.GetMessage(langKey, this, playerId), args);
        }

        private bool IsServerBanned()
        {
            return BannedServers.IsBannedServer(server.Address.ToString());
        }

        #endregion Helpers
    }
}
