using System.Drawing;

namespace Icarus.Graphics.Animation
{
    /**
     * This thingey will render and grid of child renderables, where the count of the renderables depends on the GridSize property.
     */
    public class TiledFrame 
		: AbstractRenderable
	{
		/**
		 * The size of the tiled frame. Basically, this is the number of times the frame will be tiled.
		 */
		private Size mGridSize;

		/**
		 * This is the renderable that will be tiled.
		 */
		private IRenderable mChildRenderable;

		/** @property */
		public Size GridSize
		{
			get { return mGridSize; }
			set { mGridSize = value; }
		}

		/** @property */
		public IRenderable ChildRenderable
		{
			get { return mChildRenderable; }
			set { mChildRenderable = value; }
		}

		/**
		 * Returns the size of the renderable.
		 */
		public override Size GetSize()
		{
			Size result = this.ChildRenderable.GetSize();
			result.Width = result.Width * this.GridSize.Width;
			result.Height = result.Height * this.GridSize.Height;

			return result;
		}

		/**
		 * Renders the thing. Note that the destination parameter can be used to specify clipping. The size of the rectangle can be empty, in which case no clipping will be applied.
		 */
		public override void Render(Rectangle destination, IRenderParameters parameters) 
		{
			for (int r = 0; r < this.GridSize.Height; r++)
			{
				int yoffset = this.ChildRenderable.GetSize().Height * r;
				destination.Y = destination.Y + yoffset;

				for (int c = 0; c < this.GridSize.Width; c++)
				{
					int xoffset = this.ChildRenderable.GetSize().Width * c;
					destination.X = destination.X + xoffset;

					this.ChildRenderable.Render(destination, parameters);
				
					destination.X = destination.X - xoffset;
				}
			
				destination.Y = destination.Y - yoffset;
			}
		}
	}
}