using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Dto;
using System;
using System.Collections.Generic;
namespace MediaBrowser.Server.Implementations.Library.Validators
{
    /// 
    /// Class CountHelpers
    /// 
    internal static class CountHelpers
    {
        /// 
        /// Adds to dictionary.
        /// 
        /// The item.
        /// The counts.
        internal static void AddToDictionary(BaseItem item, Dictionary counts)
        {
            if (item is Movie)
            {
                IncrementCount(counts, CountType.Movie);
            }
            else if (item is Trailer)
            {
                IncrementCount(counts, CountType.Trailer);
            }
            else if (item is Series)
            {
                IncrementCount(counts, CountType.Series);
            }
            else if (item is Game)
            {
                IncrementCount(counts, CountType.Game);
            }
            else if (item is Audio)
            {
                IncrementCount(counts, CountType.Song);
            }
            else if (item is MusicAlbum)
            {
                IncrementCount(counts, CountType.MusicAlbum);
            }
            else if (item is Episode)
            {
                IncrementCount(counts, CountType.Episode);
            }
            else if (item is MusicVideo)
            {
                IncrementCount(counts, CountType.MusicVideo);
            }
            else if (item is AdultVideo)
            {
                IncrementCount(counts, CountType.AdultVideo);
            }
            IncrementCount(counts, CountType.Total);
        }
        /// 
        /// Increments the count.
        /// 
        /// The counts.
        /// The key.
        internal static void IncrementCount(Dictionary counts, CountType key)
        {
            int count;
            if (counts.TryGetValue(key, out count))
            {
                count++;
                counts[key] = count;
            }
            else
            {
                counts.Add(key, 1);
            }
        }
        /// 
        /// Gets the counts.
        /// 
        /// The counts.
        /// ItemByNameCounts.
        internal static ItemByNameCounts GetCounts(Dictionary counts)
        {
            return new ItemByNameCounts
            {
                AdultVideoCount = GetCount(counts, CountType.AdultVideo),
                AlbumCount = GetCount(counts, CountType.MusicAlbum),
                EpisodeCount = GetCount(counts, CountType.Episode),
                GameCount = GetCount(counts, CountType.Game),
                MovieCount = GetCount(counts, CountType.Movie),
                MusicVideoCount = GetCount(counts, CountType.MusicVideo),
                SeriesCount = GetCount(counts, CountType.Series),
                SongCount = GetCount(counts, CountType.Song),
                TrailerCount = GetCount(counts, CountType.Trailer),
                TotalCount = GetCount(counts, CountType.Total)
            };
        }
        /// 
        /// Gets the count.
        /// 
        /// The counts.
        /// The key.
        /// System.Int32.
        internal static int GetCount(Dictionary counts, CountType key)
        {
            int count;
            if (counts.TryGetValue(key, out count))
            {
                return count;
            }
            return 0;
        }
        /// 
        /// Sets the item counts.
        /// 
        /// The user id.
        /// The media.
        /// The names.
        /// The master dictionary.
        internal static void SetItemCounts(Guid userId, BaseItem media, IEnumerable names, Dictionary>> masterDictionary)
        {
            foreach (var name in names)
            {
                Dictionary> libraryCounts;
                if (!masterDictionary.TryGetValue(name, out libraryCounts))
                {
                    libraryCounts = new Dictionary>();
                    masterDictionary.Add(name, libraryCounts);
                }
                var userLibId = userId/* ?? Guid.Empty*/;
                Dictionary userDictionary;
                if (!libraryCounts.TryGetValue(userLibId, out userDictionary))
                {
                    userDictionary = new Dictionary();
                    libraryCounts.Add(userLibId, userDictionary);
                }
                AddToDictionary(media, userDictionary);
            }
        }
    }
    internal enum CountType
    {
        AdultVideo,
        MusicAlbum,
        Episode,
        Game,
        Movie,
        MusicVideo,
        Series,
        Song,
        Trailer,
        Total
    }
}