using Mox.Shared.Math;
using Mox.Shared.Renderer;

namespace Mox.Shared.UI
{
	public class Image : Control
	{
		public override void Render()
		{
			RenderImage(Background);
		}

		protected void RenderImage(SubImage _subImage)
		{
			var rect = LayoutedRect;

			if (_subImage != null)
			{
				if (BackgroundSplit != null)
				{
					var sx0 = _subImage.Rect.Left;
					var sx1 = sx0 + BackgroundSplit.Left;
					var sx3 = _subImage.Rect.Left + _subImage.Rect.Width;
					var sx2 = sx3 - BackgroundSplit.Width;
					var sy0 = _subImage.Rect.Top;
					var sy1 = sy0 + BackgroundSplit.Top;
					var sy3 = _subImage.Rect.Top + _subImage.Rect.Height;
					var sy2 = sy3 - BackgroundSplit.Height;
					var dx0 = rect.Left;
					var dx1 = dx0 + BackgroundSplit.Left;
					var dx3 = rect.Left + rect.Width;
					var dx2 = dx3 - BackgroundSplit.Width;
					var dy0 = rect.Top;
					var dy1 = dy0 + BackgroundSplit.Top;
					var dy3 = rect.Top + rect.Height;
					var dy2 = dy3 - BackgroundSplit.Height;
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx0, sy0, sx1 - sx0, sy1 - sy0), new Rect(dx0, dy0, dx1 - dx0, dy1 - dy0));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx1, sy0, sx2 - sx1, sy1 - sy0), new Rect(dx1, dy0, dx2 - dx1, dy1 - dy0));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx2, sy0, sx3 - sx2, sy1 - sy0), new Rect(dx2, dy0, dx3 - dx2, dy1 - dy0));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx0, sy1, sx1 - sx0, sy2 - sy1), new Rect(dx0, dy1, dx1 - dx0, dy2 - dy1));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx1, sy1, sx2 - sx1, sy2 - sy1), new Rect(dx1, dy1, dx2 - dx1, dy2 - dy1));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx2, sy1, sx3 - sx2, sy2 - sy1), new Rect(dx2, dy1, dx3 - dx2, dy2 - dy1));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx0, sy2, sx1 - sx0, sy3 - sy2), new Rect(dx0, dy2, dx1 - dx0, dy3 - dy2));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx1, sy2, sx2 - sx1, sy3 - sy2), new Rect(dx1, dy2, dx2 - dx1, dy3 - dy2));
					RendererBase.Instance.DrawImage(_subImage.Image, new Rect(sx2, sy2, sx3 - sx2, sy3 - sy2), new Rect(dx2, dy2, dx3 - dx2, dy3 - dy2));
				}
				else
				{
					RendererBase.Instance.DrawImage(_subImage.Image, _subImage.Rect, rect);
				}
			}
		}

		public override void Serialize(Serializer.Serializer _serializer)
		{
			_serializer.Serialize(ref m_Background, nameof(Background));
			_serializer.Serialize(ref m_BackgroundSplit, nameof(BackgroundSplit), null);

			base.Serialize(_serializer);
		}

		public SubImage Background
		{
			get => m_Background;
			set
			{
				m_Background = value;
				Root.Instance.InvalidateLayout();
			}
		}

		public Rect BackgroundSplit
		{
			get => m_BackgroundSplit;
			set
			{
				m_BackgroundSplit = value;
				Root.Instance.InvalidateLayout();
			}
		}

		public override Measure MinWidth { get { return new Measure(Background.Rect.Width, EUnit.Pixel); } }
		public override Measure MinHeight { get { return new Measure(Background.Rect.Height, EUnit.Pixel); } }

		private SubImage m_Background;
		private Rect m_BackgroundSplit;
	}
}
