namespace SHNDecrypt
{
    using Microsoft.Win32;
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class MRUManager
    {
        private string NameOfProgram;
        private Action<object, EventArgs> OnClearRecentFilesClick;
        private Action<object, EventArgs> OnRecentFileClick;
        private ToolStripMenuItem ParentMenuItem;
        private string SubKeyName;

        public MRUManager(ToolStripMenuItem parentMenuItem, string nameOfProgram, Action<object, EventArgs> onRecentFileClick, Action<object, EventArgs> onClearRecentFilesClick = null)
        {
            if (((parentMenuItem == null) || (onRecentFileClick == null)) || (((nameOfProgram == null) || (nameOfProgram.Length == 0)) || nameOfProgram.Contains(@"\")))
            {
                throw new ArgumentException("Bad argument.");
            }
            this.ParentMenuItem = parentMenuItem;
            this.NameOfProgram = nameOfProgram;
            this.OnRecentFileClick = onRecentFileClick;
            this.OnClearRecentFilesClick = onClearRecentFilesClick;
            this.SubKeyName = $"Software\{this.NameOfProgram}\MRU";
            this._refreshRecentFilesMenu();
        }

        private void _onClearRecentFiles_Click(object obj, EventArgs evt)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(this.SubKeyName, true);
                if (key == null)
                {
                    return;
                }
                foreach (string str in key.GetValueNames())
                {
                    key.DeleteValue(str, true);
                }
                key.Close();
                this.ParentMenuItem.DropDownItems.Clear();
                this.ParentMenuItem.Enabled = false;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
            if (this.OnClearRecentFilesClick != null)
            {
                this.OnClearRecentFilesClick(obj, evt);
            }
        }

        private void _refreshRecentFilesMenu()
        {
            RegistryKey key;
            try
            {
                key = Registry.CurrentUser.OpenSubKey(this.SubKeyName, false);
                if (key == null)
                {
                    this.ParentMenuItem.Enabled = false;
                    return;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Cannot open recent files registry key:\n" + exception.ToString());
                return;
            }
            this.ParentMenuItem.DropDownItems.Clear();
            foreach (string str2 in key.GetValueNames())
            {
                string text = key.GetValue(str2, null) as string;
                if (text != null)
                {
                    this.ParentMenuItem.DropDownItems.Add(text).Click += new EventHandler(this.OnRecentFileClick.Invoke);
                }
            }
            if (this.ParentMenuItem.DropDownItems.Count != 0)
            {
                this.ParentMenuItem.DropDownItems.Add("-");
                this.ParentMenuItem.DropDownItems.Add("Clear list").Click += new EventHandler(this._onClearRecentFiles_Click);
                this.ParentMenuItem.Enabled = true;
            }
            else
            {
                this.ParentMenuItem.Enabled = false;
            }
        }

        public void AddRecentFile(string fileNameWithFullPath)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.CreateSubKey(this.SubKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree);
                int num = 0;
                while (true)
                {
                    string str = key.GetValue(num.ToString(), null) as string;
                    if (str == null)
                    {
                        key.SetValue(num.ToString(), fileNameWithFullPath);
                        key.Close();
                        goto Label_0068;
                    }
                    if (str == fileNameWithFullPath)
                    {
                        key.Close();
                        goto Label_0068;
                    }
                    num++;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        Label_0068:
            this._refreshRecentFilesMenu();
        }

        public void RemoveRecentFile(string fileNameWithFullPath)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(this.SubKeyName, true);
                foreach (string str in key.GetValueNames())
                {
                    if ((key.GetValue(str, null) as string) == fileNameWithFullPath)
                    {
                        key.DeleteValue(str, true);
                        this._refreshRecentFilesMenu();
                        goto Label_006A;
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        Label_006A:
            this._refreshRecentFilesMenu();
        }
    }
}

