using UnityEngine;
using TMPro;

namespace Marro.PacManUdon
{
    public class StatusDisplay : SyncedObject
    {
        [SerializeField] private Transform scoreDisplayGroup;
        [SerializeField] private TMP_Text label1UPText;
        [SerializeField] private TMP_Text score1UPText;
        [SerializeField] private TMP_Text labelHighScoreText;
        [SerializeField] private TMP_Text scoreHighScoreText;
        [SerializeField] private TMP_Text debugText;
        [SerializeField] private TMP_Text debugText2;
        [SerializeField] private TMP_Text gameOverText;
        [SerializeField] private TMP_Text player1Text;
        [SerializeField] private TMP_Text readyText;
        [SerializeField] private Transform levelDisplayDigitsContainer;
        [SerializeField] private Transform extraLifeIndicatorsContainer;

        private Animator[] levelDisplayDigitAnimators;
        private Renderer[] levelDisplayDigitRenderers;
        private GameObject[] extraLifeIndicators;


        private bool label1UPVisible;
        private bool label1UPTextBlinking;
        private const float labelBlinkToggleInterval = 0.26666667f;
        private float labelBlinkTimer;

        public void Initialize()
        {
            // Level Display
            levelDisplayDigitAnimators = levelDisplayDigitsContainer.GetComponentsInChildren<Animator>();
            levelDisplayDigitRenderers = levelDisplayDigitsContainer.GetComponentsInChildren<Renderer>();

            // Extra Lifes Display
            Transform[] extraLifeIndicatorTransforms = extraLifeIndicatorsContainer.GetComponentsInChildren<Transform>();
            extraLifeIndicators = new GameObject[extraLifeIndicatorTransforms.Length - 1];
            for (int i = 1; i < extraLifeIndicatorTransforms.Length; i++) // We skip the first found components as it is the container and not an actual indicator
            {
                extraLifeIndicators[i - 1] = extraLifeIndicatorTransforms[i].gameObject;
            }
            // Debug.Log($"{gameObject} extraLifeIndicators.Length: {extraLifeIndicators.Length}");
        }


        public void Reset()
        {
            SetLabel1UPTextBlinking(false);
        }

        public override void SyncedUpdate()
        {
            if (label1UPTextBlinking)
            {
                labelBlinkTimer += networkManager.SyncedDeltaTime;
                if (labelBlinkTimer > labelBlinkToggleInterval)
                {
                    labelBlinkTimer -= labelBlinkToggleInterval;
                    SetLabel1UPTextVisible(!label1UPVisible);
                }
            }
        }

        public void Set1UPScore(int score)
        {
            if (score == 0)
            {
                score1UPText.text = "00";
            }
            else
            {
                score1UPText.text = score.ToString();
            }
        }

        public void SetHighScore(int score)
        {
            if (score == 0)
            {
                scoreHighScoreText.text = "";
            }
            else
            {
                scoreHighScoreText.text = score.ToString();
            }
        }

        public void SetDebugText(int debugTextLine, string text)
        {
            if (debugTextLine == 1)
            {
                debugText.text = text;
            }
            else if (debugTextLine == 2)
            {
                debugText2.text = text;
            }
        }

        public void SetScoreDisplayVisible(bool visible)
        {
            scoreDisplayGroup.gameObject.SetActive(visible);
        }

        public void SetExtraLivesDisplayVisible(bool visible)
        {
            extraLifeIndicatorsContainer.gameObject.SetActive(visible);
        }

        public void SetLevelDisplayVisible(bool visible)
        {
            foreach (Renderer renderer in levelDisplayDigitRenderers)
            {
                renderer.enabled = visible;
            }
        }

        public void SetGameOverTextVisible(bool visible)
        {
            gameOverText.gameObject.SetActive(visible);
        }

        public void SetReadyTextVisible(bool visible)
        {
            readyText.gameObject.SetActive(visible);
        }

        public void SetPlayer1TextVisible(bool visible)
        {
            player1Text.gameObject.SetActive(visible);
        }

        void SetLabel1UPTextVisible(bool visible)
        {
            label1UPVisible = visible;
            label1UPText.gameObject.SetActive(visible);
        }

        public void SetLabel1UPTextBlinking(bool blinking)
        {
            SetLabel1UPTextVisible(false);
            labelBlinkTimer = 0;
            label1UPTextBlinking = blinking;
            if (blinking == false)
            {
                SetLabel1UPTextVisible(true);
            }
        }

        public bool SetExtraLives(int extraLives)
        {
            // Debug.Log($"{gameObject} SetExtraLives: {extraLives}");
            for (int extraLifeIndicatorIndex = 0; extraLifeIndicatorIndex < extraLifeIndicators.Length; extraLifeIndicatorIndex++)
            {
                extraLifeIndicators[extraLifeIndicatorIndex].SetActive(extraLifeIndicatorIndex < extraLives);
            }
            return true;
        }

        public void SetLevel(int level)
        {
            for (int i = 0; i < levelDisplayDigitAnimators.Length; i++)
            {
                levelDisplayDigitAnimators[i].SetFloat("FruitType", PacManConstants.FruitTypeToValue(PacManConstants.GetFruitTypeForLevel(level - i)));
            }
        }
    }
}