using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.Drawing {
	/// <summary>
	/// Contains a map of named sprites associated with texture
	/// </summary>
	public class NamedTextureSpriteCollection {
		private BaseTexture _texture;
		private Dictionary<string, Sprite> _map = new Dictionary<string, Sprite>();

		/// <summary>
		/// Initializes a new instance of the <see cref="TextureSpriteCollection"/> class.
		/// </summary>
		/// <param name="texture">The owning texture.</param>
		internal NamedTextureSpriteCollection(BaseTexture texture) {
			_texture = texture;
		}

		/// <summary>
		/// Adds the sprite to collection with specified sprite name bounding specified region.
		/// </summary>
		/// <param name="spriteName">Name of the sprite.</param>
		/// <param name="spriteRegion">The sprite region.</param>
		public void Add(string spriteName, Rect spriteRegion) {
			_map.Add(spriteName, _texture.ToSprite(spriteRegion));
		}

		/// <summary>
		/// Adds the sprite to collection with specified sprite name bounding specified region.
		/// </summary>
		/// <param name="spriteName">Name of the sprite.</param>
		/// <param name="x">The x of left top sprite region.</param>
		/// <param name="y">The y of left top sprite region.</param>
		/// <param name="width">The width of region.</param>
		/// <param name="height">The height of region.</param>
		public void Add(string spriteName, float x, float y, float width, float height) {
			Add(spriteName, Rect.FromBox(x, y, width, height));
		}

		/// <summary>
		/// Determines whether sprite collection contains the specified sprite by name.
		/// </summary>
		/// <param name="spriteName">Name of the sprite.</param>
		/// <returns>
		/// 	<c>true</c> if map contains specified sprite; otherwise, <c>false</c>.
		/// </returns>
		public bool Contains(string spriteName) {
			return _map.ContainsKey(spriteName);
		}

		/// <summary>
		/// Removes the specified sprite.
		/// </summary>
		/// <param name="spriteName">Name of the sprite to remove.</param>
		public void Remove(string spriteName) {
			_map.Remove(spriteName);
		}

		/// <summary>
		/// Clears this texture sprite collection.
		/// </summary>
		public void Clear() {
			_map.Clear();
		}

		/// <summary>
		/// Gets the count of sprites in collection.
		/// </summary>
		/// <value>The count of sprites in collection.</value>
		public int Count {
			get { return _map.Count; }
		}

		/// <summary>
		/// Gets the <see cref="Vortex.Drawing.Sprite"/> with the specified sprite name.
		/// </summary>
		/// <value>The <see cref="Sprite"/> instance</value>
		public Sprite this[string spriteName] {
			get { return _map[spriteName]; }
		}

		/// <summary>
		/// Gets the collection of sprite names.
		/// </summary>
		/// <value>The names collection.</value>
		public ICollection<string> Names {
			get { return _map.Keys; }
		}

		/// <summary>
		/// Gets the collection of sprites.
		/// </summary>
		/// <value>The sprites collection.</value>
		public ICollection<Sprite> Values {
			get { return _map.Values; }
		}

		/// <summary>
		/// Tries to get the sprite by name.
		/// </summary>
		/// <param name="spriteName">Name of the sprite.</param>
		/// <param name="sprite">The result sprite.</param>
		/// <returns><c>true</c> if sprite exists and successfully stored; otherwise <c>false</c></returns>
		public bool TryGet(string spriteName, out Sprite sprite) {
			return _map.TryGetValue(spriteName, out sprite);
		}
	}
}
