using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IntroCinematic : MonoBehaviour
{
    public PlayerControl player;
    public PlayerAnimator playerAnim;
    public CameraHandler cameraHandler;
    public GameObject mainMenuContainer;
    public ISoundEffect soundPop;
    public ScreenShake shake;
    public AnimationCurve introRumble;
    public Animator femaleAnimator;
    public Animator meteoriteAnimator;
    public float standardShakeMagnitude = 0.1f;
    public GameObject tutorial;
    public float musicVolume = 0.2f;
    public AudioSource music;
    public CollectiblesTracker collectiblesTracker;

    private bool started = false;

    void Update() {
        if(!started && Input.GetButtonDown("Jump")) {
            started = true;
            StartCoroutine(RunIntro());
        }
    }

    private IEnumerator RunIntro() {
        // Play pop sound
        soundPop.Trigger();

        // Hide play button and such
        mainMenuContainer.SetActive(false);

        // Start female animator
        femaleAnimator.SetTrigger("FemaleIntro");
        meteoriteAnimator.SetTrigger("Meteorite");
        
        yield return null;

        // Fade out music
        float musicTime = Time.timeSinceLevelLoad;
        while(Time.timeSinceLevelLoad - musicTime < 1f) {
            float percent = (Time.timeSinceLevelLoad - musicTime) / 1f;
            music.volume = (1-percent) * musicVolume;
            yield return null;
        }
        // Stop music
        music.Stop();

        // Start rumble
        float totalRumbleTime = 7f;

        shake.shakeMagnitude = 0f;
        shake.TriggerShake(totalRumbleTime);
        float rumbleTime = Time.timeSinceLevelLoad;
        while(Time.timeSinceLevelLoad - rumbleTime < .5f) {
            float percent = (Time.timeSinceLevelLoad - rumbleTime) / totalRumbleTime;
            shake.shakeMagnitude = introRumble.Evaluate(percent);
            yield return null;
        }

        playerAnim.TriggerIntroLookUp();
        
        while(Time.timeSinceLevelLoad - rumbleTime < 1f) {
            float percent = (Time.timeSinceLevelLoad - rumbleTime) / totalRumbleTime;
            shake.shakeMagnitude = introRumble.Evaluate(percent);
            yield return null;
        }

        // Run player to the right
        float time = Time.timeSinceLevelLoad;
        while(Time.timeSinceLevelLoad - time < 0.8f) {
            // Player
            player.ResetInput();
            player.AutoRunRight();

            // Rumble
            float percent = (Time.timeSinceLevelLoad - rumbleTime) / totalRumbleTime;
            shake.shakeMagnitude = introRumble.Evaluate(percent);
            yield return null;
        }
        player.ResetInput();
        
        while(Time.timeSinceLevelLoad - rumbleTime < 7f) {
            float percent = (Time.timeSinceLevelLoad - rumbleTime) / totalRumbleTime;
            shake.shakeMagnitude = introRumble.Evaluate(percent);
            yield return null;
        }

        // Fade in music
        music.Play();
        musicTime = Time.timeSinceLevelLoad;
        while(Time.timeSinceLevelLoad - musicTime < 0.5f) {
            float percent = (Time.timeSinceLevelLoad - musicTime) / 1f;
            music.volume = percent * musicVolume;
            yield return null;
        }

        // Enable user controls
        EnableControls();
        // Start game timer
        collectiblesTracker.StartGameTimer();
        
        // Continue fade in music
        while(Time.timeSinceLevelLoad - musicTime < 1f) {
            float percent = (Time.timeSinceLevelLoad - musicTime) / 1f;
            music.volume = percent * musicVolume;
            yield return null;
        }

        //yield return new WaitForSeconds(0.5f);

        tutorial.SetActive(true);
    }

    private void EnableControls() {
        shake.shakeMagnitude = standardShakeMagnitude;
        player.enableUserControl = true;
        cameraHandler.enabled = true;
    }
}
