using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.IO;
namespace MediaBrowser.Controller.Localization
{
///
/// Class RatingsDefinition
///
public class RatingsDefinition
{
///
/// Gets or sets the logger.
///
/// The logger.
private ILogger Logger { get; set; }
///
/// Initializes a new instance of the class.
///
/// The file.
/// The logger.
public RatingsDefinition(string file, ILogger logger)
{
Logger = logger;
Logger.Info("Loading Certification Ratings from file " + file);
this.file = file;
if (!Load())
{
Init(Kernel.Instance.Configuration.MetadataCountryCode.ToUpper());
}
}
///
/// Inits the specified country.
///
/// The country.
protected void Init(string country)
{
//intitialze based on country
switch (country)
{
case "US":
RatingsDict = new USRatingsDictionary();
break;
case "GB":
RatingsDict = new GBRatingsDictionary();
break;
case "NL":
RatingsDict = new NLRatingsDictionary();
break;
case "AU":
RatingsDict = new AURatingsDictionary();
break;
default:
RatingsDict = new USRatingsDictionary();
break;
}
Save();
}
///
/// The file
///
readonly string file;
///
/// Save to file
///
public void Save()
{
// Use simple text serialization - no need for xml
using (var fs = new StreamWriter(file))
{
foreach (var pair in RatingsDict)
{
fs.WriteLine(pair.Key + "," + pair.Value);
}
}
}
///
/// Load from file
///
/// true if XXXX, false otherwise
protected bool Load()
{
// Read back in our simple serialized format
RatingsDict = new Dictionary(StringComparer.OrdinalIgnoreCase);
try
{
using (var fs = new StreamReader(file))
{
while (!fs.EndOfStream)
{
var line = fs.ReadLine() ?? "";
var values = line.Split(',');
if (values.Length == 2)
{
int value;
if (int.TryParse(values[1], out value))
{
RatingsDict[values[0].Trim()] = value;
}
else
{
Logger.Error("Invalid line in ratings file " + file + "(" + line + ")");
}
}
}
}
}
catch
{
// Couldn't load - probably just not there yet
return false;
}
return true;
}
///
/// The ratings dict
///
public Dictionary RatingsDict = new Dictionary(StringComparer.OrdinalIgnoreCase);
}
}