using Mox.Shared.Renderer;

namespace Mox.Shared.UI
{
    public struct SCheckBoxEventData
	{
		public SCheckBoxEventData(bool _bChecked, int _iX, int _iY, int _iIndex = -1, string _sTag = null)
		{
			Checked = _bChecked;
			X = _iX;
			Y = _iY;
			Index = _iIndex;
			Tag = _sTag;
		}

		public bool Checked;
		public int X;
		public int Y;
		public int Index;
		public string Tag;
	}

	public class CheckBox : Button
	{
		public override void Render()
		{
			if (m_bChecked)
			{
				if ((States & ControlStates.Disabled) != 0)
				{
					RenderImage(BackgroundCheckedDisabled ?? BackgroundChecked ?? Background);
				}
				else if ((States & ControlStates.Pressed) != 0)
				{
					RenderImage(BackgroundCheckedPressed ?? BackgroundChecked ?? Background);
				}
				else if ((States & ControlStates.Highlighted) != 0)
				{
					RenderImage(BackgroundCheckedHover ?? BackgroundChecked ?? Background);
				}
				else
				{
					RenderImage(BackgroundChecked ?? Background);
				}
			}
			else
			{
				base.Render();
			}
		}
		
		protected override void OnClick(SMouseEventData _mouseEvent)
		{
			m_bChecked = !m_bChecked;
			Root.Instance.FireEvent(this, EEventType.CheckBoxCheckChanged, EEventPropagation.Bubble, new SCheckBoxEventData(m_bChecked, _mouseEvent.X, _mouseEvent.Y, _mouseEvent.Index, Tag));
		}

		protected override void OnDisabledClick(SMouseEventData _mouseEvent)
		{
		}

		public override void Serialize(Serializer.Serializer _serializer)
		{
			_serializer.Serialize(ref m_BackgroundChecked, nameof(BackgroundChecked));
			_serializer.Serialize(ref m_BackgroundCheckedHover, nameof(BackgroundCheckedHover));
			_serializer.Serialize(ref m_BackgroundCheckedPressed, nameof(BackgroundCheckedPressed));
			_serializer.Serialize(ref m_BackgroundCheckedDisabled, nameof(BackgroundCheckedDisabled));
			_serializer.Serialize(ref m_bChecked, nameof(Checked), false);

			base.Serialize(_serializer);
		}
		
		public SubImage BackgroundChecked { get => m_BackgroundChecked; set => m_BackgroundChecked = value; }
		public SubImage BackgroundCheckedHover { get => m_BackgroundCheckedHover; set => m_BackgroundCheckedHover = value; }
		public SubImage BackgroundCheckedPressed { get => m_BackgroundCheckedPressed; set => m_BackgroundCheckedPressed = value; }
		public SubImage BackgroundCheckedDisabled { get => m_BackgroundCheckedDisabled; set => m_BackgroundCheckedDisabled = value; }
		public bool Checked { get => m_bChecked; set => m_bChecked = value; }

		private SubImage m_BackgroundChecked;
		private SubImage m_BackgroundCheckedHover;
		private SubImage m_BackgroundCheckedPressed;
		private SubImage m_BackgroundCheckedDisabled;
		private bool m_bChecked;
	}
}
