using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using Microsoft.Win32;


namespace LJLib.InstallHelper
{
    [RunInstaller(true)]
    public partial class LJInstaller : System.Configuration.Install.Installer
    {
        public LJInstaller()
        {
            InitializeComponent();
            // DONE: 读取服务
            this.serviceInstaller1.ServiceName = LJInstallGlobal.ServerName;
            this.serviceInstaller1.DisplayName = LJInstallGlobal.DisplayName;
            this.serviceInstaller1.Description = LJInstallGlobal.Description;
        }

        private void LJInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            var curPath = LJInstallGlobal.ImagePath;
            if (!string.IsNullOrEmpty(curPath))
            {
                // DONE: 修改命令行
                RegistryKey service = Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet").OpenSubKey("services").OpenSubKey(LJInstallGlobal.ServerName, true);
                service.SetValue("ImagePath", curPath);
            }
            ServiceController sc = new ServiceController(LJInstallGlobal.ServerName);
            if (sc.Status != ServiceControllerStatus.Running)
            {
                sc.Start();
            }
        }

        private void LJInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            // DONE: 停止服务
            ServiceController sc = new ServiceController(LJInstallGlobal.ServerName);
            if (sc.Status != ServiceControllerStatus.Stopped)
            {
                sc.Stop();
            }
        }
    }
}
