using System;
using System.Security.Cryptography;
using DeviceId;
using DeviceId.Components;
using DeviceId.Encoders;
using DeviceId.Formatters;

namespace InABox.DeviceIdentifier
{
    public static class DeviceID
    {
        public static string Value(string token, Action<string>? ondebug)
        {
            var deviceId = new DeviceIdBuilder()
                .AddMachineName()
                .OnWindows(w => w
                    .AddProcessorId()
                    .AddMotherboardSerialNumber()
                    .AddSystemDriveSerialNumber()
                    .AddSystemUuid()
                    .AddComponent("Port", new DeviceIdComponent(token))
                )
                .UseFormatter(new HashDeviceIdFormatter(() => SHA256.Create(), new Base64UrlByteArrayEncoder()))
                .ToString();

            if (ondebug is not null)
            {
                ondebug(string.Format("N: {0}", new DeviceIdBuilder().AddMachineName()));
                ondebug(string.Format("P: {0}", new DeviceIdBuilder().OnWindows(w => w.AddProcessorId())));
                ondebug(string.Format("B: {0}", new DeviceIdBuilder().OnWindows(w => w.AddMotherboardSerialNumber())));
                ondebug(string.Format("D: {0}", new DeviceIdBuilder().OnWindows(w => w.AddSystemDriveSerialNumber())));
                ondebug(string.Format("U: {0}", new DeviceIdBuilder().OnWindows(w => w.AddSystemUuid())));
                ondebug(string.Format("T: {0}", new DeviceIdBuilder().AddComponent("Port", new DeviceIdComponent(token))));
                ondebug(string.Format("L: {0}", deviceId));
            }

            return deviceId;
        }
    }
}