using System;
using System.IO;

namespace LevelCode.License.Security
{
    public static class KeyManager
    {
        private static readonly string PastaKeys =
            @"C:\LevelCode\Levelcode-license";

        private static readonly string CaminhoPrivateKey =
            Path.Combine(PastaKeys, "private.key");

        private static readonly string CaminhoPublicKey =
            Path.Combine(PastaKeys, "public.key");

        // =========================
        // GARANTIR QUE AS CHAVES EXISTEM
        // =========================
        public static void GarantirChaves()
        {
            if (File.Exists(CaminhoPrivateKey) &&
                File.Exists(CaminhoPublicKey))
                return;

            Directory.CreateDirectory(PastaKeys);

            string chavePublica;
            string chavePrivada;

            RsaService.GerarParDeChaves(
                out chavePublica,
                out chavePrivada);

            File.WriteAllText(CaminhoPrivateKey, chavePrivada);
            File.WriteAllText(CaminhoPublicKey, chavePublica);
        }

        // =========================
        // OBTER CHAVES
        // =========================
        public static string ObterChavePrivada()
        {
            GarantirChaves();
            return File.ReadAllText(CaminhoPrivateKey);
        }

        public static string ObterChavePublica()
        {
            GarantirChaves();
            return File.ReadAllText(CaminhoPublicKey);
        }
    }
}
