using System;

using Plus.Core;
using Plus.Communication.Packets.Incoming;
using Plus.Utilities;
using Plus.HabboHotel.Global;
using Plus.HabboHotel.Quests;
using Plus.HabboHotel.Rooms;
using Plus.HabboHotel.GameClients;
using Plus.HabboHotel.Rooms.Chat.Logs;
using Plus.Communication.Packets.Outgoing.Messenger;
using Plus.Communication.Packets.Outgoing.Rooms.Chat;
using Plus.Communication.Packets.Outgoing.Moderation;
using Plus.HabboHotel.Rooms.Chat.Styles;
using Plus.Database.Interfaces;
using System.Data;
using Plus.Communication.Packets.Outgoing.Rooms.Notifications;
using System.Text.RegularExpressions;

namespace Plus.Communication.Packets.Incoming.Rooms.Chat
{
    public class ChatEvent : IPacketEvent
    {
        public void Parse(GameClient Session, ClientPacket Packet)
        {
            if (Session == null || Session.GetHabbo() == null || !Session.GetHabbo().InRoom)
                return;

            Room Room = Session.GetHabbo().CurrentRoom;
            if (Room == null)
                return;

            RoomUser User = Room.GetRoomUserManager().GetRoomUserByHabbo(Session.GetHabbo().Id);
            if (User == null)
                return;

            string Message = StringCharFilter.Escape(Packet.PopString());
            if (Message.Length > 100)
                Message = Message.Substring(0, 100);

            Message = System.Text.RegularExpressions.Regex.Replace(Message, "Ã$", "à");

            int Colour = Packet.PopInt();

            ChatStyle Style = null;
            if (!PlusEnvironment.GetGame().GetChatManager().GetChatStyles().TryGetStyle(Colour, out Style) || (Style.RequiredRight.Length > 0 && !Session.GetHabbo().GetPermissions().HasRight(Style.RequiredRight)))
                Colour = 0;

            User.UnIdle();

            if (PlusEnvironment.GetUnixTimestamp() < Session.GetHabbo().FloodTime && Session.GetHabbo().FloodTime != 0)
                return;

            if (Session.GetHabbo().TimeMuted > 0)
            {
                Session.SendMessage(new MutedComposer(Session.GetHabbo().TimeMuted));
                return;
            }

            if (!Session.GetHabbo().GetPermissions().HasRight("room_ignore_mute") && Room.CheckMute(Session))
            {
                Session.SendWhisper("Oups, vous êtes actuellement en sourdine.");
                return;
            }

            User.LastBubble = Session.GetHabbo().CustomBubbleId == 0 ? Colour : Session.GetHabbo().CustomBubbleId;

            if (!Session.GetHabbo().GetPermissions().HasRight("mod_tool"))
            {
                int MuteTime;
                if (User.IncrementAndCheckFlood(out MuteTime))
                {
                    Session.SendMessage(new FloodControlComposer(MuteTime));
                    return;
                }
            }
            if (PlusEnvironment.Antipub(Message))
            {

                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                {

                    if (Session.GetHabbo().Antipub == 0)
                    {
                        Double Expire = PlusEnvironment.GetUnixTimestamp() + 78892200;
                        Session.SendWhisper("Ton compte est banni !", 34);
                        PlusEnvironment.GetGame().GetModerationManager().BanUser("#ANTIPUB", HabboHotel.Moderation.ModerationBanType.IP, Convert.ToString(Session.GetHabbo().IpLast), "Publicité.", Expire);
                        Session.Disconnect();
                    }
                    if (Session.GetHabbo().Antipub == 2)
                    {
                        Session.GetHabbo().Antipub = Session.GetHabbo().Antipub - 1;
                        Session.GetHabbo().SaveAntiPubPoints();
                        Session.SendMessage(new RoomNotificationComposer("Robot Antipub",
                             "<b>Attention " + Session.GetHabbo().Username + " !</b>\n" +
                             "Notre robot viens de detecté une publicité dans votre message. </b>.\n<b><font color='red'>Il vous reste " + Session.GetHabbo().Antipub + " chances avant de vous sanctionnez!</font></b>\r" +
                             "\n\n", "pub", "J'ai compris.", "event:close"));
                        string name = "Antipub";
                        PlusEnvironment.GetGame().GetClientManager().StaffAlert(new CustomConsoleMessageComposer(-21474837, "" + Session.GetHabbo().Username + "vient d'annoncer un mot filtré!", name + "/" + Session.GetHabbo().Look + "/" + Session.GetHabbo().Id), Session.GetHabbo().Id);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("[ANTIPUB] Mot filtré de (" + Session.GetHabbo().Username + ")");
                    }
                    else
                    {
                        Session.GetHabbo().Antipub = Session.GetHabbo().Antipub - 1;
                        Session.GetHabbo().SaveAntiPubPoints();
                        Session.SendMessage(new RoomNotificationComposer("Robot Antipub",
                             "<b>Attention " + Session.GetHabbo().Username + " !</b>\n" +
                             "Notre robot viens de detecté une publicité dans votre message. </b>.\n<b><font color='red'>Il vous reste " + Session.GetHabbo().Antipub + " chances avant de vous sanctionnez!</font></b>\r" +
                             "\n\n", "pub", "J'ai compris.", "event:close"));
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("[ANTIPUB] Mot filtré de (" + Session.GetHabbo().Username + ")");
                    }
                    Message = "# Filtré par l'antipub #";
                }
            }
            if (PlusEnvironment.GetGame().GetChatManager().GetFilter().FiltrePUB(Message))
            {

                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                {

                    if (Session.GetHabbo().Antipub == 0)
                    {
                        Double Expire = PlusEnvironment.GetUnixTimestamp() + 78892200;
                        Session.SendWhisper("Ton compte est banni !", 34);
                        PlusEnvironment.GetGame().GetModerationManager().BanUser("#ANTIPUB", HabboHotel.Moderation.ModerationBanType.IP, Convert.ToString(Session.GetHabbo().IpLast), "Publicité.", Expire);
                        Session.Disconnect();
                    }
                    if (Session.GetHabbo().Antipub == 2)
                    {
                        Session.GetHabbo().Antipub = Session.GetHabbo().Antipub - 1;
                        Session.GetHabbo().SaveAntiPubPoints();
                        Session.SendMessage(new RoomNotificationComposer("Robot Antipub",
                             "<b>Attention " + Session.GetHabbo().Username + " !</b>\n" +
                             "Notre robot viens de detecté une publicité dans votre message. </b>.\n<b><font color='red'>Il vous reste " + Session.GetHabbo().Antipub + " chances avant de vous sanctionnez!</font></b>\r" +
                             "\n\n", "pub", "J'ai compris.", "event:close"));
                        string name = "Antipub";
                        PlusEnvironment.GetGame().GetClientManager().StaffAlert(new CustomConsoleMessageComposer(-21474837, "" + Session.GetHabbo().Username + "vient d'annoncer un mot filtré!", name + "/" + Session.GetHabbo().Look + "/" + Session.GetHabbo().Id), Session.GetHabbo().Id);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("[ANTIPUB] Mot filtré de (" + Session.GetHabbo().Username + ")");
                    }
                    else
                    {
                        Session.GetHabbo().Antipub = Session.GetHabbo().Antipub - 1;
                        Session.GetHabbo().SaveAntiPubPoints();
                        Session.SendMessage(new RoomNotificationComposer("Robot Antipub",
                             "<b>Attention " + Session.GetHabbo().Username + " !</b>\n" +
                             "Notre robot viens de detecté une publicité dans votre message. </b>.\n<b><font color='red'>Il vous reste " + Session.GetHabbo().Antipub + " chances avant de vous sanctionnez!</font></b>\r" +
                             "\n\n", "pub", "J'ai compris.", "event:close"));
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("[ANTIPUB] Mot filtré de (" + Session.GetHabbo().Username + ")");
                    }
                    Message = "# Filtré par l'antipub #";
                }
            }
            Room.AddChatlog(Session.GetHabbo().Id, Message);

            PlusEnvironment.GetGame().GetChatManager().GetLogs().StoreChatlog(new ChatlogEntry(Session.GetHabbo().Id, Room.Id, Message, UnixTimestamp.GetNow(), Session.GetHabbo(), Room));

            if (Message.StartsWith(":", StringComparison.CurrentCulture) && PlusEnvironment.GetGame().GetChatManager().GetCommands().Parse(Session, Message))
                return;

            if (PlusEnvironment.GetGame().GetChatManager().GetFilter().CheckBannedWords(Message))
            {
                Session.GetHabbo().BannedPhraseCount++;
                if (Session.GetHabbo().BannedPhraseCount >= PlusStaticGameSettings.BannedPhrasesAmount)
                {
                    PlusEnvironment.GetGame().GetModerationManager().BanUser("System", HabboHotel.Moderation.ModerationBanType.USERNAME, Session.GetHabbo().Username, "Spamming banned phrases (" + Message + ")", (PlusEnvironment.GetUnixTimestamp() + 78892200));
                    Session.Disconnect();
                    return;
                }

                Session.SendMessage(new ChatComposer(User.VirtualId, "@" + Session.GetHabbo().Colour + "@ " + Message, 0, Colour));
                return;
            }

            if (!Session.GetHabbo().GetPermissions().HasRight("word_filter_override"))
                Message = PlusEnvironment.GetGame().GetChatManager().GetFilter().CheckMessage(Message);


            PlusEnvironment.GetGame().GetQuestManager().ProgressUserQuest(Session, QuestType.SOCIAL_CHAT);
            string sPattern = "@";
            if (System.Text.RegularExpressions.Regex.IsMatch(Message, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                Message = PlusEnvironment.GetGame().GetChatManager().GetNotif().CheckNotif(Message, Session);
                Session.GetHabbo().Colour = "blue";
            }
            if (Session.GetHabbo().Colour == null)
            {
                User.OnChat(User.LastBubble, Message, false);
            }
            else
            {
                User.OnChat(User.LastBubble, "@" + Session.GetHabbo().Colour + "@ " + Message, false);
            }
        }
    }
}