using UnityEngine;

namespace InterfaceOff
{
    public class TrollWindow : WindowBase
    {
        private int m_Health = int.MaxValue;
        private bool m_SpawnOnRight;
        
        public override void OnWindowInstantiation()
        {
            /* Adds a random advert to the display */
            Advert advert = AdvertRegistry.GetRandomAdvert();
            Components.WindowImage.sprite = advert.Image;
            Components.InfoText.text = advert.Name;
            Components.WindowImage.color = new Color(1, 1, 1, 1);
            
            /* Calculates a random health value */
            m_Health = Random.Range(2, 6);
        }

        public override void OnWindowCloseButtonClicked()
        {
            /* Removes info text as it can overlap with the close button */
            Components.InfoText.text = string.Empty;
            
            /* Decreases health and destroys if at 0 */
            m_Health--;
            if (m_Health <= 0)
            {
                DestroyWindow();
                return;
            }
            
            /* If not at zero randomly moves the close button */
            float randX = m_SpawnOnRight ? Random.Range(20, 80) : Random.Range(-80, -20);
            m_SpawnOnRight = !m_SpawnOnRight; // Makes sure the close button is semi far away

            Components.CloseButtonRectTransform.anchoredPosition = new Vector2(randX, 0);
        }
    }
}