using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Dialogue : MonoBehaviour
{
    public GameObject dialoguePanel;
    public TextMeshProUGUI dialogueText;
    public string[] dialogue;
    private int index;

    // untuk inisiasi kamera
    public GameObject camera;

    public string sceneName;

    public GameObject nextLine;
    public float wordSpeed;

    public Animator animator;

    private bool isTyping = false;

    void Start()
    {
        dialogueText.text = "";
        StartDialogue();
    }

    public void StartDialogue()
    {
        index = 0;
        dialogueText.text = "";
        StartCoroutine(Typing());
    }    

    public void NextLine()
    {
        if (isTyping) return;

        nextLine.SetActive(false);

        StartCoroutine(MoveCamera(new Vector3(camera.transform.position.x + 20.0f, camera.transform.position.y, camera.transform.position.z), 0.0f));

        if (index < dialogue.Length - 1)
        {        
            index++;
            StartCoroutine(Typing());            
        }
        else
        {
            StartCoroutine(TransitionStart(sceneName));
        }

        //if (index == index)
        //{
        //    // ketika index = 1 kamera bergerak ke arah kanan kekoordinat x yang sudah di tentukan, misal 20.0 untuk melihat ke area lain
        //    StartCoroutine(MoveCamera(new Vector3(20.0f, camera.transform.position.y, camera.transform.position.z), 0.01f));
        //}
    }

    public void PreviousLine()
    {
        if (index != 0)
        {
            if (isTyping) return;

            StartCoroutine(MoveCamera(new Vector3(camera.transform.position.x - 20.0f, camera.transform.position.y, camera.transform.position.z), 0.0f));

            if (index > 0)
            {
                index--;
                dialogueText.text = "";
                StartCoroutine(Typing());
            }
        }
    }

    public void ZeroText()
    {
        dialogueText.text = "";
        index = 0;
    }

    IEnumerator Typing()
    {
        isTyping = true;
        dialogueText.text = "";

        foreach (char letter in dialogue[index].ToCharArray())
        {
            dialogueText.text += letter;
            yield return new WaitForSeconds(wordSpeed);
        }

        isTyping = false;
        nextLine.SetActive(true);
    }

    IEnumerator MoveCamera(Vector3 targetPosition, float duration)
    {
        Vector3 startPosition = camera.transform.position;
        float time = 0;

        while (time < duration)
        {
            camera.transform.position = Vector3.Lerp(startPosition, targetPosition, time / duration);
            time += Time.deltaTime;
            yield return null;
        }

        camera.transform.position = targetPosition; // Pastikan posisi akhir tepat
    }

    IEnumerator TransitionStart(string sceneName)
    {
        animator.SetTrigger("end");
        yield return new WaitForSeconds(1);
        SceneManager.LoadScene(sceneName);
    }
}
