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

    internal class FileAssociation
    {
        public static bool AssociationCheck(string Ext, string ProgID)
        {
            try
            {
                Registry.ClassesRoot.OpenSubKey(Ext);
                if (Registry.ClassesRoot.OpenSubKey(Ext).GetValue("").ToString() != ProgID)
                {
                    return false;
                }
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }

        public static string AssociationCreation(string Ext, string ProgID, string ContentType, string[] OpenWithList = null, bool DeleteOldCreateNew = true, string Description = "No descrption")
        {
            try
            {
                Registry.ClassesRoot.OpenSubKey(Ext);
                if (DeleteOldCreateNew)
                {
                    try
                    {
                        Registry.ClassesRoot.DeleteSubKeyTree(Ext, false);
                    }
                    catch (Exception)
                    {
                    }
                    if (((Ext == null) || (ProgID == null)) || (ContentType == null))
                    {
                        if (Ext == null)
                        {
                            return "Please check extension.";
                        }
                        if (ProgID == null)
                        {
                            return "Please check program ID.";
                        }
                        if (ContentType == null)
                        {
                            return "Please check content type.";
                        }
                        return "An unknown error occured.";
                    }
                    if (OpenWithList == null)
                    {
                        AssociationInformation(Ext, ProgID, ContentType, Description);
                        return ("Association with " + Ext + " files have been created successfully without an open list.");
                    }
                    foreach (string str in OpenWithList)
                    {
                        Registry.ClassesRoot.CreateSubKey(Ext + @"\OpenWithList\" + str, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                    AssociationInformation(Ext, ProgID, ContentType, Description);
                    return ("Association with " + Ext + " files have been created successfully with the appropriate open list.");
                }
                return "A sub key already exsists for this file extension.";
            }
            catch (NullReferenceException)
            {
                if (((Ext == null) || (ProgID == null)) || (ContentType == null))
                {
                    if (Ext == null)
                    {
                        return "Please check extension.";
                    }
                    if (ProgID == null)
                    {
                        return "Please check program ID.";
                    }
                    if (ContentType == null)
                    {
                        return "Please check content type.";
                    }
                    return "An unknown error occured.";
                }
                if (OpenWithList == null)
                {
                    AssociationInformation(Ext, ProgID, ContentType, Description);
                    return ("Association with " + Ext + " files have been created successfully without an open list.");
                }
                foreach (string str2 in OpenWithList)
                {
                    Registry.ClassesRoot.CreateSubKey(Ext + @"\OpenWithList\" + str2, RegistryKeyPermissionCheck.ReadWriteSubTree);
                }
                AssociationInformation(Ext, ProgID, ContentType, Description);
                return ("Association with " + Ext + " files have been created successfully with the appropriate open list.");
            }
            catch (Exception exception)
            {
                return ("An unknown error occured: " + exception.Message);
            }
        }

        public static void AssociationDeletion(string Ext, string ProgID)
        {
            try
            {
                Registry.ClassesRoot.DeleteSubKeyTree(Ext, false);
                Registry.ClassesRoot.DeleteSubKeyTree(ProgID, false);
                Registry.CurrentUser.DeleteSubKeyTree(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + Ext, false);
            }
            catch (Exception)
            {
            }
        }

        private static void AssociationInformation(string Ext, string ProgID, string ContentType, string Description)
        {
            Registry.ClassesRoot.CreateSubKey(Ext, RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue("", ProgID);
            Registry.ClassesRoot.CreateSubKey(Ext, RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue("Content Type", ContentType);
            Registry.ClassesRoot.CreateSubKey(ProgID + @"\shell\open\command").SetValue("", Application.ExecutablePath + " \"%1\"", RegistryValueKind.String);
            Registry.ClassesRoot.CreateSubKey(ProgID, RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue("", Description, RegistryValueKind.String);
            Registry.ClassesRoot.CreateSubKey(ProgID + @"\DefaultIcon").SetValue("", Application.ExecutablePath, RegistryValueKind.String);
        }
    }
}

