using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Controller.Providers
{
    /// 
    /// Provides images for generic types by looking for standard images in the IBN
    /// 
    public class ImagesByNameProvider : ImageFromMediaLocationProvider
    {
        public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager)
        {
        }
        /// 
        /// Supportses the specified item.
        /// 
        /// The item.
        /// true if XXXX, false otherwise
        public override bool Supports(BaseItem item)
        {
            //only run for these generic types since we are expensive in file i/o
            return item is IndexFolder || item is BasePluginFolder;
        }
        /// 
        /// Gets the priority.
        /// 
        /// The priority.
        public override MetadataProviderPriority Priority
        {
            get
            {
                return MetadataProviderPriority.Last;
            }
        }
        /// 
        /// Gets a value indicating whether [refresh on file system stamp change].
        /// 
        /// true if [refresh on file system stamp change]; otherwise, false.
        protected override bool RefreshOnFileSystemStampChange
        {
            get
            {
                return false;
            }
        }
        /// 
        /// Override this to return the date that should be compared to the last refresh date
        /// to determine if this provider should be re-fetched.
        /// 
        /// The item.
        /// DateTime.
        protected override DateTime CompareDate(BaseItem item)
        {
            // If the IBN location exists return the last modified date of any file in it
            var location = GetLocation(item);
            return Directory.Exists(location) ? FileSystem.GetFiles(location).Select(f => f.CreationTimeUtc > f.LastWriteTimeUtc ? f.CreationTimeUtc : f.LastWriteTimeUtc).Max() : DateTime.MinValue;
        }
        /// 
        /// The us culture
        /// 
        private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
        /// 
        /// Gets the location.
        /// 
        /// The item.
        /// System.String.
        protected string GetLocation(BaseItem item)
        {
            var invalid = Path.GetInvalidFileNameChars();
            var name = item.Name ?? string.Empty;
            name = invalid.Aggregate(name, (current, c) => current.Replace(c.ToString(UsCulture), string.Empty));
            return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
        }
        /// 
        /// Gets the image.
        /// 
        /// The item.
        /// The filename without extension.
        /// System.Nullable{WIN32_FIND_DATA}.
        protected override WIN32_FIND_DATA? GetImage(BaseItem item, string filenameWithoutExtension)
        {
            var location = GetLocation(item);
            var result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".png"));
            if (!result.HasValue)
                result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".jpg"));
            return result;
        }
    }
}