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

public class MasterUI : MonoBehaviour
{
    public VideoPlayer Video;
    private float VideoStartTime = 0;
    private float AudioStartTime = 0;
    private bool StartedVideo = false;
    public AudioSource Audio;
    public float FadeOutTime = 0.25f;
    public float VideoDelayTime = 0.4f;
    private RenderTexture RT;
    private float Alpha = 0;

    public static bool IsBusy = false;
    private bool IsFinished = false;

    private void Awake()
    {
        IsBusy = true;
    }

    // Start is called before the first frame update
    void Start()
    {
        RT = new RenderTexture((int)Video.clip.width, (int)Video.clip.height, 0);
        Video.targetTexture = RT;
        StartCoroutine(DelayPlaying());
    }

    IEnumerator DelayPlaying()
    {
        if (VideoDelayTime < 0)
        {
            Video.Play();
            VideoStartTime = Time.timeSinceLevelLoad;
            StartedVideo = true;
        }
        else
        {
            Audio.Play();
            AudioStartTime = Time.timeSinceLevelLoad;
        }

        if(VideoDelayTime != 0)
            yield return new WaitForSeconds(Mathf.Abs(VideoDelayTime));

        if (VideoDelayTime < 0)
        {
            Audio.Play();
            AudioStartTime = Time.timeSinceLevelLoad;
        }
        else
        {
            Video.Play();
            VideoStartTime = Time.timeSinceLevelLoad;
            StartedVideo = true;
        }
        yield return null;
    }

    // Update is called once per frame
    void Update()
    {
        float VideoPlayTime = Time.timeSinceLevelLoad - VideoStartTime;
        float AudioPlayTime = Time.timeSinceLevelLoad - AudioStartTime;

        if (Video.isPlaying)
        {            
            Alpha = 1 - Mathf.InverseLerp((float)Video.clip.length - FadeOutTime, (float)Video.clip.length, VideoPlayTime);
            if (FadeOutTime < 0)
                Alpha = 1 - Alpha;
        }
        else if (StartedVideo && VideoPlayTime >= Video.clip.length - FadeOutTime)
        {
            Video.enabled = false;
            Alpha = 0;
            IsBusy = false;
        }

        if (AudioPlayTime > Audio.clip.length)
        {
            IsFinished = true;
            GetComponent<MainMenu>().GoToNextScene();
        }
    }

    private void OnGUI()
    {
        if(!IsFinished && IsBusy)
        {
            GUI.color = new Color(0, 0, 0, 1);
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Texture2D.whiteTexture, ScaleMode.StretchToFill);
        }

        if(Alpha > 0)
        {
            GUI.color = new Color(1, 1, 1, Alpha);
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), RT, ScaleMode.StretchToFill);
        }

        if (StartedVideo)
            return;
        GUI.color = Color.black;
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Texture2D.whiteTexture, ScaleMode.StretchToFill);
    }
}
