namespace RelayClient.Crypto;

public static class KeyStorage
{
    private static string GetKeyFolder()
    {
        var folder = Path.Combine(FileSystem.AppDataDirectory, "keys");
        Directory.CreateDirectory(folder);
        return folder;
    }

    public static void SavePrivateKey(string username, string privateKey)
    {
        File.WriteAllText(Path.Combine(GetKeyFolder(), $"{username}.private.key"), privateKey);
    }

    public static void SavePublicKey(string username, string publicKey)
    {
        File.WriteAllText(Path.Combine(GetKeyFolder(), $"{username}.public.key"), publicKey);
    }

    public static string LoadPrivateKey(string username)
    {
        return File.ReadAllText(Path.Combine(GetKeyFolder(), $"{username}.private.key"));
    }

    public static string LoadPublicKey(string username)
    {
        return File.ReadAllText(Path.Combine(GetKeyFolder(), $"{username}.public.key"));
    }

    public static bool HasKeys(string username)
    {
        return File.Exists(Path.Combine(GetKeyFolder(), $"{username}.private.key")) &&
               File.Exists(Path.Combine(GetKeyFolder(), $"{username}.public.key"));
    }
}