using UnityEngine;

public class EnemyContainer : MonoBehaviour
{
    public GameObject enemyPrefab;
    public GameObject environmentObj;
    public GameObject targetControllerObj;

    private TargetController targetCon;

    private void Start()
    {
        targetCon = targetControllerObj.GetComponent<TargetController>();
    }

    // initialize enemy by random
    public void RandomInitEnemys(int EnemyNum)
    {
        for (int i = 0; i < EnemyNum; i++)
        {
            float randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
            float randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
            int enemyY = 1;
            InitEnemyAtHere(new Vector3(randX, enemyY, randZ));
        }
    }

    // initialize enemy by random but not in block area
    public void RandomInitEnemysExcept(int enemyNum, Vector3 blockPosition, float sceneSize)
    {
        float randX = 0f;
        float randZ = 0f;
        for (int i = 0; i < enemyNum; i++)
        {
            randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
            randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
            while (Vector3.Distance(blockPosition, new Vector3(randX, 0f, randZ)) < sceneSize / 2)
            {
                // while in scene area then respawn
                Debug.Log("spawn enemy in area, re:roll");
                randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
                randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
            }

            int enemyY = 1;
            InitEnemyAtHere(new Vector3(randX, enemyY, randZ));
        }
    }

    // initialize enemy to thisPosition
    public void InitEnemyAtHere(Vector3 thisPosition)
    {
        Instantiate(enemyPrefab, thisPosition + environmentObj.transform.position, Quaternion.identity, this.transform);
    }

    // destroyEnemy delete enemyContainer's all enemy
    public void DestroyAllEnemys()
    {
        foreach (Transform childObj in this.transform)
        {
            childObj.GetComponent<States>().DestroyMe();
        }
    }
}