﻿using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Mono.Unix;
using Mono.Unix.Native;
using Nancy.Hosting.Self;
using roadie.Hubs;
using roadie.Library.Utility;
using System;
using System.Linq;

namespace roadie
{
    internal class Program 
    {
        private static void Main(string[] args)
        {

            Console.WriteLine("------------------------------------------------------------" + Environment.NewLine +
                              "Roadie" + Environment.NewLine +
                              "::          UTC Time: " + DateTime.UtcNow.ToString("R") + Environment.NewLine +
                              "::  Operating System: " + Environment.OSVersion.ToString() + Environment.NewLine +
                              "::  .NET Environment: " + Environment.Version.ToString() + Environment.NewLine +
                              "::               RAM: " + AvailableRAM() + "/" + PhysicalRAM() + Environment.NewLine +
                              "::          CPU Mode: " + (Is64BitMode() ? "64bit" : "32bit") + Environment.NewLine +
                              "------------------------------------------------------------" + Environment.NewLine
                              );

            var url = SettingsHelper.Instance.ListenAddress;

            using (WebApp.Start<Startup>(url))
            {

                if (Type.GetType("Mono.Runtime") != null)
                {
                    Console.WriteLine("Press any [CTRL+C] to shutdown.");
                    UnixSignal.WaitAny(new[] {
                            new UnixSignal(Signum.SIGINT),
                            new UnixSignal(Signum.SIGTERM),
                            new UnixSignal(Signum.SIGQUIT),
                            new UnixSignal(Signum.SIGHUP)
                        });
                }
                else
                {
                    Console.WriteLine("Press any [Enter] to shutdown.");
                    Console.ReadLine();
                }
                Console.WriteLine("Stopping Roadie");

                var hub = GlobalHost.ConnectionManager.GetHubContext<PlayActivityHub>();
                hub.Clients.All.Shutdown();

            }

        }

        public static bool Is64BitMode()
        {
            return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8;
        }

        public static string AvailableRAM()
        {
            if (Type.GetType("Mono.Runtime") != null)
            {
                var pc = new System.Diagnostics.PerformanceCounter("Mono Memory", "Available Physical Memory");
                return pc.RawValue.ToString();
            }
            return "N/A";
        }

        public static string PhysicalRAM()
        {
            if (Type.GetType("Mono.Runtime") != null)
            {
                var pc2 = new System.Diagnostics.PerformanceCounter("Mono Memory", "Total Physical Memory");
                return pc2.RawValue.ToString();
            }
            return "N/A";
        }


    }
}