using System.Collections;
using System.Collections.Generic;
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();
        }
    }
}
