/*
© 2017 Altavant Technologies Inc.
Web: http://www.altavant.com
*/

namespace Altavant.Fusion.Layouting
{
	using System;
	using Fusion.Graphics;
	using Fusion.Utils;

	public class Corners
	{
		private static readonly Corners _default;

		static Corners()
		{
			_default = new Corners();
		}

		public static Corners Default
		{
			get
			{
				return _default;
			}
		}
			
		private Corner _topStart;
		private Corner _topEnd;
		private Corner _bottomStart;
		private Corner _bottomEnd;

		public Corners()
		{
			_topStart = Corner.Default;
			_topEnd = Corner.Default;
			_bottomStart = Corner.Default;
			_bottomEnd = Corner.Default;
		}

		public Corners(Pen pen, float radius)
		{
			_topStart = new Corner(pen, radius);
			_topEnd = new Corner(pen, radius);
			_bottomStart = new Corner(pen, radius);
			_bottomEnd = new Corner(pen, radius);
		}

		internal Corners(Corner topStart, Corner topEnd, Corner bottomStart, Corner bottomEnd)
		{
			_topStart = topStart;
			_topEnd = topEnd;
			_bottomStart = bottomStart;
			_bottomEnd = bottomEnd;
		}

		public Corner All
		{
			set
			{
				if (value != null)
				{
					_topStart = value;
					_topEnd = value;
					_bottomStart = value;
					_bottomEnd = value;
				}
				else
				{
					_topStart = Corner.Default;
					_topEnd = Corner.Default;
					_bottomStart = Corner.Default;
					_bottomEnd = Corner.Default;
				}
			}
		}

		public Corner TopStart
		{
			get
			{
				if (_topStart == Corner.Default)
					_topStart = new Corner();

				return _topStart;
			}
			set
			{
				if (value != null)
					_topStart = value;
				else
					_topStart = Corner.Default;
			}
		}

		public Corner TopEnd
		{
			get
			{
				if (_topEnd == Corner.Default)
					_topEnd = new Corner();

				return _topEnd;
			}
			set
			{
				if (value != null)
					_topEnd = value;
				else
					_topEnd = Corner.Default;
			}
		}

		public Corner BottomStart
		{
			get
			{
				if (_bottomStart == Corner.Default)
					_bottomStart = new Corner();

				return _bottomStart;
			}
			set
			{
				if (value != null)
					_bottomStart = value;
				else
					_bottomStart = Corner.Default;
			}
		}

		public Corner BottomEnd
		{
			get
			{
				if (_bottomEnd == Corner.Default)
					_bottomEnd = new Corner();

				return _bottomEnd;
			}
			set
			{
				if (value != null)
					_bottomEnd = value;
				else
					_bottomEnd = Corner.Default;
			}
		}

		internal Corner GetTopStart()
		{
			return _topStart;
		}

		internal Corner GetTopEnd()
		{
			return _topEnd;
		}

		internal Corner GetBottomStart()
		{
			return _bottomStart;
		}

		internal Corner GetBottomEnd()
		{
			return _bottomEnd;
		}

		public static Corners Parse(string s)
		{
			if (string.IsNullOrEmpty(s))
				throw new ArgumentException(nameof(s));

			string[] vals = Utils.Split(s, ';');

			Pen pen = Pen.Parse(vals[0]);

			if (vals.Length == 1)
				return new Corners(pen, 0F);

			float radius = Converter.ToPoints(vals[1]);

			return new Corners(pen, radius);
		}

		public Corners Clone()
		{
			Corner topStart = _topStart;
			
			if (topStart != Corner.Default)
				topStart = topStart.Clone();

			Corner topEnd = _topEnd;
			
			if (topEnd != Corner.Default)
				topEnd = topEnd.Clone();

			Corner bottomStart = _bottomStart;
			
			if (bottomStart != Corner.Default)
				bottomStart = bottomStart.Clone();

			Corner bottomEnd = _bottomEnd;
			
			if (bottomEnd != Corner.Default)
				bottomEnd = bottomEnd.Clone();

			return new Corners(topStart, topEnd, bottomStart, bottomEnd);
		}
	}
}