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

public class LocalEnemySpawner : MonoBehaviour
{
    // Reference to the Prefab. Drag a Prefab into this field in the Inspector.
    public GameObject EnemyPrefab;
    public AudioClip[] audioClips;
    public AudioSource audioSource;
    public AudioListener audioListener;

    public float spawn_radius = 1.0f;
    
    public int start_num = 20;
    public float spawn_rate = 1.0f;

    public float radius = 15.0f;
    public float min_distance = 1f;

    private float time_accumulator = 0.4f;


    // Vector3 generateRandomPolarCoordinate()
    // {
    //     return new Vector3(1,0,0);
    // }

    // void convertPolarToCarteesianAndNormal(Vector3 polar, out Vector3 position, Vector3 normal)
    // {
    //     //position = new Vector3(Mathf.Cos(cornerAngle) * radius, Mathf.Sin(cornerAngle) * radius,1);
    // }

    Vector3 generateRandomSphericalVector()
    {
        float lon = Random.Range(0,Mathf.PI*2f);
        float lat = Random.Range(0,Mathf.PI*2f);

        return new Vector3(Mathf.Cos(lat)*Mathf.Cos(lon), Mathf.Cos(lat)*Mathf.Sin(lon), Mathf.Sin(lat));
    }

    bool isSpawnPositionOk(Vector3 pos)
    {
        if(Vector3.Distance(pos, transform.position)<min_distance)return false;


        GameObject[] gos = GameObject.FindGameObjectsWithTag("Enemy");

        foreach (GameObject obj in gos)
        {
            if(Vector3.Distance(pos, obj.transform.position)<min_distance)return false; 
        }
        return true;
    }

    Vector3 generateSpawnPosition()
    {
        Vector3 pos= generateRandomSphericalVector();
        pos+= new Vector3(1.0f,1.0f,1.0f);
        pos*=spawn_radius;
        pos += transform.position;

        pos = Vector3.Normalize(pos)*radius;

        return pos;
    }

    void spawn()
    {
        Vector3 pos;
        int max_tries=100;

        do{
            pos = generateSpawnPosition();
        }while(max_tries-->0  && !isSpawnPositionOk(pos));

        if(max_tries<0)return;

        GameObject instance = Instantiate(EnemyPrefab, pos, Quaternion.FromToRotation (transform.up, pos) * transform.rotation);
        //instance.transform.SetParent(transform);
        if (!audioSource.isPlaying)
        {
            PlayRandom();
        }

        //objects.Add(instance);
    }

    void PlayRandom()
    {
        audioSource.clip = audioClips[Random.Range(0, audioClips.Length)];
        audioSource.Play();
    }

    // Start is called before the first frame update
    void Start()
    {
        //radius = transform.localScale.x/2.0f;
        radius = transform.position.magnitude;

        for(int i=0;i<start_num;i++)
        {
            spawn();
        }

        audioListener = GetComponent<AudioListener>();
        audioSource = gameObject.GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if(spawn_rate>0.0f){
                
            time_accumulator += Time.deltaTime;
            if(time_accumulator> spawn_rate){
                spawn();
                time_accumulator=0;
            }
        }
    }
}
