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

public class SpawnTest : MonoBehaviour {

    public GameObject spawnParent;
    public GameObject guestPrefab;
    public GameObject[] spawnPoints;
    private NavMeshAgent guestAgent;
	// Use this for initialization
	void Start () {
		
	}


    void Update() {
        if (Input.GetKeyUp(KeyCode.Alpha1)) {
            SpawnAndMoveGuest();            
            //SpawnGuest(new Vector3(4, 0.98f, 4), new Vector3(0, 0, 0));
        }
        else if (Input.GetKeyUp(KeyCode.Alpha2))
        {
            SpawnAndMoveGuest(2);
        }
        else if (Input.GetKeyUp(KeyCode.Alpha3)) {
            SpawnAndMoveGuest(3);
        }
    }

    public void SpawnAndMoveGuest(int numberOfGuests)
    {
        //random pick door to spawn at
        int spawnIndex = Random.Range(0, spawnPoints.Length);
        int[] indexes = new int[spawnPoints[spawnIndex].transform.childCount];

        for (int i = 0; i < numberOfGuests; i++)
        {
           
            //randomly pick "greet point" from children of spawn point object
            int greetIndex = Random.Range(0, spawnPoints[spawnIndex].transform.childCount);

            SpawnGuest(spawnPoints[spawnIndex].transform.position, spawnPoints[spawnIndex].transform.GetChild(greetIndex).transform.position);
        }
    }

    public void SpawnAndMoveGuest() {
        //random pick door to spawn at
        int spawnIndex = Random.Range(0, spawnPoints.Length);
        //randomly pick "greet point" from children of spawn point object
        int greetIndex = Random.Range(0, spawnPoints[spawnIndex].transform.childCount);

        SpawnGuest(spawnPoints[spawnIndex].transform.position, spawnPoints[spawnIndex].transform.GetChild(greetIndex).transform.position);
    }

    void SpawnGuest(Vector3 startPos, Vector3 endPos)
    {

        GameObject guest = Instantiate(guestPrefab, startPos, Quaternion.identity);
        guestAgent = guest.GetComponent<NavMeshAgent>();
        guestAgent.SetDestination(endPos);
        guestAgent.transform.parent = spawnParent.transform;
    }

    void SpawnGuests() {

    }
}
