using System;
using System.IO;
using System.Data;
using System.Linq;
using System.ServiceProcess;

using FilterAPI;
using FilterAPI.CustomCurrency;

using FilterTimers.LocalHandlers;
using FilterTimers.LocalNetworking;

using FilterTimers.Timers;

namespace FilterTimers
{
    public partial class Service : ServiceBase
    {
        public Service() { }

        protected override void OnStart(String[] Args)
        {
			Init();
        }

		internal void Init()
		{
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

			Program.Auth = new Authentication();
			Program.SQL = new Database(Program.Auth);
			Program.Currencies = new CurrencyLoader(Program.Auth);

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

			Program.LoadSHNs();

			for (Byte Counter = 1; Counter < Convert.ToByte(Program.Auth.ConfigValues["TimersMTC"]) + 1; Counter++) { MoneyTimer MTimer = new MoneyTimer(Convert.ToInt32(Program.Auth.ConfigValues[String.Format("TimersMT{0}I", Counter)]), Convert.ToInt32(Program.Auth.ConfigValues[String.Format("TimersMT{0}A", Counter)]), Program.Auth.ConfigValues[String.Format("TimersMT{0}M", Counter)]); }

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersTLE"])) { TaxLotteryTimer TLT = new TaxLotteryTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersTLI"]), "You have won WIN_AMOUNT, from the tax lottery!", "\"CHARACTER_NAME\" who is IS_ONLINE has won WIN_AMOUNT.", Convert.ToInt32(Program.Auth.ConfigValues["TimersTLMP"])); }

			//if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersRATE"])) { RAbstateTimer RAT = new RAbstateTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersRATI"]), "You have won \"ABSTATE_NAME\", from the abstate lottery!", "\"CHARACTER_NAME\" who is IS_ONLINE has won \"ABSTATE_NAME\"."); }

			//if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersIATE"])) { IAbstateTimer IAT = new IAbstateTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersIATI"]), Convert.ToInt32(Program.Auth.ConfigValues["TimersIATD"])); }

			//if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersDBCTE"])) { DBCheckerTimer DBCT = new DBCheckerTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersDBCTI"])); }

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersREATE"])) { REAbstateTimer REAT = new REAbstateTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersREATI"]), Convert.ToInt32(Program.Auth.ConfigValues["TimersREATD"])); }

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersREMTE"]))
			{
				foreach (DataRow Row in Program.Rebirth.Table.Rows.Cast<DataRow>().Where(ItemArray => Convert.ToString(ItemArray.ItemArray[2]) == "MONEY")) { REMoneyTimer REMT = new REMoneyTimer(Convert.ToInt32(Row[4]), Convert.ToInt32(Row[3]), String.Format("You have been given {0} for being rebirth level {1}.", Money.ConvertMoney(Convert.ToInt64(Row[3])), Row[0]), Convert.ToByte(Row[0])); }
			}

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersLUSTE"])) { LUSkillsTimer LUST = new LUSkillsTimer(); }

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersPATE"])) { PAbstateTimer PAT = new PAbstateTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersPATI"]), Convert.ToInt32(Program.Auth.ConfigValues["TimersPATD"])); }

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersDNTE"])) { DayNightTimer DNT = new DayNightTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersDNTI"])); }

			if (Convert.ToBoolean(Program.Auth.ConfigValues["TimersMCTE"])) { for (Byte Counter = 1; Counter < Convert.ToByte(Program.Auth.ConfigValues["TimersMCTC"]) + 1; Counter++) { MCWinTimer MCWT = new MCWinTimer(Convert.ToInt32(Program.Auth.ConfigValues[String.Format("TimersMCT{0}I", Counter)]), "You have won WIN_AMOUNT mall coins!", "\"CHARACTER_NAME\" who is IS_ONLINE has won WIN_AMOUNT mall coins.", Convert.ToInt32(Program.Auth.ConfigValues[String.Format("TimersMCT{0}CWA", Counter)]), Convert.ToInt32(Program.Auth.ConfigValues[String.Format("TimersMCT{0}WPM", Counter)])); } }


			DBBackupTimer DBBT = new DBBackupTimer(Convert.ToInt32(Program.Auth.ConfigValues["TimersDBBTI"]));

			GAbstateTimer GAT = new GAbstateTimer(5, 7);

			//DBDuperTimer DBDT = new DBDuperTimer(5);
		}

		private void CurrentDomain_UnhandledException(Object Sender, UnhandledExceptionEventArgs Args)
        {
            Program.SendConsoleText(ConsoleColor.Red, ((Exception)Args.ExceptionObject).ToString());

            File.WriteAllLines(String.Format("{0}{1}Crash.txt", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "")), new String[] { ((Exception)Args.ExceptionObject).ToString() });
        }

        protected override void OnStop() { }
    }
}
