using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Yarn.Markup;
using Yarn.Unity;
using Yarn.Unity.Attributes;

namespace DialogueSystem
{
    
    /// <summary>
    /// Copying the source code from LinePresenterButtonHandler, as their script is skipping lines when pressing continue and text is unfinished. 
    /// </summary>
    
    public class ContinueButtonHandler : ActionMarkupHandler
    {
       [MustNotBeNull, SerializeField] Button? continueButton;

        [MustNotBeNullWhen(nameof(continueButton), "A " + nameof(DialogueRunner) + " must be provided for the continue button to work.")]
        [SerializeField] DialogueRunner? dialogueRunner;
        
        private bool isTextFullyDisplayed = false;

        void Awake()
        {
            if (continueButton == null)
            {
                Debug.LogWarning($"The {nameof(continueButton)} is null, is it not connected in the inspector?", this);
                return;
            }
            continueButton.interactable = false;
            continueButton.enabled = false;
        }

        public override void OnPrepareForLine(MarkupParseResult line, TMP_Text text)
        {
            if (continueButton == null)
            {
                Debug.LogWarning($"The {nameof(continueButton)} is null, is it not connected in the inspector?", this);
                return;
            }
            
            isTextFullyDisplayed = false;
            
            // enable the button
            continueButton.interactable = true;
            continueButton.enabled = true;

            continueButton.onClick.AddListener(() =>
            {
                if (dialogueRunner == null)
                {
                    Debug.LogWarning($"Continue button was clicked, but {nameof(dialogueRunner)} is null!", this);
                    return;
                }

                if (!isTextFullyDisplayed)
                {
                    // Text is still typing - finish it
                    dialogueRunner.RequestHurryUpLine();
                    isTextFullyDisplayed = true;
                }
                else
                {
                    // Text is already displayed - advance to next line
                    dialogueRunner.RequestNextLine();
                }
            });
        }

        public override void OnLineDisplayBegin(MarkupParseResult line, TMP_Text text)
        {
            return;
        }

        public override YarnTask OnCharacterWillAppear(int currentCharacterIndex, MarkupParseResult line, CancellationToken cancellationToken)
        {
            return YarnTask.CompletedTask;
        }

        public override void OnLineDisplayComplete()
        {
            isTextFullyDisplayed = true;
        }

        public override void OnLineWillDismiss()
        {
            if (continueButton == null)
            {
                return;
            }
            // disable interaction
            continueButton.onClick.RemoveAllListeners();
            continueButton.interactable = false;
            continueButton.enabled = false;
        }
    }
}
