using System;

namespace Icarus.Resources
{
	/// <summary>
	/// The resource manager class gives us a central repository of paths that the application needs to access its assets, like graphics, sounds, etc.
	/// </summary>
	public class ResourceManager
	{
		#region Singleton Implementation
		/// <summary>
		/// Singleton Implementation.
		/// </summary>
		private static ResourceManager mg_Instance = new ResourceManager();

		/// <summary>
		/// Singleton Implementation.
		/// </summary>
		private ResourceManager() 
		{
			this.m_BasePath = AppDomain.CurrentDomain.BaseDirectory;
		}

		/// <summary>
		/// Singleton Implementation.
		/// </summary>
		public static ResourceManager Instance 
		{
			get { return mg_Instance; }
		}
		#endregion

		private Random mRandomGenerator = new Random();

		/// <summary>
		/// The base path of the application.
		/// </summary>
		private string m_BasePath;

		/// <summary>
		/// Returns the base path for all the resources. By default, the base path is the directory from which the application is launched.
		/// </summary>
		public string BasePath 
		{
			get { return m_BasePath; }
			set { m_BasePath = value; }
		}

		/// <summary>
		/// The name of the directory, relative to the base path, where the images reside.
		/// </summary>
		private string m_ImageDirName = "images";

		/// <summary>
		/// The name of the directory, relative to the base path, where the levels reside.
		/// </summary>
		private string m_LevelDirName = "levels";

		/// <summary>
		/// The name of the directory, relative to the base path, where the music resides.
		/// </summary>
		private string m_MusicDirName = "music";

		/// <summary>
		/// The name of the directory, relative to the base path, where the text files reside.
		/// </summary>
		private string m_TextDirName = "text";

		/// <summary>
		/// The name of the directory, relative to the base path, where the sound files reside.
		/// </summary>
		private string m_SoundDirName = "sound_effects";

		/// <summary>
		/// Gets or sets the name of the directory, relative to the base path, where the images reside.
		/// </summary>
		public string ImageDirName { get { return m_ImageDirName; } set { m_ImageDirName = value; } }

		/// <summary>
		/// Gets or sets the name of the directory, relative to the base path, where the levels reside.
		/// </summary>
		public string LevelDirName { get { return m_LevelDirName; } set { m_LevelDirName = value; } }

		/// <summary>
		/// Gets or sets the name of the directory, relative to the base path, where the music resides.
		/// </summary>
		public string MusicDirName { get { return m_MusicDirName; } set { m_MusicDirName = value; } }

		/// <summary>
		/// Gets or sets the name of the directory, relative to the base path, where the text files reside.
		/// </summary>
		public string TextDirName { get { return m_TextDirName; } set { m_TextDirName = value; } }

		/// <summary>
		/// Gets or sets the name of the directory, relative to the base path, where the sound files reside.
		/// </summary>
		public string SoundDirName { get { return m_SoundDirName; } set { m_SoundDirName = value; } }

		/// <summary>
		/// Gets the full path to the image directory.
		/// </summary>
		public string ImagePath 
		{
			get { return this.BasePath + this.ImageDirName + "/"; }
		}

		/// <summary>
		/// Gets the full path to the levels directory.
		/// </summary>
		public string LevelPath
		{
			get { return this.BasePath + this.LevelDirName + "/"; }
		}

		/// <summary>
		/// Gets the full path to the music directory.
		/// </summary>
		public string MusicPath
		{
			get { return this.BasePath + this.MusicDirName + "/"; }
		}

		/// <summary>
		/// Gets the full path to the sound directory.
		/// </summary>
		public string SoundPath
		{
			get { return this.BasePath + this.SoundDirName+ "/"; }
		}

		/// <summary>
		/// Gets a random sound from the directory whose id is passed in.
		/// </summary>
		/// <param name="dirId"></param>
		/// <returns></returns>
		public string GetRandomSoundFrom(string dirId)
		{
			System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(SoundPath + dirId);
			if (dir.Exists)
			{
				System.IO.FileInfo[] files = dir.GetFiles("*.wav");

				if (files.Length > 0)
				{
					return files[mRandomGenerator.Next(files.Length)].FullName;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Gets the full path to the text directory.
		/// </summary>
		public string TextPath
		{
			get { return this.BasePath + this.TextDirName+ "/"; }
		}
	}
}
