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

public class DialogCaller
{
    public static DialogCaller Instance { get; private set; }
    static DialogCaller() { Instance = new DialogCaller(); }

    private GameObject mCurrentlyOpenedLayer = null;
    private System.Type mCurrentDialogType;
    private Enums.eDialogModes mCurrentDialogMode;
    private List<KeyValuePair<System.Type, Enums.eDialogModes>> mQueue = new List<KeyValuePair<System.Type, Enums.eDialogModes>>();

    public event EventHandler<DialogDimissedEventArgs> DialogDismissed;
    public class DialogDimissedEventArgs : EventArgs
    {
        public GameObject DialogLayerObj { get; private set; }
        public System.Type CurrentDialogType { get; private set; }
        public Enums.eDialogModes CurrentDialogMode { get; private set; }
        public DialogDimissedEventArgs(GameObject dialogLayerObj, System.Type dialogType, Enums.eDialogModes dialogMode) 
        { 
            DialogLayerObj = dialogLayerObj;
            CurrentDialogType = dialogType;
            CurrentDialogMode = dialogMode;
        }
    }

    public void Call(System.Type dialogType, Enums.eDialogModes dialogMode)
    {
        if (mCurrentlyOpenedLayer != null)
        {
            bool dialogAlreadyQueued = false;
            if (mCurrentDialogType == dialogType)
            {
                dialogAlreadyQueued = true;
            }
            foreach (KeyValuePair<System.Type, Enums.eDialogModes> kvp in mQueue)
            {
                if (kvp.Key == dialogType)
                {
                    dialogAlreadyQueued = true;
                    break;
                }
            }
            if (!dialogAlreadyQueued)
            {
                mQueue.Add(new KeyValuePair<System.Type, Enums.eDialogModes>(dialogType, dialogMode));
            }
        }
        else
        {
            mCurrentlyOpenedLayer = LayerManager.Instance.AddLayerOnTop(LayerManager.Layers.DialogScreen, dialogMode != Enums.eDialogModes.Timed, new DialogDataPack(dialogType, dialogMode), false);
            mCurrentDialogType = dialogType;
            mCurrentDialogMode = dialogMode;
        }
    }

    public void Dismiss(bool clearQueue)
    {
        if (mCurrentlyOpenedLayer != null)
        {
            if (DialogDismissed != null)
            {
                DialogDismissed(this, new DialogDimissedEventArgs(mCurrentlyOpenedLayer, mCurrentDialogType, mCurrentDialogMode));
            }
            LayerManager.Instance.RemoveLayer(mCurrentlyOpenedLayer, false, true);
            mCurrentlyOpenedLayer = null;
            if (clearQueue)
            {
                mQueue.Clear();
            }
            else if(mQueue.Count > 0)
            {
                KeyValuePair<System.Type, Enums.eDialogModes> nextDialog = mQueue[0];
                mQueue.RemoveAt(0);
                Call(nextDialog.Key, nextDialog.Value);
            }
        }
    }

    public bool GetDialogOpened()
    {
        return mCurrentlyOpenedLayer != null;
    }
}