using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;

[assembly: InternalsVisibleTo("Katteker.Test")]
namespace Katteker
{
    /// <summary>
    /// Utilities.
    /// </summary>
    public static class Utility
    {
        internal static string GetLocalAppDataDirectory()
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs");
        }

        internal static string GetApplicationName(Assembly assembly = null) => (assembly ?? Assembly.GetEntryAssembly()).GetName().Name;

        /// <summary>
        /// Is true if the url is a web-address. False otherwise.
        /// </summary>
        /// <param name="urlOrPath"></param>
        /// <returns></returns>
        internal static bool IsWebUrl(string urlOrPath)
        {
            var uri = new Uri(urlOrPath);
            return uri.Scheme == "http" || uri.Scheme == "https";
        }

        /// <summary>
        /// Compute file hash with SHA1. 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns>Returns the hash as base64 string</returns>
        internal static string ComputeFileHash(string filename)
        {
            var fileInfo = new FileInfo(filename);

            using (var sha1Managed = new SHA1Managed())
            using (var fileStream = fileInfo.OpenRead())
            {
                return Convert.ToBase64String(sha1Managed.ComputeHash(fileStream));
            }
        }
    }
}