namespace RequestService
{
    //using MassTransit.Log4NetIntegration.Logging;
    using Topshelf;
    using System.Xml;
    using ServiceController;
    using System.IO;
    using LAPS_WATCHER.Configure;
    using Microsoft.Extensions.Configuration;
    using Message.MessageTypes;

    class Program
    {
        static void Main(string[] args)
        {
            //working code
            //RequestConsumer.ProcessFile(args);
            //RequestConsumer.ProcessFile (new PublishMessage("", "31889383", "204", "checkout_stage_withfile_watcher", "", "lapsbookcuplive", null, "A","","","",""));
            //end

            ReadXML();
            //ConfigureLogger();
            //Log4NetLogWriterFactory.Use();
            //Log4NetLogger.Use();
            var rc = HostFactory.Run(x =>                                  
            {
                x.Service<RequestService>();
                x.StartAutomatically(); 
                x.RunAsLocalSystem();                                      
                x.SetDescription("WATCHER SERVICE " + Global.name);                  
                x.SetDisplayName(Global.name);                                  
                x.SetServiceName(Global.name);                                  
            });
        }
        static void ReadXML()
        {

            XmlReader reader = XmlReader.Create(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),"config.xml"));
            //XmlReader reader = XmlReader.Create(CustomConfiguration.AppSettings.GetValue<string>("AppSettings:configpath"));
            
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                && reader.Name == "LAPS")
                {
                    Global.ID = reader.GetAttribute(0);
                    Global.name = reader.GetAttribute(1);

                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "RabbitMQHost")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.RabbitMQHost = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if
                        else if (reader.Name == "ServiceQueueName")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.ServiceQueueName = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if
                        else if (reader.Name == "username")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.username = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if
                        else if (reader.Name == "password")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.password = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if
                        else if (reader.Name == "isparallel")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.isparallel = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if
                        else if (reader.Name == "maxthread")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.maxthread = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if
                        else if (reader.Name == "active")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    Global.active = reader.Value;
                                }
                            }
                            reader.Read();
                        } //end if

                    } //end while
                } //end if

            } //end while
        }
        //static void ConfigureLogger()
        //{
        //    const string logConfig = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
        //    <log4net>
        //      <root>
        //        <level value=""INFO"" />
        //        <appender-ref ref=""console"" />
        //      </root>
        //      <appender name=""console"" type=""log4net.Appender.ColoredConsoleAppender"">
        //        <layout type=""log4net.Layout.PatternLayout"">
        //          <conversionPattern value=""%m%n"" />
        //        </layout>
        //      </appender>
        //    </log4net>";

        //    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(logConfig)))
        //    {
        //        XmlConfigurator.Configure(stream);
        //    }
        //}
    }
}
