using UnityEngine;
using System.Collections;

public class Temple : MonoBehaviour {

    public int monksCount = 66;
    public float monksScale = 0.6f;
	public GameObject[] monkPrefabs;
    public GameObject suspicionPrefab;

    private float maxX;
    private float minX;
    private float maxY;
    private float minY;

    void Awake () {
        maxX = +GameMgr.instance.maxWidth / 2;
        minX = -GameMgr.instance.maxWidth / 2;
        maxY = +GameMgr.instance.maxHeight / 2;
        minY = -GameMgr.instance.maxHeight / 2;
    }
	
	// Update is called once per frame
	void Update () {
	
	}


    void OnEnable()
    {
        int howManyColors = GameMgr.instance.colArray.Length;
        GameObject fakeMonk = InstantiateMonk("Infiltrator", Random.Range(0, howManyColors), true);
        fakeMonk.GetComponent<NPC>().enabled = false;
        fakeMonk.GetComponent<PC>().enabled = true;
        for (int i = 1; i < monksCount; i++)
        {
            InstantiateMonk("Monk #" + i, Random.Range(0, howManyColors));
        }
    }

	private GameObject InstantiateMonk(string monkName, int color, bool player=false)
    {
		GameObject newMonk = GameObject.Instantiate<GameObject>(monkPrefabs[color]) as GameObject;
        float x = Random.Range(minX, maxX);
        float y = Random.Range(minY, maxY);
        float z = y / 10;
        newMonk.transform.position = new Vector3(x, y, z);
        newMonk.transform.parent = this.gameObject.transform;
        newMonk.transform.localScale = Vector3.one * monksScale;
        newMonk.name = monkName;
		if(!player)
			Destroy(newMonk.gameObject.GetComponent<Rigidbody2D>());
        newMonk.GetComponent<Monk>().color = color;
        newMonk.SetActive(true);
        GameObject newSuspicion = GameObject.Instantiate<GameObject>(suspicionPrefab);
        newSuspicion.transform.parent = newMonk.transform;
        newSuspicion.transform.localPosition = Vector3.zero;
        newSuspicion.name = "Suspicion on " + monkName;
        return newMonk;
    }


    void OnDisable() // TODO Check if it works!
    {
        foreach (Transform t in transform)
        {
            Destroy(t.gameObject);
        }
    }
}
