using System;
using System.Data;
using System.Linq;
using System.Threading;
using System.Collections.Generic;

using FilterAPI.Networking;

namespace FilterTimers.Timers
{
    internal class RAbstateTimer
    {
        #region Timer Variables

        private Int32 Interval;
        private String MessageP;
        private String MessageA;

        #endregion

        private Boolean IsDone = true;

        public RAbstateTimer(Int32 I, String MP, String MA)
        {
            Interval = I;
            MessageP = MP;
            MessageA = MA;

            Thread NewThread = new Thread(delegate ()
            {
                while (true)
                {
                    Thread.Sleep(Interval * 1000);

                    if (IsDone) { Run(); }
                }
            });

            NewThread.Start();
        }

        private void Run()
        {
            IsDone = false;

            try
            {
                List<String> sIDs = Program.SQL.GetOnlinesIDs();

                Random RandomGenerator = new Random();

                String sID = sIDs[RandomGenerator.Next(0, sIDs.Count)];

                Int32 nCharNo = Program.SQL.GetnCharNoFromsID(sID);

                DataRow WinnableAbstatesRow = Program.WinnableAbstates.Table.Rows[RandomGenerator.Next(0, Program.WinnableAbstates.Table.Rows.Count - 1)];
                DataRow AbstateViewRow = Program.AbStateView.Table.Rows.Cast<DataRow>().Where(ItemArray => Convert.ToString(ItemArray.ItemArray[1]) == Convert.ToString(WinnableAbstatesRow[0])).FirstOrDefault();

                if (Program.SQL.GettLoggedInChar(sID))
                {
                    using (var LocalPacket = new Packet(5, 11))
                    {
                        LocalPacket.PacketWriter.Write(Convert.ToString(WinnableAbstatesRow[0]));
                        LocalPacket.PacketWriter.Write(Convert.ToInt32(WinnableAbstatesRow[1]));
                        LocalPacket.PacketWriter.Write(Convert.ToByte(WinnableAbstatesRow[2]));
                        LocalPacket.PacketWriter.Write(sID);

                        Program.LocalClient.SendPacket(LocalPacket);
                    }

                    using (var LocalPacket = new Packet(4, 9))
                    {
                        LocalPacket.PacketWriter.Write(sID);
                        LocalPacket.PacketWriter.Write((Byte)0);
                        LocalPacket.PacketWriter.Write("Server");
                        LocalPacket.PacketWriter.Write(MessageP.Replace("ABSTATE_NAME", Convert.ToString(AbstateViewRow[4])));

                        Program.LocalClient.SendPacket(LocalPacket);
                    }
                }
                else { Program.SQL.InsertJoinMessage(nCharNo, MessageP.Replace("ABSTATE_NAME", Convert.ToString(AbstateViewRow[4]))); }

                using (var LocalPacket = new Packet(4, 8))
                {
                    LocalPacket.PacketWriter.Write((Byte)1);
                    LocalPacket.PacketWriter.Write("");
                    LocalPacket.PacketWriter.Write(MessageA.Replace("CHARACTER_NAME", sID).Replace("IS_ONLINE", Program.SQL.GettLoggedInChar(sID) ? "Online" : "Offline").Replace("ABSTATE_NAME", Convert.ToString(AbstateViewRow[4])));

                    Program.LocalClient.SendPacket(LocalPacket);
                }
            }
            catch (Exception Error) { Program.SendConsoleText(ConsoleColor.Red, "Timers error - Random abstate giver\n{0}", Error.ToString()); }

            IsDone = true;
        }
    }
}
