using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Controller.Providers
{
    /// 
    /// Provides metadata for Folders and all subclasses by parsing folder.xml
    /// 
    public class FolderProviderFromXml : BaseMetadataProvider
    {
        public FolderProviderFromXml(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager)
        {
        }
        /// 
        /// Supportses the specified item.
        /// 
        /// The item.
        /// true if XXXX, false otherwise
        public override bool Supports(BaseItem item)
        {
            return item is Folder && item.LocationType == LocationType.FileSystem;
        }
        /// 
        /// Gets the priority.
        /// 
        /// The priority.
        public override MetadataProviderPriority Priority
        {
            get { return MetadataProviderPriority.First; }
        }
        /// 
        /// 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)
        {
            var entry = item.MetaLocation != null ? item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "folder.xml")) : null;
            return entry != null ? entry.LastWriteTimeUtc : DateTime.MinValue;
        }
        /// 
        /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
        /// 
        /// The item.
        /// if set to true [force].
        /// The cancellation token.
        /// Task{System.Boolean}.
        public override Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
        {
            return Fetch(item, cancellationToken);
        }
        /// 
        /// Fetches the specified item.
        /// 
        /// The item.
        /// The cancellation token.
        /// true if XXXX, false otherwise
        private async Task Fetch(BaseItem item, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var metadataFile = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "folder.xml"));
            if (metadataFile != null)
            {
                var path = metadataFile.FullName;
                await XmlParsingResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
                try
                {
                    new BaseItemXmlParser(Logger).Fetch((Folder)item, path, cancellationToken);
                }
                finally
                {
                    XmlParsingResourcePool.Release();
                }
                SetLastRefreshed(item, DateTime.UtcNow);
                return true;
            }
            return false;
        }
    }
}