using System.Collections;
using Intrepid.ObjectPooling;
using UnityEngine;
using UnityEngine.UI;

namespace Intrepid.Gameplay.Entities.Player.PDA.UI
{
    [RequireComponent(typeof(RectTransform))]
    public class PDAContentReferenceUI : PoolableGameObject
    {
        [SerializeField] private Image background;
        [SerializeField] private LayoutElement layoutElement;
        [SerializeField] private LinearAnimation linearAnimation;
        [SerializeField] private PDAContentTag contentTag;
        [SerializeField] private string title;
        [SerializeField] private string description;
        
        public bool AnimationRunning { get; private set; }
        private RectTransform RectTransform { get; set; }
        public string Title => title;
        public string Description => description;
        public PDAContentTag ContentTag => contentTag;

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

        public void Setup(PDAContentReferenceDTO contentReference)
        {
            contentTag = contentReference.ContentTag;
            layoutElement.minWidth = contentReference.Width;
            title = contentReference.Title;
            description = contentReference.Description;
            background.color = contentReference.Color;
        }

        private IEnumerator FocusIn()
        {
            return linearAnimation.Animate(1, 1.5f,
                di => di,
                v => RectTransform.localScale = new Vector3(v, v, v),
                r => AnimationRunning = r);

        }
        
        private IEnumerator FocusOut()
        {
            return linearAnimation.Animate(1.5f, 1,
                di => -di,
                v => RectTransform.localScale = new Vector3(v, v, v),
                r => AnimationRunning = r);

        }
        
        public void Focus()
        {
            StartCoroutine(FocusIn());
        }

        public void UnFocus()
        {
            StartCoroutine(FocusOut());
        }

        public void ResetFocus()
        {
            RectTransform.localScale = Vector3.one;
        }
    }

    public class PDAContentReferenceDTO
    {
        public float Width { get; set; }
        public PDAContentTag ContentTag { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        
        public Color Color { get; set; }
    }
}