using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Collections.Generic;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Protection;

namespace AipGateway.AIP
{
    public static class Utilities
    {
        public static X509Certificate2 ReadCertificateFromStore(string thumbprint)
        {
            X509Certificate2 cert = null;
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);

            // Find unexpired certificates.
            X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);

            // From the collection of unexpired certificates, find the ones with the correct name.
            X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, false);

            // Return the first certificate in the collection, has the right name and is current.
            cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
            store.Close();
            return cert;
        }

        public static string EnsureTrailingSlash(string value)
        {
            if (value == null)
            {
                value = string.Empty;
            }

            if (!value.EndsWith("/", StringComparison.Ordinal))
            {
                return value + "/";
            }

            return value;
        }

        public static AipLabel LabelToAip(Label label)
        {
            if (label == null) return null;

            return new AipLabel
            {
                Id = label.Id,
                Name = label.Name,
                Sensitivity = label.Sensitivity,
                Description = label.Description,
                IsActive = label.IsActive,
                ActionSource = (AipActionSource)label.ActionSource,
                Children = new List<AipLabel>()
            };
        }
        public static AipTemplate TemplateToAip(TemplateDescriptor template)
        {
            if (template == null) return null;
            var aipTemplate = new AipTemplate
            {
                Id = template.Id,
                Name = template.Name,
                Description = template.Description
            };
            return aipTemplate;
        }
    }
}