using UnityEngine;
using System.Collections;

public class ProgressiveMobSpawner : MonoBehaviour
{
    public int[] baseScoreForSpawn = new int[] { 1500, 5000, 20000 };
    public int[] intervalScoreForSpawn = new int[] { 2500, 5000, 20000 };
    private int[] spawnCount;
    public GameObject[] spawnPrefabs;
    public Vector3[] spawnLocations;
    public Vector3[] spawnRotations;
    public string[] messageColors;
    public AudioClip[] spawnSounds;
    public string[] spawnMessages;
    public float spawnCooldown = 15f;
    public float spawnTimer;


    private PlayerController playerController;

    public void DecreaseSpawnCount(int index)
    {
        spawnCount[index]--;
    }
	
    void Awake()
    {
        playerController = FindObjectOfType<PlayerController>();
    }

	void Start () 
    {
        spawnCount = new int[spawnPrefabs.Length];
	}
	
	// Update is called once per frame
	void Update () 
    {
        spawnTimer -= Time.deltaTime;

        if (spawnTimer <= 0f)
        {
            for (int i = spawnPrefabs.Length - 1; i >= 0; i--)
            {
                int target = playerController.score >= baseScoreForSpawn[i] ? (playerController.score - baseScoreForSpawn[i]) / intervalScoreForSpawn[i] + 1 : 0;
                // Debug.Log(i + ":" + target);
                if (spawnCount[i] < target)
                {
                    playerController.message = "<color=" + messageColors[i] + ">" + spawnMessages[Random.Range(0, spawnMessages.Length)] + "</color>";
                    var index = Random.Range(0, spawnLocations.Length);
                    Instantiate(spawnPrefabs[i], spawnLocations[index], Quaternion.Euler(spawnRotations[index]));
                    spawnCount[i]++;
                    OneShot3DAudio.PlayClipAtPoint(spawnSounds[i], spawnLocations[index]);
                }
            }
            spawnTimer = spawnCooldown;
        }
	
	}
}
