using UnityEngine;

namespace Assets.Source.Content.UI.Components
{
    public class ResolutionAdaptScaler : MonoBehaviour
    {
        public bool Active = true;
        public float ReferenceAspectFrom = 0.75f;
        public float ExtraSizeFrom = 0f;
        public float ReferenceAspectTo = 0.46182f;
        public float ExtraSizeTo = 0f;

        private float _prevAspect;

        void Awake()
        {
            Refresh();
        }

#if UNITY_EDITOR
        void Update()
        {
            Refresh();
        }
#endif

        private void Refresh()
        {
            if (!Active)
                return;
            var currentAspect = (float)Screen.width / Screen.height;
            if (currentAspect == _prevAspect)
                return;
            var set = Get.CoreSettings.UISettings.ResolutionAdaptation;
            var percent = Mathf.InverseLerp(ReferenceAspectFrom, ReferenceAspectTo, currentAspect);
            var extraSize = Mathf.Lerp(ExtraSizeFrom, ExtraSizeTo, percent) * set.SizeDependenceCurve.Evaluate(percent);
            transform.localScale = Vector3.one + Vector3.one * extraSize;

            _prevAspect = currentAspect;
        }
    }
}
