using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using System;
using System.IO;
namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
{
    /// 
    /// Class SeriesResolver
    /// 
    public class SeriesResolver : FolderResolver
    {
        /// 
        /// Gets the priority.
        /// 
        /// The priority.
        public override ResolverPriority Priority
        {
            get
            {
                return ResolverPriority.Second;
            }
        }
        /// 
        /// Resolves the specified args.
        /// 
        /// The args.
        /// Series.
        protected override Series Resolve(ItemResolveArgs args)
        {
            if (args.IsDirectory)
            {
                var collectionType = args.GetCollectionType();
                // If there's a collection type and it's not tv, it can't be a series
                if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
                {
                    if (args.HasParent())
                    {
                        return null;
                    } 
                    
                    return new Series
                    {
                        Path = args.Path,
                        Name = Path.GetFileName(args.Path)
                    };
                }
            }
            return null;
        }
        /// 
        /// Sets the initial item values.
        /// 
        /// The item.
        /// The args.
        protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
        {
            base.SetInitialItemValues(item, args);
            SetProviderIdFromPath(item, args.Path);
        }
        /// 
        /// Sets the provider id from path.
        /// 
        /// The item.
        /// The path.
        private void SetProviderIdFromPath(Series item, string path)
        {
            var justName = Path.GetFileName(path);
            var id = justName.GetAttributeValue("tvdbid");
            if (!string.IsNullOrEmpty(id))
            {
                item.SetProviderId(MetadataProviders.Tvdb, id);
            }
        }
    }
}