using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Microsoft.Win32;

namespace AnotherReplayReader
{
    internal static class Auth
    {
        public static string ID { get; private set; }

        static Auth()
        {
            ID = null;

            var windowsID = null as string;
            try
            {
                using (var view64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
                using (var winNt = view64?.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion", false))
                {
                    windowsID = winNt?.GetValue("ProductId") as string;
                }
            }
            catch(Exception)
            {
                return;
            }

            var randomKey = null as string;
            try
            {
                var folderPath = Cache.CacheDirectory;
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }

                var keyPath = Path.Combine(folderPath, "id");
                if(!File.Exists(keyPath))
                {
                    File.WriteAllText(keyPath, Guid.NewGuid().ToString());
                }

                randomKey = File.ReadAllText(keyPath);
            }
            catch(Exception)
            {
                return;
            }
            
            if(string.IsNullOrWhiteSpace(windowsID) || string.IsNullOrWhiteSpace(randomKey))
            {
                return;
            }


            using (var sha = SHA256.Create())
            {
                var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(windowsID + randomKey));
                ID = string.Concat(hash.Skip(3).Take(10).Select(x => $"{x:X2}"));
            }
        }

        public static string GetKey()
        {
            if(ID == null)
            {
                return string.Empty;
            }

            var text = $"{ID}{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
            var bytes = Encoding.UTF8.GetBytes(text);
            var pre = Encoding.UTF8.GetBytes("playertable!");
            var salt = new byte[9];
            using (var rng = new RNGCryptoServiceProvider())
            {
                rng.GetNonZeroBytes(salt);
            }

            for(var i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = (byte)(bytes[i] ^ salt[i % salt.Length]);
            }

            return Convert.ToBase64String(salt.Concat(bytes).Select((x, i) => (byte)(x ^ pre[i % pre.Length])).ToArray());
        }
    }
}