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

public class ScoreDarkControl : MonoBehaviour {

    public float Temp1;
    public float TempDarken;
    public int DarkenTimes;
    public float Temp2;
    public float TempScore;
    public float TempScore2;
    public Transform dark;

    public Transform mainCamera;
    public Transform score;
    public Transform highScore;

    public int stepDelay;

    public AudioClip stepSound;
    public AudioClip endSound;
    public AudioClip fanfareSound;
    public AudioClip clickSound;

    private AudioSource audioSource;
    private bool inChange;
    // Use this for initialization
    void Start () {
        audioSource = GetComponent<AudioSource>();
        dark = transform;
        dark.position = new Vector2(0, dark.position.y);
        dark.GetComponent<ScoreDarkControl>().StartBrighten();
    }
    
    public void StartBrighten()
    {
        if (!inChange)
        {
            inChange = true;
            StartCoroutine(Brighten());
        }
    }
    public void StartDarken(string name)
    {
        if (!inChange)
        {
            inChange = true;
            StartCoroutine(Darken(name));
        }
    }

    IEnumerator Brighten()
    {
        inChange = true;
        ParticleSystem ps1 = score.GetChild(0).GetComponent<ParticleSystem>();
        ParticleSystem ps2 = highScore.GetChild(0).GetComponent<ParticleSystem>();
        ScoreManager.Instance.highScore = PlayerPrefs.GetInt("HighScore", 0);
        highScore.GetComponent<TextMesh>().text = ScoreManager.Instance.highScore.ToString();
        score.GetComponent<TextMesh>().text = "0";

        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
        yield return new WaitForSeconds(Temp1);
        for (float i = 0; i < TempDarken * DarkenTimes; i += TempDarken)
        {
            dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1 - (i / (float)(TempDarken * DarkenTimes)));
            yield return new WaitForSeconds(TempDarken);
        }
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
        int tempScore = 0;
        int step = 0;
        while (tempScore < ScoreManager.Instance.score)
        {
            tempScore++;
            step++;
            if (step >= stepDelay)
            {
                step = 0;
                audioSource.PlayOneShot(stepSound);
            }
            score.GetComponent<TextMesh>().text = tempScore.ToString();
            yield return new WaitForSeconds(TempScore);
        }
        float n = ((float)ScoreManager.Instance.score) / 10.0f;
        n = Mathf.Min(n, 10);
        //print(Mathf.FloorToInt(n));
        audioSource.PlayOneShot(endSound);
        ps1.Emit(Mathf.FloorToInt(n));

        if (ScoreManager.Instance.score > ScoreManager.Instance.highScore)
        {
            yield return new WaitForSeconds(TempScore2);
            audioSource.PlayOneShot(fanfareSound);
            ps1.Emit((Mathf.Min(ScoreManager.Instance.score, 100) / 10));
            ps2.Emit(10);
            ScoreManager.Instance.highScore = ScoreManager.Instance.score;
            PlayerPrefs.SetInt("HighScore", ScoreManager.Instance.highScore);
            highScore.GetComponent<TextMesh>().text = ScoreManager.Instance.highScore.ToString();
            mainCamera.GetComponent<Shaker>().StartShake();
        }
        inChange = false;
    }
    IEnumerator Darken(string name)
    {
        audioSource.PlayOneShot(clickSound);
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
        yield return new WaitForSeconds(TempScore);
        for (float i = 0; i < TempDarken * DarkenTimes; i += TempDarken)
        {
            dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, i / (float)(TempDarken * DarkenTimes));
            yield return new WaitForSeconds(TempDarken);
        }
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
        SceneManager.LoadSceneAsync(name);
        //Application.LoadLevel(name);

    }
}
