using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CreditsScroller : MonoBehaviour
{
    public RectTransform mainCredits;
    public Fader fader;

    public AudioSource music;

    public float moveSpeed;
    public float moveDelay;

    private bool moveUp;

    private float endDelay;
    private float startAudioVolume;

	// Use this for initialization
	void Start ()
    {
        endDelay = 1;
        startAudioVolume = music.volume;
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (fader.GetFadeAmount() <= 0.05f && moveDelay > 0)
            moveDelay -= Time.deltaTime;

        if (moveDelay <= 0)
            moveUp = true;

        if (moveUp && mainCredits.localPosition.y < 1000)
        {
            mainCredits.localPosition = new Vector2(0, (mainCredits.localPosition.y + moveSpeed * Time.deltaTime));
        }
        else if (mainCredits.localPosition.y > 1000)
        {
            moveUp = false;
            fader.setFadeOut(true);
            if (fader.GetFadeAmount() >= 1)
            {
                endDelay -= Time.deltaTime;
                music.volume = startAudioVolume * endDelay;
                if (endDelay <= 0)
                    SceneManager.LoadScene(0);
            }
        }
	}
}
