using System;
using System.Timers;
using System.Windows.Forms;

using FilterAPI;
using FilterAPI.Networking;

using FilterMessenger.LocalHandlers;
using FilterMessenger.LocalNetworking;

namespace FilterMessenger
{
    public partial class Main : Form
    {
        private System.Timers.Timer SendTimer;

        public Main() { InitializeComponent(); }

        private void Main_Shown(Object Sender, EventArgs Args)
        {
            Program.Auth = new Authentication();
            Program.SQL = new Database(Program.Auth);

            Text = String.Format("{0} Filter [Messenger] :: Aeris.NET :: 2017/2018", Program.Auth.ConfigValues["ServerName"]);

            Program.LocalHandlers = new LocalHandlerLoader();
            Program.LocalClient = new LocalClient();
        }

        private void StartSend_Click(Object Sender, EventArgs Args)
        {
            if (!AutoSend.Checked && StartSend.Text == "Start/Send Message") { SendMessages(); }
            else if (StartSend.Text == "Stop")
            {
                StartSend.Text = "Start/Send Message";

                SendTimer.Stop();
            }
            else
            {
                StartSend.Text = "Stop";

                SendTimer = new System.Timers.Timer();
                SendTimer.AutoReset = true;
                SendTimer.Elapsed += SendTimer_Elapsed;
                SendTimer.Interval = Convert.ToInt32(Send_Interval.Value) * 1000;
                SendTimer.Start();
            }
        }

        private void SendTimer_Elapsed(Object Sender, ElapsedEventArgs Args) { SendMessages(); }

        private void SendMessages()
        {
            foreach (String Line in Messages.Lines)
            {
                if (Send_WMW.Checked)
                {
                    using (var LocalPacketWM = new Packet(4, 8))
                    {
                        using (var LocalPacketW = new Packet(4, 8))
                        {
                            LocalPacketWM.PacketWriter.Write((Byte)1);
                            LocalPacketW.PacketWriter.Write((Byte)0);

                            LocalPacketWM.PacketWriter.Write(IGName.Text);
                            LocalPacketW.PacketWriter.Write(IGName.Text);

                            LocalPacketWM.PacketWriter.Write(ConvertLine((String)Line));
                            LocalPacketW.PacketWriter.Write(ConvertLine((String)Line));

                            Program.LocalClient.SendPacket(LocalPacketWM);
                            Program.LocalClient.SendPacket(LocalPacketW);
                        }
                    }
                }
                else
                {
                    using (var LocalPacket = new Packet(4, 8))
                    {
                        if (Send_WM.Checked) { LocalPacket.PacketWriter.Write((Byte)1); }
                        else { LocalPacket.PacketWriter.Write((Byte)0); }

                        LocalPacket.PacketWriter.Write(IGName.Text);
                        LocalPacket.PacketWriter.Write(ConvertLine((String)Line));

                        Program.LocalClient.SendPacket(LocalPacket);
                    }
                }
            }
        }

        private String ConvertLine(String Line)
        {
            Line = Line.Replace("{ONLINE_COUNT}", Convert.ToString(Program.SQL.GetOnlineCount()));

            return Line;
        }
    }
}
