﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CrmMonitoring.RemoteService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {

#if (!DEBUG)            
            var logger = NLog.LogManager.GetLogger("Service");
            logger.Info("Starting the service...");
            try
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
            { 
                new ProbeService() 
            };

                System.ServiceProcess.ServiceBase.Run(ServicesToRun);
                ServiceBase.Run(ServicesToRun);
                logger.Info("Service started...");
            }catch (Exception e)
            {
                logger.Error(e);
            }            
#else
            var service = new ProbeService();
            MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart", BindingFlags.Instance | BindingFlags.NonPublic);
            Console.WriteLine("Starting...");
            onStartMethod.Invoke(service, new object[] { new string[] { } });
            Console.WriteLine("Press enter to stop...");
            Thread.Sleep(int.MaxValue);
            MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop", BindingFlags.Instance | BindingFlags.NonPublic);
            Console.WriteLine("Stopping...");
            onStopMethod.Invoke(service, null);
            Console.WriteLine("Stoped.");

#endif
        }
    }
}
