using UnityEngine;
using UnityEngine.EventSystems;

namespace Cryville.Crtr.Browsing {
	public class PVPBool : PropertyValuePanel, IPointerClickHandler {
		bool _value;
		public override object Value {
			get { return _value; }
			set { _value = (bool)value; }
		}

		[SerializeField]
		RectTransform m_on;
		[SerializeField]
		RectTransform m_handleArea;
		[SerializeField]
		RectTransform m_handle;

		public void Toggle() {
			_value = !_value;
			Callback(Value);
		}

		const float SPEED = 8;
		float _ratio;
#pragma warning disable IDE0051
		void Update() {
			if (_value && _ratio != 1) {
				_ratio += SPEED * Time.deltaTime;
				if (_ratio > 1) _ratio = 1;
				UpdateGraphics();
			}
			else if (!_value && _ratio != 0) {
				_ratio -= SPEED * Time.deltaTime;
				if (_ratio < 0) _ratio = 0;
				UpdateGraphics();
			}
		}

		void OnRectTransformDimensionsChange() {
			m_handleArea.sizeDelta = new Vector2(m_handle.rect.height - m_handle.rect.width, 0);
		}
#pragma warning restore IDE0051

		void UpdateGraphics() {
			m_on.anchorMax = new Vector2(_ratio, m_on.anchorMax.y);
			m_handle.anchorMin = new Vector2(_ratio, m_handle.anchorMin.y);
			m_handle.anchorMax = new Vector2(_ratio, m_handle.anchorMax.y);
		}

		public void OnPointerClick(PointerEventData eventData) {
			Toggle();
		}
	}
}
