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

public class SpawnerManager : MonoBehaviour{

    [SerializeField] Vector3 offset;
    [SerializeField] Vector3 size;
    [Range(.25f, 10f)] [SerializeField] float spaceBetweenPoints = 1.25f;
    [Range(.15f, 5f)] [SerializeField] float gizmosSphereSize = .15f;
    [SerializeField] LayerMask whatPositionIsOccuped;

    [Space]
    [SerializeField] int initialNumberToSpawn = 5;
    [SerializeField] int numberToSpawn;
    [SerializeField] string[] enemyTypes;
    List<Vector3> possiblePositions;

    void Awake(){
        possiblePositions = new List<Vector3>();
        for (int i = 0; i < (size.z / spaceBetweenPoints); i++){
            for (int j = 0; j < (size.x / spaceBetweenPoints); j++){
                Vector3 pos = (new Vector3(offset.x - (size.x / 2), offset.y, offset.z - (size.z / 2)) + new Vector3(spaceBetweenPoints * j, 0, spaceBetweenPoints * i));
                possiblePositions.Add(pos);
            }
        }
    }

    // Start is called before the first frame update
    void Start(){
        StartCoroutine(InitialSpawn());
    }

    IEnumerator InitialSpawn(){
        List<Vector3> pawnsPositions = new List<Vector3>();
        while (pawnsPositions.Count < initialNumberToSpawn){
            Vector3 possiblePosition = possiblePositions[Random.Range(0, possiblePositions.Count)];
            if (!pawnsPositions.Contains(possiblePosition)){
                pawnsPositions.Add(possiblePosition);
            }
        }
        foreach (Vector3 pos in pawnsPositions){
            yield return new WaitForSeconds(Random.Range(0, .02f));
            ObjectPooler.instance.SpawnFromPool(enemyTypes[0].ToString(), pos, Quaternion.identity);
        }

        List<Vector3> rookPositions = new List<Vector3>();
        while (rookPositions.Count < initialNumberToSpawn){
            Vector3 possiblePosition = possiblePositions[Random.Range(0, possiblePositions.Count)];
            if (!rookPositions.Contains(possiblePosition)){
                rookPositions.Add(possiblePosition);
            }
        }
        foreach (Vector3 pos in rookPositions){
            yield return new WaitForSeconds(Random.Range(0, .02f));
            ObjectPooler.instance.SpawnFromPool(enemyTypes[1].ToString(), pos, Quaternion.identity);
        }

        List<Vector3> knightPositions = new List<Vector3>();
        while (knightPositions.Count < initialNumberToSpawn){
            Vector3 possiblePosition = possiblePositions[Random.Range(0, possiblePositions.Count)];
            if (!knightPositions.Contains(possiblePosition)){
                knightPositions.Add(possiblePosition);
            }
        }
        foreach (Vector3 pos in knightPositions){
            yield return new WaitForSeconds(Random.Range(0, .02f));
            ObjectPooler.instance.SpawnFromPool(enemyTypes[2].ToString(), pos, Quaternion.identity);
        }

        List<Vector3> bishopPositions = new List<Vector3>();
        while (bishopPositions.Count < initialNumberToSpawn){
            Vector3 possiblePosition = possiblePositions[Random.Range(0, possiblePositions.Count)];
            if (!bishopPositions.Contains(possiblePosition)){
                bishopPositions.Add(possiblePosition);
            }
        }
        foreach (Vector3 pos in bishopPositions){
            yield return new WaitForSeconds(Random.Range(0, .02f));
            ObjectPooler.instance.SpawnFromPool(enemyTypes[3].ToString(), pos, Quaternion.identity);
        }
    }

    // Update is called once per frame
    void Update(){
        
    }

    public IEnumerator Spawn(){
        List<Vector3> positions = new List<Vector3>();
        while (positions.Count < numberToSpawn){
            Vector3 possiblePosition = possiblePositions[Random.Range(0, possiblePositions.Count)];
            if(!Physics.CheckSphere(possiblePosition, .3f, whatPositionIsOccuped)){
                if (!positions.Contains(possiblePosition)){
                    positions.Add(possiblePosition);
                }
            }
        }
        foreach (Vector3 pos in positions){
            yield return new WaitForSeconds(Random.Range(0, .02f));
            ObjectPooler.instance.SpawnFromPool(enemyTypes[Mathf.RoundToInt(Random.Range(0, 300) / 100)].ToString(), pos, Quaternion.identity);
        }
    }

    void OnDrawGizmosSelected(){
        if (!Application.isPlaying){
            Gizmos.DrawWireCube(offset, size);

            for (int i = 0; i < (size.z / spaceBetweenPoints); i++){
                for (int j = 0; j < (size.x / spaceBetweenPoints); j++){
                    Vector3 pos = (new Vector3(offset.x - (size.x / 2), offset.y, offset.z - (size.z / 2)) + new Vector3(spaceBetweenPoints * j, 0, spaceBetweenPoints * i));
                    Gizmos.DrawSphere(pos, gizmosSphereSize);
                }
            }
        }
    }
}
