using Intrepid.Core.Themes;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

namespace Intrepid.Core.UI.Gauge
{
    public class PowerDemandedUI : MonoBehaviour
    {
        [GroupInjections, SerializeField] private Image pen;
        [GroupInjections, SerializeField] private Image[] marks;
        [Title("Theme")]
        [GroupConfigurations, SerializeField, HideLabel, ReadOnly] private UIPowerDemandedTheme theme;
        [GroupDebug, SerializeField, Range(0, 1)] private float value;

        private RectTransform RectTransform { get; set; }
        
        public float Value
        {
            get => value;
            set => this.value = value;
        }
        public UIPowerDemandedTheme Theme
        {
            get => theme;
            set => theme = value;
        }

        private void OnValidate()
        {
            RectTransform = GetComponent<RectTransform>();
            UpdateComponents(RectTransform);
        }

        private void Awake()
        {
            OnValidate();
        }

        private void Update()
        {
            UpdateComponents(RectTransform);
        }

        private void UpdateComponents(RectTransform rectTransform)
        {
            var quarter = rectTransform.rect.width / 4f;
            pen.rectTransform.localScale = new Vector3(Value, 1, 1);
            pen.color = Value switch
            {
                <= 0.25f => Theme.PenMin,
                > 0.5f => Theme.PenMax,
                _ => Color.Lerp(Theme.PenMin, Theme.PenMax, Value * 4f - 1f)
            };
            marks[1].rectTransform.localPosition = new Vector3(-quarter, 0, 0);
            marks[3].rectTransform.localPosition = new Vector3(quarter, 0, 0);
            foreach (var mark in marks)
            {
                mark.color = Theme.Marks;
            }
        }
    }
}