using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ServiceProcess;
using System.Threading;
using FI.FIC.DataProviders.Alert;

namespace IronIntel.Contractor
{
    public class IronIntelService : ServiceBase
    {
        private static bool IsTrue(string s)
        {
            return (string.Compare(s, "true", true) == 0 || string.Compare(s, "yes", true) == 0 || string.Compare(s, "1", true) == 0);
        }

        private AlertManagerEx _Alert = null;

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            SystemParams.CreateDbObjects();

            if (IsTrue(ConfigurationManager.AppSettings["StartFICAlertService"]))
            {
                _Alert = new AlertManagerEx();
                _Alert.Start();
                Log.WriteMessage("FIC Alert Service Started.");
            }
            if (IsTrue(ConfigurationManager.AppSettings["StartFICChartSubscribeService"]))
            {
                FI.FIC.Email.ChartProvider.StartSend();
                Log.WriteMessage("FIC Chart Subscribe Service Started.");
            }
        }

        protected override void OnStop()
        {
            if(_Alert!=null)
            {
                _Alert.Stop();
                _Alert = null;
            }
            
            FI.FIC.Email.ChartProvider.EndSend();
            base.OnStop();
        }
    }
}
