using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Generic;
using Plus.Communication.Packets.Outgoing.Notifications;
using System.Text.RegularExpressions;
using Plus.Database.Interfaces;


namespace Plus.HabboHotel.Rooms.Chat.Filter
{
    public sealed class WordFilterManager
    {
        private List<WordFilter> _filteredWords;
        private List<Userdev> _user;

        public WordFilterManager()
        {
            this._filteredWords = new List<WordFilter>();
        }

        public void Init()
        {
            if (this._filteredWords.Count > 0)
                this._filteredWords.Clear();

            DataTable Data = null;
            using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                dbClient.SetQuery("SELECT * FROM `wordfilter`");
                Data = dbClient.getTable();

                if (Data != null)
                {
                    foreach (DataRow Row in Data.Rows)
                    {
                        this._filteredWords.Add(new WordFilter(Convert.ToString(Row["word"]), Convert.ToString(Row["replacement"]), PlusEnvironment.EnumToBool(Row["strict"].ToString()), PlusEnvironment.EnumToBool(Row["bannable"].ToString())));
                    }
                }
            }
        }

        public string CheckMessage(string Message)
        {
            foreach (WordFilter Filter in this._filteredWords.ToList())
            {

                if (Message.ToLower().Contains(Filter.Word) && Filter.IsStrict || Message == Filter.Word)
                {
                    Message = Regex.Replace(Message, Filter.Word, Filter.Replacement, RegexOptions.IgnoreCase);
                }
                else if (Message.ToLower().Contains(Filter.Word) && !Filter.IsStrict || Message == Filter.Word)
                {
                    string[] Words = Message.Split(' ');

                    Message = "";
                    foreach (string Word in Words.ToList())
                    {
                        if (Word.ToLower() == Filter.Word)
                            Message += Filter.Replacement + " ";

                        else
                            Message += Word + " ";
                    }
                }
            }

            return Message.TrimEnd(' ');
        }


        public bool CheckBannedWords(string Message)
        {
            Message = Message.Replace(" ", "").Replace(".", "z").Replace("_", "").ToLower();

            foreach (WordFilter Filter in this._filteredWords.ToList())
            {
                if (!Filter.IsBannable)
                    continue;

                if (Message.Contains(Filter.Word))
                    return true;
            }
            return false;
        }



        public bool FiltrePUB(string Message)
        {
            byte[] data = Encoding.Default.GetBytes(Message);
            Message = Encoding.UTF8.GetString(data);
            Message = Regex.Replace(Message, "[àâäàáâãäåÀÁÂÃÄÅ@4ª∂A]", "a");
            Message = Regex.Replace(Message, "[ß8B]", "b");
            Message = Regex.Replace(Message, "[©çÇ¢C]", "c");
            Message = Regex.Replace(Message, "[ÐD]", "d");
            Message = Regex.Replace(Message, "[éèëêðÉÈËÊ£3∑E]", "e");
            Message = Regex.Replace(Message, "[ƒF]", "f");
            Message = Regex.Replace(Message, "[ìíîïÌÍÎÏI]", "i");
            Message = Regex.Replace(Message, "[1L]", "l");
            Message = Regex.Replace(Message, "[ñÑπN]", "n");
            Message = Regex.Replace(Message, "[òóôõöøÒÓÔÕÖØ0|ºΩO]", "o");
            Message = Regex.Replace(Message, "[®R]", "r");
            Message = Regex.Replace(Message, "[šŠ$5∫§S]", "s");
            Message = Regex.Replace(Message, "[ùúûüµÙÚÛÜU]", "u");
            Message = Regex.Replace(Message, "[ÿŸ¥Y]", "y");
            Message = Regex.Replace(Message, "[žŽZ]", "z");
            Message = Regex.Replace(Message, "[-('’_^=&~{[}]|µ]", "");
            Message = Message.Replace("â€¡", "o");
            Message = Message.Replace("œ", "oe");
            Message = Message.Replace("Œ", "Oe");
            Message = Message.Replace(" ", "");
            Message = Message.Replace("™", "TM");
            Message = Message.Replace("æ", "ae");
            Message = Message.Replace("∞", "oo");
            Message = Message.Replace("dot", ".");
            Message = Message.Replace(".", "");
            Message = Message.Replace("G", "g");
            Message = Message.Replace("H", "h");
            Message = Message.Replace("J", "j");
            Message = Message.Replace("K", "k");
            Message = Message.Replace("M", "m");
            Message = Message.Replace("P", "p");
            Message = Message.Replace("Q", "q");
            Message = Message.Replace("T", "t");
            Message = Message.Replace("V", "v");
            Message = Message.Replace("Æ", "");
            //return Message;
            foreach (WordFilter Filter in this._filteredWords.ToList())
            {

                if (Message.Contains((Filter.Word)))
                    return true;

            }
            return false;
        }
        public string ReplaceNumbers(string Message)
        {
            Message = Message.Replace("5", "s");
            Message = Message.Replace("1", "l");
            Message = Message.Replace("4", "a");
            Message = Message.Replace("8", "b");
            Message = Message.Replace("0", "o");
            Message = Message.Replace("()", "o");
            return Message;
        }

    }
}
