using System;
using System.Collections;
using Server;
using Knives.Utils;

namespace Knives.Chat3
{
    public class SendMessageGump : GumpPlus
    {
        public static void SendTo(Mobile from, Mobile to)
        {
            SendTo(from, to, "", null);
        }

        public static void SendTo(Mobile from, Mobile to, string txt)
        {
            SendTo(from, to, txt, null);
        }

        public static void SendTo(Mobile from, Mobile to, Message reply)
        {
            SendTo(from, to, "", reply);
        }

        public static void SendTo(Mobile from, Mobile to, string txt, Message reply)
        {
            General.ClearGump(from, typeof(SendMessageGump));

            new SendMessageGump(from, to, txt, reply);
        }

        #region Class Definitions

        private Mobile c_From, c_To;
        private Message c_Reply;
        private string c_Text, c_Subject;

        public Data GetData { get { return Data.GetData(Owner); } }

        #endregion

        #region Constructors

        public SendMessageGump(Mobile from, Mobile to, string txt, Message reply) : base(from, 200, 200)
        {
            c_From = from;
            c_To = to;
            c_Text = txt;
            c_Subject = "";
            c_Reply = reply;

            if (c_Reply != null)
            {
                if (c_Reply.Subject.IndexOf("RE:") != 0)
                    c_Subject = "RE: " + c_Reply.Subject;
                else
                    c_Subject = c_Reply.Subject;
            }

            NewGump();
        }

        #endregion

        #region Methods

        protected override void BuildGump()
        {
            Owner.CloseGump(typeof(SendMessageGump));

            AddBackground(0, 0, 400, 400, 0x1400);

            if (c_To == null)
                AddHtml(0, 8, 400, 21, HTML.White + "<CENTER>" + General.Local(94), false, false);
            else
                AddHtml(0, 8, 400, 21, HTML.White + "<CENTER>" + General.Local(62) + " " + c_To.Name, false, false);

            AddImageTiled(20, 30, 350, 21, 0xBBC);
            AddTextField(20, 30, 350, 21, 0x480, 0, c_Subject);

            if (GetData.Recording == this)
            {
                AddHtml(20, 30, 350, 25, HTML.White + c_Subject, true, false);
                AddHtml(10, 57, 380, 310, HTML.White + c_Text, true, true);
                AddHtml(0, 370, 400, 21, HTML.White + "<CENTER>" + General.Local(63), false, false);
            }
            else
            {
                AddImageTiled(20, 30, 350, 21, 0xBBC);
                AddTextField(20, 30, 350, 21, 0x480, 0, c_Subject);

                AddImageTiled(10, 57, 380, 310, 0xBBC);
                AddTextField(10, 57, 380, 310, 0x480, 1, c_Text);
                AddButton(10, 370, 0x2333, 0x2333, "Record", new TimerCallback(Record));
            }

            AddButton(360, 370, 0xFBD, 0xFBE, "Send", new TimerCallback(Send));
        }

        #endregion

        #region Responses

        public void AddText(string txt)
        {
            c_Text += txt;

            NewGump();
        }

        private void Save()
        {
            c_Subject = GetTextField(0);
            c_Text = GetTextField(1);
        }

        private void Record()
        {
            Save();

            if (c_Subject.Trim() == "")
            {
                Owner.SendMessage(GetData.SystemC, General.Local(194));
                NewGump();
                return;
            }

            if (GetData.Recording != null)
                GetData.Recording = null;

            Save();
            GetData.Recording = this;
            Owner.SendMessage(GetData.SystemC, General.Local(65));

            NewGump();
        }

        private void Send()
        {
            if( GetData.Recording == null )
                Save();

            if (c_Subject.Trim() == "" || c_Text.Trim() == "")
            {
                Owner.SendMessage(GetData.SystemC, General.Local(66));
                NewGump();
                return;
            }

            if (!TrackSpam.LogSpam(Owner, "Message", TimeSpan.FromSeconds(Data.MsgSpam)))
            {
                Owner.SendMessage(GetData.SystemC, General.Local(97));
                NewGump();
                return;
            }

            if (GetData.Recording == this)
                GetData.Recording = null;

            if (Data.FilterMsg)
            {
                c_Text = Filter.FilterText(Owner, c_Text, false);
                c_Subject = Filter.FilterText(Owner, c_Subject, false);
            }

            if (c_To == null)
            {
                foreach (Data data in Data.Datas.Values)
                {
                    data.AddMessage(new Message(Owner, c_Subject, c_Text, MsgType.System));

                    if (data.Mobile.HasGump(typeof(FriendsGump)))
                        General.RefreshGump(data.Mobile, typeof(FriendsGump));
                    else
                        FriendsGump.SendTo(data.Mobile, true);

                    General.RefreshGump(data.Mobile, typeof(MailGump));
                }
            }
            else
            {
                Data.GetData(c_To).AddMessage(new Message(Owner, c_Subject, c_Text, MsgType.Normal));

                Owner.SendMessage(GetData.SystemC, General.Local(67) + " " + c_To.Name);
                if (Data.GetData(c_To).Status != OnlineStatus.Online)
                    Owner.SendMessage(GetData.SystemC, c_To.Name + ": " + Data.GetData(c_To).AwayMsg);

                if (c_To.HasGump(typeof(FriendsGump)))
                    General.RefreshGump(c_To, typeof(FriendsGump));
                else
                    FriendsGump.SendTo(c_To, true);

                General.RefreshGump(c_To, typeof(MailGump));
            }
 
            foreach( Data data in Data.Datas.Values)
                if (data.Mobile.AccessLevel >= c_From.AccessLevel && ((data.GlobalM && !data.GIgnores.Contains(c_From)) || data.GListens.Contains(c_From)))
                    data.Mobile.SendMessage(data.GlobalMC, String.Format("(Global) <Mail> {0} to {1}: {2}", Owner.Name, (c_To == null ? "All" : c_To.Name), c_Text ));
        }

        #endregion

    }
}