using Microsoft.Win32;
using System;

namespace AnotherReplayReader.Utils
{
    public static class RegistryUtils
    {
        public static string? Retrieve32(RegistryHive hive, string path, string value)
        {
            try
            {
                using var view32 = RegistryKey.OpenBaseKey(hive, RegistryView.Registry32);
                using var key = view32.OpenSubKey(path, false);
                return key?.GetValue(value) as string;
            }
            catch (Exception e)
            {
                Debug.Instance.DebugMessage += $"Failed to retrieve registy {hive}:{path}:{value}: {e}";
                return null;
            }
        }

        public static string? RetrieveInHklm64(string path, string value)
        {
            try
            {
                using var view64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                using var key = view64?.OpenSubKey(path, false);
                return key?.GetValue(value) as string;
            }
            catch (Exception e)
            {
                Debug.Instance.DebugMessage += $"Failed to retrieve registy HKLM64:{path}:{value}: {e}";
                return null;
            }
        }

        public static string? RetrieveInRa3(RegistryHive hive, string value)
        {
            return Retrieve32(hive, @"Software\Electronic Arts\Electronic Arts\Red Alert 3", value);
        }
    }
}