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

public class Level1Manager : MonoBehaviour {

    public Collider wall1;
    public Text timeText;
    public Text EndScoreText;
    public CameraFollow camFollow;
    public CombinerScript combiner;

    public Button button0;
    public Button button1;
    public Button button2;

    AudioSource level0Audio;
    AudioSource level1Audio;
    AudioSource level2Audio;

    public AudioClip level0Clip;
    public AudioClip level1Clip;
    public AudioClip level2Clip;

    float scoretime = 0;

    public bool StartMenu = false;

    void Start () {
        if (!StartMenu)
        {
            StartCoroutine(Begin());
        }

        level0Audio = gameObject.AddComponent<AudioSource>();
        level1Audio = gameObject.AddComponent<AudioSource>();
        level2Audio = gameObject.AddComponent<AudioSource>();

        level0Audio.spatialBlend = 0;
        level0Audio.loop = true;
        level0Audio.playOnAwake = false;
        level1Audio.spatialBlend = 0;
        level1Audio.loop = true;
        level1Audio.playOnAwake = false;
        level2Audio.spatialBlend = 0;
        level2Audio.loop = true;
        level2Audio.playOnAwake = false;

        level0Audio.clip = level0Clip;
        level1Audio.clip = level1Clip;
        level2Audio.clip = level2Clip;
    }
	
	IEnumerator Begin()
    {
        yield return new WaitForSeconds(3f);
        Destroy(wall1);
        level0Audio.Play();
        level1Audio.Play();
        level2Audio.Play();
    }

	void Update () {
        timeText.text = Time.timeSinceLevelLoad.ToString() + "ms";
        timeText.color = ParticleController.player.LightColor;
        EndScoreText.color = ParticleController.player.LightColor;

        if (ParticleController.player.speed > ParticleController.player.midSpeed - 0.3 * (ParticleController.player.maxSpeed - ParticleController.player.minSpeed))
        {
            level1Audio.mute = false;
        }
        else
        {
            level1Audio.mute = true;
        }
        if (ParticleController.player.speed > ParticleController.player.midSpeed + 0.3 * (ParticleController.player.maxSpeed - ParticleController.player.minSpeed))
        {
            level2Audio.mute = false;
        }
        else
        {
            level2Audio.mute = true;
        }
    }

    private void OnCollisionEnter(Collision col)
    {
        if (col.collider.tag == "Particle")
        {
            if (!StartMenu)
            {
                if (col.gameObject == ParticleController.player.gameObject)
                {

                }
                else
                {
                    combiner.triggerCombine = true;
                }

                ParticleController.player.rb.constraints = RigidbodyConstraints.None;
                camFollow.enabled = false;

                End();
            }
            else
            {
                if (col.gameObject == ParticleController.player.gameObject)
                {

                }
                else
                {
                    combiner.triggerCombine = true;
                }
                Invoke("NextLevel", 2f);
                ParticleController.player.rb.constraints = RigidbodyConstraints.None;
            }
        }
    }

    void End()
    {
        EndScoreText.gameObject.SetActive(true);
        
        button0.gameObject.SetActive(true);
        button1.gameObject.SetActive(true);
        try
        {
            button2.gameObject.SetActive(true);
        }
        catch
        {

        }

        timeText.gameObject.SetActive(false);

        scoretime = Time.timeSinceLevelLoad;
        EndScoreText.text = SceneManager.GetActiveScene().name + "\n"
            + "Time = " + scoretime + "ms" + "\n"
            + "Luminosity = " + ParticleController.player.intensity.ToString() + "Lux" + "\n"
            + "Score = " + (1000 * ParticleController.player.intensity / scoretime).ToString() + "LUXEMS";

            

    }

    public void toTitleScreen()
    {
        SceneManager.LoadScene(0);
    } 

    public void Replay()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void titleStart()
    {
        Destroy(wall1);
        button2.gameObject.SetActive(false);
    }

    public void NextLevel()
    {
        try
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
        catch
        {

        }
    }
}
