using System;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using Tutorials;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace DialogueSystem
{
    public class MainMenuManager : MonoBehaviour
    {
        [Header("Buttons")] 
        public Button startButton;
        public Button tutorialsButton;
        public Button settingsButton;
        public Button closeSettingsButton;
        public Button tutorialsBackButton;
        public Button quitGameButton;
        
        [Header("Prefabs")]
        // this is the parent object to which we want to add our tutorials
        public GameObject tutorialsLayoutGroup;
        // this is the tutorial button prefab, which gets instantiated when tutorials are added to the archive
        public Button tutorialButtonPrefab;

        [Header("Panels")]
        public GameObject tutorialsPanel;
        public GameObject mainMenuPanel;
        public DialogueManager dialogueManager;
        public GameObject settingsPanel;

        private List<TutorialScriptableObject> tutorialsToShow;
        private List<Button> tutorialButtons = new List<Button>();
        
        [Serializable]
        public enum PanelType
        {
            MainMenu,
            Tutorials
        }

        private void Awake()
        {
            //startButton.onClick.AddListener(() => OpenPanel(PanelType.MainMenu));
            tutorialsButton.onClick.AddListener(() => OpenPanel(PanelType.Tutorials));
            tutorialsBackButton.onClick.AddListener(() => OpenPanel(PanelType.MainMenu));
            settingsButton.onClick.AddListener(() => OpenSettings());
            closeSettingsButton.onClick.AddListener(() => CloseSettings());
            
            AddTutorialsFromArchive();

            startButton.transform
                .DOScale(2.1f, 0.5f)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetUpdate(true);
            
            tutorialsButton.transform
                .DOScale(2.1f, 0.5f)
                    .SetLoops(-1, LoopType.Yoyo)
                    .SetEase(Ease.InOutSine)
                    .SetUpdate(true);
            
            settingsButton.transform
                .DOScale(2.1f, 0.5f)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetUpdate(true);
            
            quitGameButton.transform
                .DOScale(2.1f, 0.5f)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetUpdate(true);
            
            if (tutorialsToShow.Count == 0)
                tutorialsButton.interactable = false;
            
        }

        private void OpenSettings()
        {
            settingsPanel.SetActive(true);
            startButton.gameObject.SetActive(false);
            settingsButton.gameObject.SetActive(false);
            tutorialsButton.gameObject.SetActive(false);
            quitGameButton.gameObject.SetActive(false);
        }

        private void CloseSettings()
        {
            settingsPanel.SetActive(false);
            startButton.gameObject.SetActive(true);
            settingsButton.gameObject.SetActive(true);
            tutorialsButton.gameObject.SetActive(true);
            quitGameButton.gameObject.SetActive(true);
        }

        private void Start()
        {
            dialogueManager.tutorialIsOpen
                .Skip(1)
                .Subscribe(tutorialOpen =>
                {
                    if (tutorialOpen)
                    {
                        tutorialsPanel.SetActive(false);
                    }
                    else
                    {
                        tutorialsPanel.SetActive(true);
                    }
                });
        }


        public void OpenPanel(PanelType panelType)
        {
            
            if (panelType == PanelType.MainMenu)
            {
                mainMenuPanel.SetActive(true);
                tutorialsPanel.SetActive(false);
            }

            if (panelType == PanelType.Tutorials)
            {
                mainMenuPanel.SetActive(false);
                tutorialsPanel.SetActive(true);
                OnTutorialsOpen();
            }
        }

        private void OnTutorialsOpen()
        {
            DialogueManager.Instance.tutorialContentUI.InstantiateUI();
            
            var archivedItems = GenericScriptableObjectArchive
                .GetArchivedItems<TutorialScriptableObject>("Tutorial", "Tutorials");

            DeleteAllButtons();
            
            for (int i = 0; i < archivedItems.Count; i++)
            {
                var archivedItem = archivedItems[i];
                
                var tutorialButton = Instantiate(tutorialButtonPrefab, tutorialsLayoutGroup.transform, true);
                tutorialButton.GetComponentInChildren<TextMeshProUGUI>().text = archivedItem.headerText;

                tutorialButtons.Add(tutorialButton);
                
                tutorialButton.onClick.AddListener(() => { TutorialButtonAction(archivedItem);});
                
            }
            
        }

        private void DeleteAllButtons()
        {
            foreach (var button in tutorialButtons)
            {
                DestroyImmediate(button.gameObject);
            }
            tutorialButtons.Clear();
        }
        
        private void TutorialButtonAction(TutorialScriptableObject tutorialScriptableObject)
        {
            // TODO: call the window to show the tutorial
            Debug.Log("TODO: call the window to show the tutorial");
            dialogueManager.OpenTutorialWindow(tutorialScriptableObject);
        }
        
        private void AddTutorialsFromArchive()
        {
            tutorialsToShow = GenericScriptableObjectArchive.GetArchivedItems<TutorialScriptableObject>("Tutorial", "Tutorials");
        }
        
    }
}
