using System;
using System.Threading;
using System.Collections.Generic;

using FilterAPI;
using FilterAPI.Systems;
using FilterAPI.Networking;

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

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

        #endregion

        private Boolean IsDone = true;

        public TaxLotteryTimer(Int32 I, String MP, String MA, Int32 WPM)
        {
            Interval = I;
            MessageP = MP;
            MessageA = MA;
            WinningPlayerMaximum = WPM;

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

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

            NewThread.Start();
        }

        private void Run()
        {
            IsDone = false;

            try
            {
                Int64 Tax = Program.SQL.GettTaxLottery();

                if (Tax != 0)
                {
                    List<String> sIDs = Program.SQL.GetOnlinesIDs();

                    Random RandomGenerator = new Random();

                    for (Int32 Counter = 0; Counter < WinningPlayerMaximum; Counter++)
                    {
                        String sID = sIDs[RandomGenerator.Next(0, sIDs.Count)];

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

                        Program.SQL.PutMoneyInStorage(sID, Tax / WinningPlayerMaximum);
                        
                        if (Program.SQL.GettLoggedInChar(sID))
                        {
                            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("WIN_AMOUNT", Money.ConvertMoney(Tax / WinningPlayerMaximum)));

                                Program.LocalClient.SendPacket(LocalPacket);
                            }
                        }
                        else { Program.SQL.InsertJoinMessage(nCharNo, MessageP.Replace("WIN_AMOUNT", Money.ConvertMoney(Tax / WinningPlayerMaximum))); }

                        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("WIN_AMOUNT", Money.ConvertMoney(Tax / WinningPlayerMaximum)));

                            Program.LocalClient.SendPacket(LocalPacket);
                        }
                    }

                    Program.SQL.WipetTaxLottery();

                    sIDs.Clear();
                }
            }
            catch (Exception Error) { Program.SendConsoleText(ConsoleColor.Red, "Timers error - Tax lottery\n{0}", Error.ToString()); }

            IsDone = true;
        }
    }
}
