using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundBlastGenerator : MonoBehaviour {
    public static SoundBlastGenerator instance;

    [SerializeField]
    private GameObject minorTemplate, majorTemplate, medusaTemplate;

    private void Awake()
    {
        instance = this;
    }

    public IEnumerator PerformMajorClap(Vector3 origin, bool menu = false)
    {
        instance.StartCoroutine(PerformClap(origin, SoundBlast.Type.Major));
        yield return new WaitForSeconds(0.05f);
        instance.StartCoroutine(PerformClap(origin, SoundBlast.Type.Major));
        yield return new WaitForSeconds(0.05f);
        instance.StartCoroutine(PerformClap(origin, SoundBlast.Type.Major));
    }

    public IEnumerator PerformClap(Vector3 origin, SoundBlast.Type type, bool menu = false)
    {
        GameObject tmp = null;
        float speed = 100;
		float radius = 50;
        switch (type)
        {
		case SoundBlast.Type.Menu:
			tmp = minorTemplate;
			speed = 0.1f;
			radius = 50;
                break;
		case SoundBlast.Type.Minor:
			tmp = minorTemplate;
			speed = 0.75f;
			radius = 25;
                break;
		case SoundBlast.Type.Major:
			tmp = majorTemplate;
			speed = 0.5f;
			radius = 50;
                break;
		case SoundBlast.Type.Medusa:
			tmp = medusaTemplate;
			speed = 0.5f;
			radius = 7.5f;
                break;
        }

        var sphere = Instantiate(tmp);
        sphere.transform.position = origin;

        sphere.transform.localScale = Vector3.zero;
        sphere.gameObject.SetActive(true);
        yield return null;
        var scale = 0f;
        while (scale < radius)
        {
            if (Time.timeScale > 0)
            {
                sphere.transform.localScale = new Vector3(scale, scale, scale);
                scale += speed;
            }
            yield return null;
        }
		sphere.GetComponent<MeshRenderer> ().enabled = false;
		sphere.GetComponent<SphereCollider> ().enabled = false;
		yield return new WaitForSeconds (2);
        Destroy(sphere);
    }
}
