#region <<< Using Directives >>>
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Net;
using System.Resources;
using System.Runtime.CompilerServices;
#endregion

namespace Volpe.Cafe.Resources
{
    /// <summary>
    /// Provides access to shared resources for all Volpe.Cafe.* assemblies.
    /// </summary>
    public static class AppResources
    {
        #region <<< Variables >>>

        //----- variables for resource management -----
        /// <summary>
        /// Gets the cached <see cref="System.Resources.ResourceManager"/> instance used by this class.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        internal static readonly ResourceManager ResourceManager =
            new ResourceManager("Volpe.Cafe.Resources.AppResources", typeof(AppResources).Assembly);

        /// <summary>
        /// Gets or sets the <see cref="CultureInfo"/> instance used by this class, that overrides the current thread's
        /// <see cref="System.Threading.Thread.CurrentUICulture"/> property for all resource lookups.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        internal static readonly CultureInfo ResourceCulture = null;

        #endregion

        #region <<< Methods >>>

        /// <summary>
        /// Retrieves a stream object for the application icon that is typically displayed in the application window's
        /// system menu box and in the user's taskbar.
        /// </summary>
        public static Stream GetAppIconStream()
        {
            return GetResourceStream("AppIcon");
        }

        /// <summary>
        /// Retrieves a stream object for the application setup icon.
        /// </summary>
        public static Stream GetSetupIconStream()
        {
            return GetResourceStream("SetupIcon");
        }

        /// <summary>
        /// Retrieves a stream object for the theme-icon image with the specified <paramref name="theme"/> and
        /// <paramref name="name"/>, and with the <see cref="IconSize.Default"/> size.
        /// </summary>
        public static Stream GetThemeIconStream(IconTheme theme, IconName name)
        {
            return GetThemeIconStream(theme, IconSize.Default, name);
        }
        /// <summary>
        /// Retrieves a stream object for the theme-icon image with the specified <paramref name="theme"/>,
        /// <paramref name="size"/>, and <paramref name="name"/>.
        /// </summary>
        public static Stream GetThemeIconStream(IconTheme theme, IconSize size, IconName name)
        {
            string resourceName = IconThemeHelper.GetFullIconName(theme, size, name);
            return GetResourceStream(resourceName);
        }
        /// <summary>
        /// Retrieves a stream object for the theme-icon image with the specified <paramref name="theme"/>, size (in
        /// pixels), and <paramref name="name"/>.
        /// </summary>
        public static Stream GetThemeIconStream(IconTheme theme, int sizeInPixels, IconName name)
        {
            return GetThemeIconStream(theme, IconThemeHelper.PixelsToIconSize(sizeInPixels), name);
        }

        /// <summary>
        /// Retrieves a stream object for the specified <paramref name="resource"/> embedded within this assembly.
        /// </summary>
        /// <param name="resource">The resource to retrieve.</param>
        /// <returns>A stream object for the specified resource, or null, if the resource cannot be found.</returns>
        public static Stream GetResourceStream(EmbeddedResources resource)
        {
            return GetResourceStream(resource.GetResourceName());
        }

        /// <summary>
        /// Returns a stream object for the resource with the specified <paramref name="resourceName"/> embedded within
        /// this assembly, or null, if the resource cannot be found.
        /// </summary>
        private static Stream GetResourceStream(string resourceName)
        {
            byte[] buffer = GetResourceBuffer(resourceName);
            return (buffer == null) ? null : new MemoryStream(buffer);
        }

        /// <summary>
        /// Returns a byte array buffer for the resource with the specified <paramref name="resourceName"/> embedded
        /// within this assembly, or null, if the resource cannot be found.
        /// </summary>
        private static byte[] GetResourceBuffer(string resourceName)
        {
            return (byte[])GetResource(resourceName);
        }

        /// <summary>
        /// Returns an object for the resource with the specified <paramref name="resourceName"/> embedded within this
        /// assembly, or null, if the resource cannot be found.
        /// </summary>
        private static object GetResource(string resourceName)
        {
            return (resourceName == null)
                ? null
                : ResourceManager.GetObject(resourceName, ResourceCulture);
        }

        #endregion
    }
}
