using System;
using System.Text;
using System.Linq;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.ServiceProcess;

namespace ServerFix
{
    static class Program
    {
        public static String LocalPath = String.Format("{0}\\", AppDomain.CurrentDomain.BaseDirectory);
        public static String ConfigFile = String.Format("{0}Config.Serpent", LocalPath);
        public static String LogFolder = String.Format("{0}Log\\", LocalPath);
        public static String ErrorFolder = String.Format("{0}Errors\\", LogFolder);
        public static String ExceptionFolder = String.Format("{0}Exceptions\\", LogFolder);
        public static String PacketFolder = String.Format("{0}Packets\\", LogFolder);
        public static String BlockFolder = String.Format("{0}Blocks\\", LogFolder);

        public static String Name = "Dupe Fix :D";
        public static String Service_Name = "Truffles_Dupe_Fix";
        public static String Service_Display = "_Truffles_Dupe_Fix";

        public static Int32 Byte_Size;
        public static String Bind_IP;
        public static String Connect_IP;
        public static Boolean Lock_Out;
        public static Boolean Logging;

        public static FileSystem System;

        static void Main()
        {
            System = new FileSystem();
            Byte_Size = Convert.ToInt32(System.ConfigValues["Byte_Size"]);
            Bind_IP = System.ConfigValues["Bind_IP"];
            Connect_IP = System.ConfigValues["Connect_IP"];
            Lock_Out = Convert.ToBoolean(System.ConfigValues["Lock_Out"]);
            Logging = Convert.ToBoolean(System.ConfigValues["Logging"]);

            if (Environment.UserInteractive)
            {
                if (!ServiceExist(Service_Name))
                {
                    Process CommandPromt = new Process();

                    ProcessStartInfo StartInfo = new ProcessStartInfo()
                    {
                        WindowStyle = ProcessWindowStyle.Hidden,
                        FileName = "cmd.exe",
                        Arguments = String.Format("/C sc create {0} binPath= \"{1}\" DisplayName= {2}", Service_Name, Assembly.GetEntryAssembly().Location, Service_Display)
                    };

                    CommandPromt.StartInfo = StartInfo;
                    CommandPromt.Start();

                    MessageBox.Show(String.Format("Uploaded {0} Successfully!", Name));
                    Environment.Exit(-1);
                }
                else
                {
                    MessageBox.Show(String.Format("{0} Already Exists!", Name));
                    Environment.Exit(-1);
                }
            }

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Main() 
            };
            ServiceBase.Run(ServicesToRun);
        }

        public static String BytesToHex(Byte[] Buffer)
        {
            String Header = "";

            StringBuilder Builder = new StringBuilder(Header);

            foreach (Byte CurrentByte in Buffer)
            {
                Builder.AppendFormat("{0:X2} ", CurrentByte);
            }

            return Convert.ToString(Builder);
        }

        private static Boolean ServiceExist(String ServiceName)
        {
            ServiceController[] ServiceList = ServiceController.GetServices();

            foreach (ServiceController Service in ServiceList)
            {
                if (Service.ServiceName == ServiceName)
                {
                    return true;
                }
            }

            return false;
        }

        private static Boolean ServiceOnline()
        {
            ServiceController Service = ServiceController.GetServices().FirstOrDefault(ServiceName => ServiceName.ServiceName == Service_Name);

            if (Service.Status == ServiceControllerStatus.Running)
            {
                return true;
            }

            return false;
        }
    }
}
