using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueEntity : BoardEntity
{
    public List<DialogueTree> dialogueTrees = new List<DialogueTree>();
    public List<DialogueTree> queuedTrees = new List<DialogueTree>();
    private int discussionCount = 0;
    private List<string> deliveredDialogues;
    private string linger = "...";
    private bool hasDialogue = false;

    private SpriteRenderer m_Notification;

    protected override void OnAwake()
    {
        //dialogueTrees[0] = ScriptableObject.Instantiate<DialogueTree>(queuedTrees[0]);
        m_Notification = transform.GetChild(0).GetComponent<SpriteRenderer>();
        UpdateDialogue();
    }

    private void EvaluateTrees()
    {
        for(int i=0; i < queuedTrees.Count;)
        {
            if (Evaluate(queuedTrees[i]))
            {
                dialogueTrees.Add(ScriptableObject.Instantiate<DialogueTree>( queuedTrees[i] ));
                queuedTrees.RemoveAt(i);
            }
            else
            {
                i++;
            }
        }
    }

    private bool Evaluate(DialogueTree t)
    {
        if (t.m_Conditions.Count < 1)
            return true;
        bool good = true;
        for (int i = 0; i < t.m_Conditions.Count; i++)
        {
            switch (t.m_Conditions[i].m_Condition)
            {
                case ConditionType.Death:
                    good &= PlayerPrefs.GetInt("PP_DeathCount", 0) >= t.m_Conditions[i].m_ConditionValue;
                    break;
            }
        }
        return good;
    }

    public DialogueObject GetDialogue()
    {

        if (queuedTrees.Count > 0)
        {
            EvaluateTrees();
        }
        if (dialogueTrees.Count > 0)
        {
            int i = discussionCount % dialogueTrees[0].dialogues.Count;
            DialogueObject ret = dialogueTrees[0].dialogues[i];
            if (ret.once)
            {
                dialogueTrees[0].dialogues.RemoveAt(i);
                if (dialogueTrees[0].dialogues.Count < 1)
                {
                    if (dialogueTrees[0].m_Next != null)
                    {
                        queuedTrees.Add(ScriptableObject.Instantiate<DialogueTree>(dialogueTrees[0].m_Next));
                    }
                    if (dialogueTrees[0].lingerMessage.Length > 0)
                        linger = dialogueTrees[0].lingerMessage;
                    else
                        linger = "...";
                    dialogueTrees.RemoveAt(0);
                    discussionCount = 0;
                }
            }
            else
            {
                discussionCount++;
            }
            UpdateDialogue();
            return ret;
        }
        else
        {
            UpdateDialogue();
            return null;
        }
        //return new string[] { "Hey!", "What's up?", "...", "Whaddya want?\n A tutorial?      \nIn THIS economy?" };
    }

    public void UpdateDialogue()
    {        
        if (queuedTrees.Count > 0)
        {
            EvaluateTrees();
        }

        hasDialogue = dialogueTrees.Count > 0;

        m_Notification.gameObject.SetActive(hasDialogue);
    }
}
