using UnityEngine;

public class Talkable : Interactable
{
    // this is a character or object that will talk upon interaction
    public Dialogue defaultDialogue;

    // this component is optional, and also must have a non-None voice selected to work
    public DialogueVoice voice;
    public GameObject talkIndicator;

    public Quest[] quests;
    Dialogue currentDialogue;

    public void Start()
    {
        voice = GetComponent<DialogueVoice>();

        if (talkIndicator != null)
        {
            talkIndicator.SetActive(false);
        }
    }

    void DetermineDialogue()
    {
        // based on if we have any open quests and where we're at in the quest, 
        // determine what dialogue to show
        Quest currentQuest = null;

        // Grab the first ACTIVE quest
        // If none active, grab the first AVAILABLE quest
        // If none available, revert to default dialogue
        if (quests.Length > 0)
        {
            // check for ACTIVE quests
            foreach (Quest q in quests)
            {
                if (q.hasStarted && !q.hasCompleted)
                {
                    currentQuest = q;
                }
            }
            // check for AVAILABLE quests
            if (currentQuest == null)
            {
                foreach (Quest q in quests)
                {
                    if (!q.hasStarted && !q.hasCompleted)
                    {
                        currentQuest = q;
                    }
                }
            }
        }

        // revert to default if no suitable quest found
        if (currentQuest == null)
        {
            currentDialogue = defaultDialogue;
        }
        else
        {
            if (currentQuest.hasStarted)
                currentQuest.CheckComplete(); // don't check for completion before asking player for help

            currentDialogue = new Dialogue();
            currentDialogue.charName = defaultDialogue.charName;

            // set up dialogue based on quest
            if (currentQuest.hasStarted && !currentQuest.hasCompleted)
            {
                currentDialogue.sentences = currentQuest.duringText;
            }
            else if (!currentQuest.hasStarted && !currentQuest.hasCompleted)
            {
                currentDialogue.sentences = currentQuest.askText;
                currentQuest.StartQuest();
            }
            else
            {
                currentDialogue.sentences = currentQuest.completionText;
            }
        }
    }

    public override void Interact()
    {
        if (currentDialogue == null || !currentDialogue.isActive)
        {
            DetermineDialogue();
            GameManager.Instance.DialogueManager.StartDialogue(currentDialogue, voice);
        }
        else
            GameManager.Instance.DialogueManager.DisplayNextSentence();

        if (talkIndicator != null)
        {
            talkIndicator.SetActive(false);
        }
    }

    public override void MoveInsideRange()
    {
        if (talkIndicator != null)
        {
            talkIndicator.SetActive(true);
        }
    }

    public override void MoveOutsideRange()
    {
        GameManager.Instance.DialogueManager.EndDialogue();

        if (talkIndicator != null)
        {
            talkIndicator.SetActive(false);
        }
    }
}
