using UnityEngine;
using System.Collections;

public class ClickController : MonoBehaviour {
    public Transform Player;
    public Transform LivesTranform;
    public Transform SpawnerObj;
    public Transform particles1;
    public Transform particles2;
    public Transform ParentObjects;
    public Transform TimeText;
    public Transform trail;
    public Transform mainCamera;
    public Transform Darken;


    public AudioClip deathSound;
    public AudioClip[] timeSounds;
    public AudioClip[] psySounds;

    private AudioSource audioSource;
    private Lifes lif;
    private ParticleSystem ps1;
    private ParticleSystem ps2;
    private int points;
    private bool lose;
    private int soundStep;
    // Use this for initialization
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        lose = false;
        points = 0;
        soundStep = 0;
        TimeText.GetComponent<TextMesh>().text = points.ToString();
        lif = LivesTranform.GetComponent<Lifes>();
        ps1 = particles1.GetComponent<ParticleSystem>();
        ps2 = particles2.GetComponent<ParticleSystem>();
        
        trail.GetComponent<TrailRenderer>().sortingLayerName = "Background";
        trail.GetComponent<TrailRenderer>().sortingOrder = 10;
        Darken.GetComponent<DarkControl>().StartGame();
        
    }

    IEnumerator waitAndAdd()
    {
        while (!lose)
        {
            yield return new WaitForSeconds(1);
            if (!lose && Darken.GetComponent<DarkControl>().IsInGame())
            {
                audioSource.PlayOneShot(timeSounds[soundStep]);
                soundStep++;
                if (soundStep >= timeSounds.Length)
                {
                    soundStep = 0;
                }

                points++;
                TimeText.GetComponent<TextMesh>().text = points.ToString();
            }
        }
    }
    public void loseGame()
    {
        audioSource.PlayOneShot(deathSound);
        lose = true;
        StartCoroutine(waitAndShake());

        ScoreManager.Instance.score = points;
        ScoreManager.Instance.highScore = PlayerPrefs.GetInt("HighScore", 0);
    }
    IEnumerator waitAndShake()
    {
        yield return new WaitForSeconds(0.2f);
        mainCamera.GetComponent<Shaker>().StartShake();
        yield return new WaitForSeconds(0.1f);
        Destroy(Player.gameObject, 0.5f);
        Darken.GetComponent<DarkControl>().EndGame();
    }
    // Update is called once per frame
    void Update ()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            if ((lif.getUses() > 0)&&(Darken.GetComponent<DarkControl>().IsInGame()))
            {
                lif.useRing();
                for (int i = 0; i < ParentObjects.childCount; i++)
                {
                    ParentObjects.GetChild(i).GetComponent<ClickRepulseObjects>().useForce();
                }
                mainCamera.GetComponent<Shaker>().StartShake();
                StartCoroutine(generateWaves());
                audioSource.PlayOneShot(psySounds[Random.Range((int)0, psySounds.Length)]);
            }
        }

    }
    public void StartGame()
    {
        StartCoroutine(waitAndAdd());
        SpawnerObj.GetComponent<Spawner>().startSpawning();
    }
    IEnumerator generateWaves()
    {
        Vector2 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        ps1.transform.position = mouse;
        ps2.transform.position = mouse;
        ps1.Emit(1);
        yield return new WaitForSeconds(0.05f);
        ps2.Emit(5);/*
        yield return new WaitForSeconds(0.05f);
        ps1.Emit(1);
        yield return new WaitForSeconds(0.05f);
        ps2.Emit(5);*/

    }
}
