using System;
using Intrepid.Core.Camera;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Core.UI
{
    [DefaultExecutionOrder(GonConstants.ExecutionOrder.ALAP)]
    public class WorldOverlayMarker : MonoBehaviour
    {
        [GroupConfigurations, SerializeField] private Vector3 screenOffset = Vector3.zero;
        [GroupInjections, SerializeField] private Transform target;
        [GroupInjections, SerializeField] private RectTransform rectTransform;

        private void LateUpdate()
        {
            if (CameraSystem.Instance.IsNull() || target.IsNull())
            {
                gameObject.SetActive(false);
                return;
            }

            var cam = CameraSystem.Instance.MainCamera;
            var screenPosition = cam.WorldToScreenPoint(target.position) + screenOffset;
            
            rectTransform.position = screenPosition;
        }
    }
}