using Cryville.EEW.Core.Map;
using System.Drawing;
using UnityEngine;

namespace Cryville.EEW.Unity.Map.Element {
	[RequireComponent(typeof(SpriteRenderer))]
	class HypocenterElement : MapElement {
		[SerializeField] Sprite m_sprite;
		[SerializeField] Sprite m_spriteLowQuality;

		PointF _tilePos;
		public override RectangleF? AABB => new(_tilePos, SizeF.Empty);

		public void SetLocation(PointF location, ref int order) {
			_tilePos = MapTileUtils.WorldToTilePos(location);
			Vector3 pos = _tilePos.ToVector2();
			pos.z = OrderToZ(ref order);
			transform.localPosition = pos;
		}

		bool m_isLowQuality;
		public bool IsLowQuality {
			get => m_isLowQuality;
			set {
				if (m_isLowQuality == value) return;
				m_isLowQuality = value;
				_spriteRenderer.sprite = value ? m_spriteLowQuality : m_sprite;
			}
		}

		float m_size;
		public float Size {
			get => m_size;
			set {
				if (m_size == value) return;
				m_size = value;
				OnSetScale();
			}
		}

		SpriteRenderer _spriteRenderer;
		void Awake() {
			_spriteRenderer = GetComponent<SpriteRenderer>();
		}

		protected override void OnSetScale() {
			transform.localScale = ComputedScale * m_size / 256 * Vector3.one;
		}
	}
}
