using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace TuxHunter.Resources
{
	/// <summary>
	/// The manager for stuff like snappy quotes, loading screen quotes, etc.
	/// </summary>
	public class TextManager
	{
		#region Singleton Implementation
		/// <summary>
		/// Singleton Implementation
		/// </summary>
		private static TextManager mgInstance;

		/// <summary>
		/// Singleton Implementation
		/// </summary>
		public static TextManager Instance
		{
			get 
			{
				if (TextManager.mgInstance == null) TextManager.mgInstance = TextManager.LoadTextManager();
				return TextManager.mgInstance; 
			}
		}
		#endregion

		#region Data
		/// <summary>
		/// The name of the file where the text lives.
		/// </summary>
		private static string mgResourceFileName = "Text.xml";

		/// <summary>
		/// The list of snappy quotes.
		/// </summary>
		private ArrayList mSnappyQuotes = new ArrayList();

		/// <summary>
		/// The random number generator.
		/// </summary>
		private Random mRandomGenerator = new Random();
		#endregion

		#region Private Parts
		/// <summary>
		/// Creates a new text manager.
		/// </summary>
		public TextManager() 
		{
		}

		/// <summary>
		/// Loads the text manager stuff.
		/// </summary>
		/// <returns></returns>
		private static TextManager LoadTextManager()
		{
			//Set the type of Object for deserialization
			TextManager result = new TextManager();
			XmlSerializer xmls = new XmlSerializer(result.GetType());

			//Open level XML file
			if (!File.Exists(Icarus.Resources.ResourceManager.Instance.TextPath + TextManager.mgResourceFileName)) return null;
			FileStream fs = new FileStream(Icarus.Resources.ResourceManager.Instance.TextPath + TextManager.mgResourceFileName, FileMode.Open);
			if(fs == null) return null;
    
			//read level file and create level
			XmlReader reader = new XmlTextReader(fs);
			result = xmls.Deserialize(reader) as TextManager;

			//Close file
			fs.Close();

			return result;
		}

		/// <summary>
		/// Saves the text manager stuff.
		/// </summary>
		private void SaveTextManager()
		{
			//Setup serializer:
			XmlSerializer xmls = new XmlSerializer(this.GetType());
            
			//Create filestream to save level
			FileStream fs = new FileStream(Icarus.Resources.ResourceManager.Instance.TextPath + TextManager.mgResourceFileName, FileMode.Create);
                        
			//Save level
			xmls.Serialize(fs, this);

			//Close file
			fs.Close();
		}
		#endregion

		#region Serialization Stuff
		/// <summary>
		/// All of Snappy's quotes. For serialization purposes only.
		/// </summary>
		public string[] SnappyQuotes
		{
			get { return mSnappyQuotes.ToArray(typeof(string)) as string[]; }
			set 
			{
				mSnappyQuotes.Clear();
				mSnappyQuotes.AddRange(value); 
			}
		}
		#endregion

		/// <summary>
		/// Returns a random snappy quote.
		/// </summary>
		/// <returns></returns>
		public string GetRandomSnappyQuote()
		{
			return mSnappyQuotes[mRandomGenerator.Next(mSnappyQuotes.Count)] as string;
		}
	}
}
