using DG.Tweening;
using TMPro;
using Tutorials;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace DialogueSystem.Tutorials
{
    public class TutorialContentUI : MonoBehaviour
    {
        public TutorialScriptableObject currentTutorial;
        public IntReactiveProperty currentTutorialNumber;

        public Button nextVideoButton;
        public Button previousVideoButton;

        [Header("UI Elements")]
        // UI elements to change
        public VideoPlayer videoPlayer;
        public RawImage videoRenderImage;
        public TextMeshProUGUI headerText;
        public TextMeshProUGUI contentText;
        
        // Button Animation
        private Tween nextButtonTween;
        private Tween previousButtonTween;
        [Header("Animation")]
        public float moveDistance;
        public float moveDuration;

        public void InstantiateTweens()
        {
            previousButtonTween = previousVideoButton.transform.DOLocalMoveX(
                    previousVideoButton.transform.localPosition.x + moveDistance, moveDuration)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetAutoKill(false)
                .SetUpdate(true);
    
            nextButtonTween = nextVideoButton.transform.DOLocalMoveX(
                    nextVideoButton.transform.localPosition.x + moveDistance, moveDuration)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetAutoKill(false)
                .SetUpdate(true);
        }

        private void OnEnable()
        {
            InstantiateUI();
        }

        public void InstantiateUI()
        {
            Debug.Log("OnEnable");
            nextVideoButton.onClick.AddListener(GoToNextVideo);
            previousVideoButton.onClick.AddListener(GoToPreviousVideo);    
            
            nextButtonTween.Restart();
            previousButtonTween.Pause();
            
            if (currentTutorial.tutorialClips.Count > 1)
            {
                EnableNextAndPreviousButton();
                currentTutorialNumber.Value = 0;
                previousVideoButton.interactable = false;
                videoPlayer.clip = currentTutorial.tutorialClips[0];
                contentText.text = currentTutorial.contentText[0];
            }
            else
            {
                // only one video does not need the next and previous 
                DisableNextAndPreviousButton();
            }
        }

        private void GoToNextVideo()
        {
            currentTutorialNumber.Value++;
            videoPlayer.clip = currentTutorial.tutorialClips[currentTutorialNumber.Value];
            contentText.text = currentTutorial.contentText[currentTutorialNumber.Value];
            
            if (currentTutorialNumber.Value == currentTutorial.tutorialClips.Count - 1)
            {
                OnTutorialEndPageReached();
                return;
            }
            
            OnNextPageReached();
            
        }

        private void GoToPreviousVideo()
        {
            currentTutorialNumber.Value--;
            videoPlayer.clip = currentTutorial.tutorialClips[currentTutorialNumber.Value];
            contentText.text = currentTutorial.contentText[currentTutorialNumber.Value];
            
            if (currentTutorialNumber.Value == 0)
            {
                OnTutorialStartPageReached();
                return;
            }

            OnNextPageReached();

        }

        private void OnTutorialStartPageReached()
        {
            previousVideoButton.interactable = false;
            nextVideoButton.interactable = true;
            nextButtonTween.Restart();
            previousButtonTween.Pause();
        }

        private void OnNextPageReached()
        {
            previousVideoButton.interactable = true;
            nextVideoButton.interactable = true;
            if(!nextButtonTween.IsPlaying())
                nextButtonTween.Play();
            if(!previousButtonTween.IsPlaying())
                previousButtonTween.Play();
        }

        private void OnTutorialEndPageReached()
        {
            nextVideoButton.interactable = false;
            previousVideoButton.interactable = true;
            previousButtonTween.Restart();
            nextButtonTween.Pause();
        }

        public void SetCurrentTutorialZero(TutorialScriptableObject current)
        {
            
            currentTutorial = current;

            headerText.text = currentTutorial.headerText;
            contentText.text = currentTutorial.contentText[currentTutorialNumber.Value];
            
            VideoClip clip = currentTutorial.tutorialClips[currentTutorialNumber.Value];
            if (clip != null)
            {
                videoPlayer.gameObject.SetActive(true);
                videoRenderImage.gameObject.SetActive(true);
                videoPlayer.clip = currentTutorial.tutorialClips[currentTutorialNumber.Value];
            }
            else
            {
                videoRenderImage.gameObject.SetActive(false);
                videoPlayer.gameObject.SetActive(false);  
            } 
           
            bool hasMultiple = currentTutorial.tutorialClips.Count > 1;
            if (hasMultiple) EnableNextAndPreviousButton();
            else DisableNextAndPreviousButton();

            nextButtonTween.Restart();
            
            OnTutorialStartPageReached();
        }

        private void DisableNextAndPreviousButton()
        {
            nextVideoButton.gameObject.SetActive(false);
            previousVideoButton.gameObject.SetActive(false);
        }

        private void EnableNextAndPreviousButton()
        {
            nextVideoButton.gameObject.SetActive(true);
            previousVideoButton.gameObject.SetActive(true);
        }
        
    }
}
