﻿using System;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace Contoso.VisualStudio
{
    // register : regasm /codebase [AssemblyFullPath].dll
    // unregister : regasm /codebase [AssemblyFullPath].dll /u
    public abstract class BootstraperBase
    {
        protected static readonly Guid CSharpCategory = new Guid("{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}");
        protected static readonly Guid VBCategory = new Guid("{164B10B9-B200-11D0-8C61-00A0C91E29D5}");
        private const string KeyFormat = @"SOFTWARE\Microsoft\VisualStudio\{0}\Generators\{1}\{2}";

        protected static void Register(Version vsVersion, Guid customToolGuid, string customToolName, string customToolDescription, params Guid[] categoryGuids)
        {
            foreach (var categoryGuid in categoryGuids)
            {
                var subKey = string.Format(KeyFormat, vsVersion, categoryGuid.ToString("B"), customToolName);
                using (var key = Registry.LocalMachine.CreateSubKey(subKey))
                {
                    key.SetValue(string.Empty, customToolDescription);
                    key.SetValue("CLSID", customToolGuid.ToString("B"));
                    key.SetValue("GeneratesDesignTimeSource", 1);
                }
            }
        }

        protected static void Unregister(Version vsVersion, string customToolName, params Guid[] categoryGuids)
        {
            foreach (var categoryGuid in categoryGuids)
            {
                var subKey = string.Format(KeyFormat, vsVersion, categoryGuid.ToString("B"), customToolName);
                Registry.LocalMachine.DeleteSubKey(subKey, false);
            }
        }
    }
}
