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

// change to "mission step" model in future
[Serializable]
public class DialogueStepModel
{
    [SerializeField]
    public string Header;

    [SerializeField]        // this flag tell whatewer dialoge ware already display to user
    public bool WasDisplayed = false;

    [SerializeField]        // list to map to queue in DialogueController
    public List<DialogueModel> ListOfSentences = new List<DialogueModel>();

    [SerializeField]
    public UnityEvent EndOfDialogueStepAction = new UnityEvent();



    // for displaying set of sentences in one display
    public DialogueController DialogueController { get; protected set; }


    public DialogueStepModel()
    {
        // Add marked step as displayed action as first!
        EndOfDialogueStepAction.AddListener(() => { MarkAsDisplayed(); });
    }

    public DialogueStepModel(DialogueController _dialogueController)
    {
        // Add marked step as displayed action as first!
        EndOfDialogueStepAction.AddListener(() => { MarkAsDisplayed(); });

        DialogueController = _dialogueController;
    }

    /// <summary>
    /// Just convert list to queue structure :D
    /// </summary>
    public virtual void Build()
    {
        // 1. Clear previous setups 

        // 2. Map first structur into second
        DialogueController = new DialogueController();

        foreach (DialogueModel DialogueModel in ListOfSentences)
        {
            // Pass data to builded panel (name, sentence, buttons)
            DialogueModel.Header = Header;  // set header
            //Debug.Log($"Add sentence - {DialogueModel.Sentence}");
            DialogueController.AddSentence(DialogueModel);
        }

        // 3 Bind ending actions
        DialogueController.SetActionAfterDialogueEnds(EndOfDialogueStepAction);
    }

    public void SetActionAfterDialogueStep(UnityEvent _endOfDialogueStepAction)
    {
        EndOfDialogueStepAction = _endOfDialogueStepAction;
    }

    public void SetActionAfterDialogueStep(Action _finishDialogueAction)
    {
        EndOfDialogueStepAction.AddListener(new UnityAction(_finishDialogueAction));
    }

    // new function - now we should change all places closing open panels toi use its not manually close
    public void ClosePanel()
    {
        if (DialogueController != null && DialogueController.CurrentPanel != null)
            DialogueController.CloseCurrentPanel();
    }

    public void MarkAsDisplayed()
    {
        WasDisplayed = true;
    }
}