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

public class NPCDialogue : MonoBehaviour
{
    public string name;
    public Text nameText;
    public Text nameText1;
    public Text dialogueText;
    public Dialogue dialogue;
    public Dialogue dialogueWQuest;
    public Dialogue dialogueAQuest;

    private Queue<string> sentences;
    private Queue<string> sentencesWQuest;
    private Queue<string> sentencesAQuest;
    public GameObject Panel;

    public GameObject QuestPanel;

    bool triggered = false;

    public int isQuest;

    void Start()
    {
        sentences = new Queue<string>();
        sentencesWQuest = new Queue<string>();
        sentencesAQuest = new Queue<string>();


    }

    public void TriggerDialogue()
    {
        if (isQuest == 2)
        {
            StartDialogue(dialogueWQuest);
        }
        else if (isQuest == 3)
        {
            StartDialogue(dialogueAQuest);
        }
        else
        {
            StartDialogue(dialogue);
        }
    }

    void OnTriggerExit2D(Collider2D collision)
    {
        if (Panel != null)
        {

            Panel.SetActive(false);
        }
        triggered = false;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            triggered = true;
        }
    }

    void Update()
    {
 
        if (triggered)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                if (Panel != null)
                {
                    Panel.SetActive(true);
                }
                if (isQuest == 2)
                {
                    StartDialogue(dialogueWQuest);
                }
                else if (isQuest == 2)
                {
                    StartDialogue(dialogueAQuest);
                }
                else
                {
                    StartDialogue(dialogue);
                }
            }
        }
    }


    public void StartDialogue(Dialogue dialogue)
    { 
        nameText.text = dialogue.name;
        nameText1.text = dialogue.name;

        sentences.Clear();

        foreach(string sentence in dialogue.sentences)
        {
            sentences.Enqueue(sentence);
        }


        DisplayNextSentence();
    }

    public void DisplayNextSentence()
    {
        if (isQuest == 2)
        {
            if (sentences.Count == 0)
            {
                EndDialogue();
                return;
            }
            string sentence = sentences.Dequeue();
            dialogueText.text = sentence;
        }
        else if (isQuest == 0)
        {
            if (sentences.Count == 0)
            {
                EndDialogue();
                return;
            }
            string sentence = sentences.Dequeue();
            dialogueText.text = sentence;
        }
        else if (isQuest == 4)
        {
            if (sentences.Count == 0)
            {
                EndDialogue();
                return;
            }
            string sentence = sentences.Dequeue();
            dialogueText.text = sentence;
        }
        else if (isQuest == 1)
        {
            if (sentences.Count == 0)
            {
                QuestPanel.SetActive(true);
                Panel.SetActive(false);
                return;
            }
            string sentence = sentences.Dequeue();
            dialogueText.text = sentence;
        }

    }

    public void EndDialogue()
    {
        Panel.SetActive(false);
        QuestPanel.SetActive(false);

    }

    public void QuestAccepted()
    {
        isQuest = 2;
        QuestPanel.SetActive(false);
        if(!gameObject.GetComponent<NPCQuest>().IsTaskAccepted())
        {
            // 1. Add task to palyer quests list
            Task myTask = gameObject.GetComponent<NPCQuest>().AcceptTask();
        
            TaskUIManager.Instance.Add(myTask);

            // 2. Drop Axe On Map
            gameObject.GetComponent<NPCQuest>().DropItem();
        } else {
            Debug.Log("Ten task został juz rozpoczęty!");
        }
    }

}