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

namespace SimplifyXR
{
    public enum NoteType
    {
        Note,
        Warning, 
        Caution
    }
    
    [System.Serializable]
    public class NoteMessage
    {
        public string noteID;
        public NoteType noteType;
        public bool requiresAcknowledgement;
        public Sprite icon;
        public string message;
    }

    public class InstructionNote : MonoBehaviour
    {

        public Color noteColor;
        public Color warningColor;
        public Color cautionColor;

        public UnityEvent<bool> OnNoteDisplayed;
        public UnityEvent<string> OnNoteAcknowledged;

        private InstructionElementRoot root;

        public void OnEnable()
        {
            root = GetComponentInParent<InstructionElementRoot>();
        }

        public void OnDisable()
        {
            root.acknowledgeButton.onClick.RemoveAllListeners();
        }

        public Color UpdateNoteColor(NoteType noteType)
        {
            switch (noteType)
            {
                case NoteType.Note:
                    return noteColor;
                case NoteType.Caution:
                    return cautionColor;
                case NoteType.Warning:
                    return warningColor;
                 default:
                    return noteColor;
            }

        }

        public void UpdateNote(NoteMessage message)
        {            
            root.noteIcon.sprite = message.icon == null ? root.noteIcon.sprite : message.icon;
            root.noteText.text = message.message;
            root.notePanel.color = UpdateNoteColor(message.noteType);
            root.acknowledgeButton.gameObject.SetActive(message.requiresAcknowledgement);
            root.acknowledgeButton.onClick.AddListener(() => AcknowledgeNote(message.noteID));
            OnNoteDisplayed?.Invoke(message.requiresAcknowledgement);
        }

        public void AcknowledgeNote(string id)
        {
            root.acknowledgeButton.gameObject.SetActive(false);
            OnNoteAcknowledged?.Invoke(id);
        }

    }
}

