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

using FilterAPI.Networking;

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

        private Int32 Interval;
        private Int64 MoneyToGive;
        private String Message;
        private Byte RebirthLevel;

        #endregion

        private Boolean IsDone = true;

        public REMoneyTimer(Int32 I, Int64 MTG, String M, Byte RL)
        {
            Interval = I;
            MoneyToGive = MTG;
            Message = M;
            RebirthLevel = RL;

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

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

            NewThread.Start();
        }

        private void Run()
        {
            IsDone = false;

            try
            {
                using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
                {
                    Command.CommandText = String.Format("SELECT nCharNo FROM {0}..tRebirth", Program.Auth.ConfigValues["FilterDB"]);

                    Dictionary<Int32, Byte> RebirthnCharNos = new Dictionary<Int32, Byte>();

                    using (SqlDataReader Reader = Command.ExecuteReader())
                    {
                        while (Reader.Read())
                        {
                            if (!RebirthnCharNos.ContainsKey(Convert.ToInt32(Reader["nCharNo"]))) { RebirthnCharNos.Add(Convert.ToInt32(Reader["nCharNo"]), 0); }

                            RebirthnCharNos[Convert.ToInt32(Reader["nCharNo"])] += 1;
                        }
                    }

                    List<String> OnlinesIDs = Program.SQL.GetOnlinesIDs();

                    foreach (Int32 RebirthnCharNo in RebirthnCharNos.Keys)
                    {
                        if (OnlinesIDs.Contains(Program.SQL.GetsIDFromnCharNo(RebirthnCharNo)) && RebirthnCharNos[RebirthnCharNo] == RebirthLevel)
                        {
                            Program.SQL.PutMoneyInStorage(Program.SQL.GetsIDFromnCharNo(RebirthnCharNo), MoneyToGive);

                            using (var LocalPacket = new Packet(4, 9))
                            {
                                LocalPacket.PacketWriter.Write(Program.SQL.GetsIDFromnCharNo(RebirthnCharNo));
                                LocalPacket.PacketWriter.Write((Byte)0);
                                LocalPacket.PacketWriter.Write("Server");
                                LocalPacket.PacketWriter.Write(Message);

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

            IsDone = true;
        }
    }
}
