// Requires: Slack

using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;

namespace Oxide.Plugins
{
    [Info("SlackChat", "Wulf/lukespragg", "0.1.2", ResourceId = 1953)]
    [Description("Sends all chat messages or keyword-based chat to Slack channel")]

    class SlackChat : CovalencePlugin
    {
        // Do NOT edit this file, instead edit SlackChat.json in oxide/config

        [PluginReference] Plugin Slack;

        void Init() => LoadDefaultConfig();

        #region Configuration

        string channel;
        List<object> keywords;
        bool keywordsOnly;
        string style;

        protected override void LoadDefaultConfig()
        {
            Config["Channel"] = channel = GetConfig("Channel", "");
            Config["Keywords"] = keywords = GetConfig("Keywords", new List<object> { "admin", "cheat" });
            Config["KeywordsOnly"] = keywordsOnly = GetConfig("KeywordsOnly", false);
            Config["Style"] = style = GetConfig("Style", "fancy");
            SaveConfig();
        }

        #endregion

        #region Chat Sending

        void OnUserChat(IPlayer player, string message)
        {
            var keyword = keywords.Any(o => message.Contains((string)o));
            if (keywordsOnly && !keyword) return;
            Slack.Call(style.ToLower() == "fancy" ? "FancyMessage" : "SimpleMessage", message, player, channel);
        }

        #endregion

        #region Helpers

        T GetConfig<T>(string name, T defaultValue)
        {
            if (Config[name] == null) return defaultValue;
            return (T)Convert.ChangeType(Config[name], typeof(T));
        }

        #endregion
    }
}