using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.Playables;

public class RulesMenuEngine : MonoBehaviour
{
    public GameObject playerCameraGO;
    public GameObject playerTrailsGO;
    public ParticleSystem starParticles;
    public PlayableDirector howToTimeline;
    private Vector3 _startPosition;

    public GameObject startGameCutscene;

    public GameObject startButton;
    public GameObject replayButton;
    public GameObject skipButton;

    public BackgroundMusicManager currentBGMManager;
    public MenuAudioManager currentMenuAudioManager;

    private bool _cutsceneEnded;

    private GameObject _lastControlSelected;

    // Start is called before the first frame update
    void Start()
    {
        // If the BGM manager is present, queue up and play the given track index
        if (currentBGMManager)
        {
            currentBGMManager.StartAudioQueueAndPlay(0);
        }

        _startPosition = playerCameraGO.transform.position;
        _cutsceneEnded = false;
    }
    public void StartGame()
    {
        // Play the menu click sound if the audio manager is present
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }
        startGameCutscene.GetComponent<PlayableDirector>().time = 0;
        startGameCutscene.SetActive(true);
    }

    public void ChangeScene()
    {
        SceneManager.LoadScene("Game");
    }

    public void PlayMenuClick()
    {
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }
    }

    public void SlowStarRate()
    {
        ParticleSystem.MainModule starsMainPS = starParticles.main;
        starsMainPS.simulationSpeed = 0.1f;
    }

    public void NormalStarRate()
    {
        ParticleSystem.MainModule starsMainPS = starParticles.main;
        starsMainPS.simulationSpeed = 1.0f;
        CheckIfEndOfTimeline();
    }

    public void BoostStarRate()
    {
        ParticleSystem.MainModule starsMainPS = starParticles.main;
        starsMainPS.simulationSpeed = 10.0f;
    }

    public void EscapeStarRate()
    {
        ParticleSystem.MainModule starsMainPS = starParticles.main;
        starsMainPS.maxParticles = 10000;
        starsMainPS.simulationSpeed = 100.0f;
        starsMainPS.startColor = Color.red;
    }

    public void DoCutSceneSkip()
    {
        double fullTime = System.Math.Floor(howToTimeline.time);
        int previousSecondMark = (int)fullTime % 10;
        howToTimeline.time = (fullTime - previousSecondMark) + 10;
        CheckIfEndOfTimeline();
    }

    public void CheckIfEndOfTimeline()
    {
        if ((howToTimeline.time + 11) >= howToTimeline.duration)
        {
            skipButton.SetActive(false);
            EventSystem.current.SetSelectedGameObject(startButton);
        }
    }

    public void RestartHowToTimeline()
    {
        howToTimeline.gameObject.SetActive(true);
        skipButton.SetActive(true);
        NormalStarRate();
        
        _cutsceneEnded = false;
        playerCameraGO.transform.position = _startPosition;

        foreach (TrailRenderer currentTrail in playerTrailsGO.GetComponentsInChildren<TrailRenderer>())
        {
            currentTrail.Clear();
        }        
            
        howToTimeline.Play();
    }

    public void EndHowToTimeline()
    {
        howToTimeline.gameObject.SetActive(false);
        skipButton.SetActive(false);
        EventSystem.current.SetSelectedGameObject(startButton);        
        replayButton.SetActive(true);
        playerCameraGO.transform.position = _startPosition;
        _cutsceneEnded = true;
        NormalStarRate();
    }

    // Update is called once per frame
    void Update()
    {
        if (_cutsceneEnded)
        {
            //playerTrailsGO.SetActive(true);
            playerCameraGO.transform.position = playerCameraGO.transform.forward * 0.05f * Time.deltaTime;
        }
        
    }
}
