using System;
using System.Linq;
using Assets.Scripts.Core;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Credits : MonoBehaviour {
    private bool _done;

    protected void Start() {
        var text = GetComponent<Text>();

        MusicController.Instance.PlayCredits();

        var lines = new String[] {
            "\"A Horrible Night\"",
            "",
            "Global Game Jam 2015",
            "",
            "",
            "",
            "Programming",
            "",
            "Pedro Medeiros",
            "Rodrigo Braz Monteiro",
            "",
            "",
            "",
            "Background Art",
            "",
            "Lucas Carvalho",
            "",
            "",
            "",
            "Character Art",
            "",
            "Amora B.",
            "",
            "",
            "",
            "Additional Art",
            "",
            "Luciana Nascimento",
            "",
            "",
            "",
            "Animation",
            "",
            "Pedro Medeiros",
            "",
            "",
            "",
            "Music",
            "",
            "Gustavo Barcamor",
            "",
            "",
            "",
            "Writing",
            "",
            "Amora B.",
            "Rodrigo Braz Monteiro",
            "",
            "",
            "",
            "Game Design",
            "",
            "Pedro Medeiros",
            "Rodrigo Braz Monteiro",
            "",
            "",
            "",
            "Sound",
            "",
            "Lucas Carvalho",
            "",
            "",
            "",
            "Food",
            "",
            "Renato Favarin",
            "",
            "",
            "",
            "Copyright (c) 2015",
            "",
            "",
            "",
            "Follow us on Twitter:",
            "",
            "@amora_b @amzeratul @barcamor @midio @renatoapf",
            "@saint11 @studiominiboss @viiolaceus",
            "",
            "",
            "",
            "Thanks for playing!",
        };

        text.text = lines.Aggregate((a, b) => a + "\n" + b);
        FadeController.Instance.FadeIn(3.0f);
    }

    protected void Update() {
        var pos = transform.localPosition;
        pos.y += Time.deltaTime * 100;
        transform.localPosition = pos;

        if (pos.y > 550 + GetComponent<RectTransform>().rect.height) {
            ExitToMenu();
        }
    }

    private void ExitToMenu() {
        if (!_done) {
            _done = true;
            StartCoroutine(GoToMenu());
        }
    }

    private IEnumerator GoToMenu() {
        FadeController.Instance.FadeOut(3.0f);
        yield return new WaitForSeconds(3.0f);
        Application.LoadLevel(0);
    }

    public void OnClick() {
        ExitToMenu();
    }
}
