using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Squirrel;
namespace BodyshopPartner.Utils
{
    public static class SquirrelAwareHelper
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public static bool AddHttpExcetion()
        {
            logger.Debug("Attempting to add HTTP Exclusion for web-server");
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "netsh http add urlacl url=http://+:1337/ user=\"Everyone\"";
            startInfo.Verb = "runas";
            process.StartInfo = startInfo;
          return process.Start();
        }

        public static void InitializeSquirrelAware()
        {
            using (var mgr = new UpdateManager(Utils.UpdateHandler.UpdatePath))
            {
                // Note, in most of these scenarios, the app exits after this method
                // completes!
                SquirrelAwareApp.HandleEvents(
                  onInitialInstall: v =>
                  {
                      logger.Debug("Installing via Squirrel Aware Helper. ", v);
                      try
                      {
                       
                          bool successful = AddHttpExcetion();

                          if (successful)
                          {
                              logger.Debug("Successfully added exception.");
                          }
                          else
                          {
                              logger.Fatal("Unable to set exception for http port");
                          }

                      }
                      catch (Exception Ex)
                      {
                          logger.Fatal("Unable to set exception for http port", Ex.ToString());
                      }

                      mgr.CreateShortcutForThisExe();
                  },
                  onAppUpdate: v => mgr.CreateShortcutForThisExe(),
                  onAppUninstall: v => mgr.RemoveShortcutForThisExe(),
                  onFirstRun: () => { logger.Info("We've run for the first time."); });
            }
        }
    }
}
