using MediaBrowser.Common.Extensions;
using MediaBrowser.Model.Logging;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Controller.Localization
{
    /// 
    /// Class Ratings
    /// 
    public static class Ratings
    {
        static internal ILogger Logger { get; set; }
        /// 
        /// The ratings def
        /// 
        private static RatingsDefinition ratingsDef;
        /// 
        /// The _ratings dict
        /// 
        private static Dictionary _ratingsDict;
        /// 
        /// Gets the ratings dict.
        /// 
        /// The ratings dict.
        public static Dictionary RatingsDict
        {
            get { return _ratingsDict ?? (_ratingsDict = Initialize(false)); }
        }
        /// 
        /// The ratings strings
        /// 
        private static readonly Dictionary ratingsStrings = new Dictionary();
        /// 
        /// Initializes the specified block unrated.
        /// 
        /// if set to true [block unrated].
        /// Dictionary{System.StringSystem.Int32}.
        public static Dictionary Initialize(bool blockUnrated)
        {
            //build our ratings dictionary from the combined local one and us one
            ratingsDef = new RatingsDefinition(Path.Combine(Kernel.Instance.ApplicationPaths.LocalizationPath, "Ratings-" + Kernel.Instance.Configuration.MetadataCountryCode + ".txt"), Logger);
            //global value of None
            var dict = new Dictionary {{"None", -1}};
            foreach (var pair in ratingsDef.RatingsDict)
            {
                dict.TryAdd(pair.Key, pair.Value);
            }
            if (Kernel.Instance.Configuration.MetadataCountryCode.ToUpper() != "US")
            {
                foreach (var pair in new USRatingsDictionary())
                {
                    dict.TryAdd(pair.Key, pair.Value);
                }
            }
            //global values of CS
            dict.TryAdd("CS", 1000);
            dict.TryAdd("", blockUnrated ? 1000 : 0);
            //and rating reverse lookup dictionary (non-redundant ones)
            ratingsStrings.Clear();
            var lastLevel = -10;
            ratingsStrings.Add(-1,LocalizedStrings.Instance.GetString("Any"));
            foreach (var pair in ratingsDef.RatingsDict.OrderBy(p => p.Value))
            {
                if (pair.Value > lastLevel)
                {
                    lastLevel = pair.Value;
                    ratingsStrings.TryAdd(pair.Value, pair.Key);
                }
            }
            ratingsStrings.TryAdd(999, "CS");
            return dict;
        }
        /// 
        /// Switches the unrated.
        /// 
        /// if set to true [block].
        public static void SwitchUnrated(bool block)
        {
            RatingsDict.Remove("");
            RatingsDict.Add("", block ? 1000 : 0);
        }
        /// 
        /// Levels the specified rating STR.
        /// 
        /// The rating STR.
        /// System.Int32.
        public static int Level(string ratingStr)
        {
            if (ratingStr == null) ratingStr = "";
            if (RatingsDict.ContainsKey(ratingStr))
                return RatingsDict[ratingStr];
            string stripped = StripCountry(ratingStr);
            if (RatingsDict.ContainsKey(stripped))
                return RatingsDict[stripped];
            return RatingsDict[""]; //return "unknown" level
        }
        /// 
        /// Strips the country.
        /// 
        /// The rating.
        /// System.String.
        private static string StripCountry(string rating)
        {
            int start = rating.IndexOf('-');
            return start > 0 ? rating.Substring(start + 1) : rating;
        }
        /// 
        /// Returns a  that represents this instance.
        /// 
        /// The level.
        /// A  that represents this instance.
        public static string ToString(int level)
        {
            //return the closest one
            while (level > 0) 
            {
                if (ratingsStrings.ContainsKey(level))
                    return ratingsStrings[level];
                level--;
            }
            return ratingsStrings.Values.FirstOrDefault(); //default to first one
        }
        /// 
        /// To the strings.
        /// 
        /// List{System.String}.
        public static List ToStrings()
        {
            //return the whole list of ratings strings
            return ratingsStrings.Values.ToList();
        }
        /// 
        /// To the values.
        /// 
        /// List{System.Int32}.
        public static List ToValues()
        {
            //return the whole list of ratings values
            return ratingsStrings.Keys.ToList();
        }
        //public Microsoft.MediaCenter.UI.Image RatingImage(string rating)
        //{
        //    return Helper.GetMediaInfoImage("Rated_" + rating);
        //}
    }
}