using Microsoft.Win32;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using ZeroLevel.Services.AsService;
namespace System.Diagnostics
{
    public class EventLogInstaller 
        : ComponentInstaller
    {
        private EventSourceCreationData sourceData = new EventSourceCreationData(null, null);
        private UninstallAction uninstallAction;
        /// Gets or sets the path of the resource file that contains category strings for the source.
        /// The path of the category resource file. The default is an empty string ("").
        [TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.FileNameEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [ComVisible(false)]
        [ResDescription("Desc_CategoryResourceFile")]
        public string CategoryResourceFile
        {
            get
            {
                return this.sourceData.CategoryResourceFile;
            }
            set
            {
                this.sourceData.CategoryResourceFile = value;
            }
        }
        /// Gets or sets the number of categories in the category resource file.
        /// The number of categories in the category resource file. The default value is zero.
        [ComVisible(false)]
        [ResDescription("Desc_CategoryCount")]
        public int CategoryCount
        {
            get
            {
                return this.sourceData.CategoryCount;
            }
            set
            {
                this.sourceData.CategoryCount = value;
            }
        }
        /// Gets or sets the name of the log to set the source to.
        /// The name of the log. This can be Application, System, or a custom log name. The default is an empty string ("").
        [TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [ResDescription("Desc_Log")]
        public string Log
        {
            get
            {
                if (this.sourceData.LogName == null && this.sourceData.Source != null)
                {
                    this.sourceData.LogName = EventLog.LogNameFromSourceName(this.sourceData.Source, ".");
                }
                return this.sourceData.LogName;
            }
            set
            {
                this.sourceData.LogName = value;
            }
        }
        /// Gets or sets the path of the resource file that contains message formatting strings for the source.
        /// The path of the message resource file. The default is an empty string ("").
        [TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.FileNameEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [ComVisible(false)]
        [ResDescription("Desc_MessageResourceFile")]
        public string MessageResourceFile
        {
            get
            {
                return this.sourceData.MessageResourceFile;
            }
            set
            {
                this.sourceData.MessageResourceFile = value;
            }
        }
        /// Gets or sets the path of the resource file that contains message parameter strings for the source.
        /// The path of the message parameter resource file. The default is an empty string ("").
        [TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.FileNameEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [ComVisible(false)]
        [ResDescription("Desc_ParameterResourceFile")]
        public string ParameterResourceFile
        {
            get
            {
                return this.sourceData.ParameterResourceFile;
            }
            set
            {
                this.sourceData.ParameterResourceFile = value;
            }
        }
        /// Gets or sets the source name to register with the log.
        /// The name to register with the event log as a source of entries. The default is an empty string ("").
        [TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [ResDescription("Desc_Source")]
        public string Source
        {
            get
            {
                return this.sourceData.Source;
            }
            set
            {
                this.sourceData.Source = value;
            }
        }
        /// Gets or sets a value that indicates whether the Installutil.exe (Installer Tool) should remove the event log or leave it in its installed state at uninstall time.
        /// One of the  values that indicates what state to leave the event log in when the  is uninstalled. The default is Remove.
        /// 
        ///    contains an invalid value. The only valid values for this property are Remove and NoAction.
        [DefaultValue(UninstallAction.Remove)]
        [ResDescription("Desc_UninstallAction")]
        public UninstallAction UninstallAction
        {
            get
            {
                return this.uninstallAction;
            }
            set
            {
                if (!Enum.IsDefined(typeof(UninstallAction), value))
                {
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(UninstallAction));
                }
                this.uninstallAction = value;
            }
        }
        /// Copies the property values of an  component that are required at installation time for an event log.
        /// An  to use as a template for the . 
        /// The specified component is not an .-or- The  or  property of the specified component is either null or empty. 
        public override void CopyFromComponent(IComponent component)
        {
            EventLog eventLog = component as EventLog;
            if (eventLog == null)
            {
                throw new ArgumentException(Res.GetString("NotAnEventLog"));
            }
            if (eventLog.Log != null && !(eventLog.Log == string.Empty) && eventLog.Source != null && !(eventLog.Source == string.Empty))
            {
                this.Log = eventLog.Log;
                this.Source = eventLog.Source;
                return;
            }
            throw new ArgumentException(Res.GetString("IncompleteEventLog"));
        }
        /// Performs the installation and writes event log information to the registry.
        /// An  used to save information needed to perform a rollback or uninstall operation. 
        /// The platform the installer is trying to use is not Windows NT 4.0 or later. 
        /// The name specified in the   property is already registered for a different event log.
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            base.Context.LogMessage(Res.GetString("CreatingEventLog", this.Source, this.Log));
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                throw new PlatformNotSupportedException(Res.GetString("WinNTRequired"));
            }
            stateSaver["baseInstalledAndPlatformOK"] = true;
            bool flag = EventLog.Exists(this.Log, ".");
            stateSaver["logExists"] = flag;
            bool flag2 = EventLog.SourceExists(this.Source, ".");
            stateSaver["alreadyRegistered"] = flag2;
            if (flag2 && EventLog.LogNameFromSourceName(this.Source, ".") == this.Log)
            {
                return;
            }
            EventLog.CreateEventSource(this.sourceData);
        }
        /// Determines whether an installer and another specified installer refer to the same source.
        /// true if this installer and the installer specified by the  parameter would install or uninstall the same source; otherwise, false.
        /// The installer to compare. 
        public override bool IsEquivalentInstaller(ComponentInstaller otherInstaller)
        {
            EventLogInstaller eventLogInstaller = otherInstaller as EventLogInstaller;
            if (eventLogInstaller == null)
            {
                return false;
            }
            return eventLogInstaller.Source == this.Source;
        }
        /// Restores the computer to the state it was in before the installation by rolling back the event log information that the installation procedure wrote to the registry.
        /// An  that contains the pre-installation state of the computer. 
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
            base.Context.LogMessage(Res.GetString("RestoringEventLog", this.Source));
            if (savedState["baseInstalledAndPlatformOK"] != null)
            {
                if (!(bool)savedState["logExists"])
                {
                    EventLog.Delete(this.Log, ".");
                }
                else
                {
                    object obj = savedState["alreadyRegistered"];
                    bool flag = obj != null && (bool)obj;
                    if (!flag && EventLog.SourceExists(this.Source, "."))
                    {
                        EventLog.DeleteEventSource(this.Source, ".");
                    }
                }
            }
        }
        /// Removes an installation by removing event log information from the registry.
        /// An  that contains the pre-installation state of the computer. 
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            if (this.UninstallAction == UninstallAction.Remove)
            {
                base.Context.LogMessage(Res.GetString("RemovingEventLog", this.Source));
                if (EventLog.SourceExists(this.Source, "."))
                {
                    if (string.Compare(this.Log, this.Source, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        EventLog.DeleteEventSource(this.Source, ".");
                    }
                }
                else
                {
                    base.Context.LogMessage(Res.GetString("LocalSourceNotRegisteredWarning", this.Source));
                }
                RegistryKey registryKey = Registry.LocalMachine;
                RegistryKey registryKey2 = null;
                try
                {
                    registryKey = registryKey.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\EventLog", false);
                    if (registryKey != null)
                    {
                        registryKey2 = registryKey.OpenSubKey(this.Log, false);
                    }
                    if (registryKey2 != null)
                    {
                        string[] subKeyNames = registryKey2.GetSubKeyNames();
                        if (subKeyNames == null || subKeyNames.Length == 0 || (subKeyNames.Length == 1 && string.Compare(subKeyNames[0], this.Log, StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            base.Context.LogMessage(Res.GetString("DeletingEventLog", this.Log));
                            EventLog.Delete(this.Log, ".");
                        }
                    }
                }
                finally
                {
                    if (registryKey != null)
                    {
                        registryKey.Close();
                    }
                    if (registryKey2 != null)
                    {
                        registryKey2.Close();
                    }
                }
            }
        }
    }
}