using DG.Tweening;
using DialogueSystem.Notes;
using DialogueSystem.Tutorials;
using Tutorials;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace DialogueSystem
{
    /// <summary>
    /// Manages the overall mechanic of the dialogue window
    /// </summary>
    public class DialogueManager : MonoBehaviour
    {
        [Header("Panels and Buttons")]
        public GameObject tutorialPanel;
        public Button exitButtonTutorial;
        
        public GameObject notePanel;

        public GameObject diaryPanel;
        public Button exitButtonDiary;
        
        [Header("UI Scripts")]
        public TutorialContentUI tutorialContentUI;
        public NoteContentUI noteContentUI;

        public BoolReactiveProperty tutorialIsOpen = new BoolReactiveProperty(false);

        public bool noteIsOpen = false;
        
        public static DialogueManager Instance { get; private set; }

        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(this);
                return;
            }
            tutorialIsOpen.Value = false;
            
            Instance = this;

            exitButtonTutorial.onClick.AddListener(CloseTutorialWindow);
            
            exitButtonDiary.onClick.AddListener(CloseDiaryWindow);
            
            tutorialPanel.SetActive(true);
            
            exitButtonTutorial.transform
                .DOScale(1.2f, 0.5f)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetUpdate(true);
            
            tutorialContentUI.InstantiateTweens();

        }

        private void Start()
        {
            tutorialPanel.SetActive(false);
        }

        public void OpenTutorialWindow(TutorialScriptableObject tutorialToShow)
        {
            tutorialContentUI.SetCurrentTutorialZero(tutorialToShow);
            tutorialPanel.SetActive(true);
            tutorialIsOpen.Value = true;
            Time.timeScale = 0f;
        }
        
        private void CloseTutorialWindow()
        {
            tutorialPanel.SetActive(false);
            tutorialIsOpen.Value = false;
            tutorialContentUI.currentTutorialNumber.Value = 0;
            Time.timeScale = 1f;
        }
        
        public void OpenNoteWindow(NoteScriptableObject noteToShow)
        {
            noteIsOpen = true;
            noteContentUI.currentNote = noteToShow;

            string header = noteToShow.header;
            string content = noteToShow.contentText;
            
            notePanel.SetActive(true);
            
            switch (noteToShow.noteType)
            {
                case NoteContentUI.NoteType.SmallNote:
                    noteContentUI.OpenSmallNote(header, content);
                    break;
                case NoteContentUI.NoteType.BigNote:
                    noteContentUI.OpenBigNote(header, content);
                    break;
                default:
                    Debug.LogError("False Type.");
                    break;
            }
            
            Time.timeScale = 0f;
        }

        public void CloseNoteWindow()
        {
            noteIsOpen = false;
            notePanel.SetActive(false);
            Time.timeScale = 1f;
        }

        public void OpenDiaryWindow()
        {
            diaryPanel.SetActive(true);
            Time.timeScale = 0f;
        }

        public void CloseDiaryWindow()
        {
            diaryPanel.SetActive(false);
            Time.timeScale = 1f;
        }
        
    }
}
