using UnityEngine;
using UnityEngine.UI;

public class MinimapMarker : MonoBehaviour
{
    [SerializeField] private Sprite _iconSprite;
    [SerializeField] private Color _iconColor = Color.red;
    [SerializeField] private Vector2 _iconSize = new Vector2(10f, 10f);
    [SerializeField] private bool _rotateWithTarget = false;

    private RectTransform _iconRect;
    private Transform _target; // 추적할 월드 오브젝트

    private void Awake()
    {
        _target = transform;
    }

    public void Initialize(MinimapManager manager)
    {
        // 마커 아이콘 UI 생성
        GameObject iconObj = new GameObject($"Marker_{gameObject.name}");
        iconObj.transform.SetParent(manager.MarkerContainer, false);

        Image img = iconObj.AddComponent<Image>();
        img.sprite = _iconSprite;
        img.color = _iconColor;

        _iconRect = iconObj.GetComponent<RectTransform>();
        _iconRect.sizeDelta = _iconSize;

        manager.RegisterMarker(this);
    }

    public void UpdatePosition(MinimapManager manager)
    {
        if (_iconRect == null || _target == null) return;

        _iconRect.anchoredPosition = manager.WorldToMapPos(_target.position);

        if (_rotateWithTarget)
        {
            _iconRect.localRotation = Quaternion.Euler(0, 0, -_target.eulerAngles.y);
        }
    }

    private void OnDestroy()
    {
        if (_iconRect != null)
            Destroy(_iconRect.gameObject);
    }
}
