using System;
using TMPro;
using UnityEngine;
using Yarn.Markup;
using Yarn.Unity;
using UnityEngine.SceneManagement;
namespace DialogueSystem
{
    public class OutroDialogue : MonoBehaviour
    {
        public DialogueRunner dialogueRunner;
        public GameObject linePresenter;
        
        public static OutroDialogue Instance;

        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
                return;
            }
            Instance = this;
            //StartOutroDialogue();
            dialogueRunner.Stop();
            linePresenter.SetActive(false);

            dialogueRunner.AddCommandHandler<string>("choiceMade", OnChoiceMade);
        }
        
        private void OnChoiceMade(string choice)
        {
            string playerChoice = choice;
            Debug.Log($"Player chose: {playerChoice}");
        
            // Now you can use playerChoice to trigger different game logic
            if (playerChoice == "let_her_go")
            {
                HandleLetHerGo();
            }
            else if (playerChoice == "keep_trying")
            {
                HandleKeepTrying();
            }
        }

        private void HandleLetHerGo()
        {
            // TODO: add animation of grandma going up
            Debug.Log("Handle: LetHerGo");
            SceneManager.LoadScene("OutroPresentScene");
        }

        private void HandleKeepTrying()
        {
            // TODO: start level 4
            Debug.Log("Handle: Keep Trying");
            SceneManager.LoadScene("Level4");
        }
        
        public void StartOutroDialogue()
        {
            linePresenter.SetActive(true);
            dialogueRunner.StartDialogue("Start");
        }
        
        public void OnContinueButtonPressed()
        {
            dialogueRunner.RequestHurryUpLine();
        }
    }
}