using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.TerrainAi
{
    public class AiControllers : MonoBehaviour
    {

        public GameObject[] Vehicles;

        public static AiControllers Instance;

        void Awake()
        {
            Instance = this;
        }

        public GameObject GetCar(Vector3 position, List<Vector3> aiPoints, int i)
        {
            var id = Random.Range(0, Vehicles.Length);
            var go = GameObject.Instantiate(Vehicles[id], position, Quaternion.identity);
            return go;
        }

        public WaypointNode[] CreateWaypoints(List<Vector3> aiPoints)
        {
            var waypoints = new GameObject("Waypoints");
            waypoints.transform.position = Vector3.zero;

            var waypointScripts = new List<WaypointNode>();
            var wayPointId = 0;

            for (var x = 0; x < aiPoints.Count; x += 5)
            {
                var wp = new GameObject("Waypoint " + wayPointId);
                var box = wp.AddComponent<BoxCollider>();
                box.isTrigger = true;
                wp.tag = "Waypoint";

                wp.transform.position = aiPoints[x];
                var script = wp.AddComponent<WaypointNode>();
                script.WaypointNumber = wayPointId;
                wp.transform.parent = waypoints.transform;
                waypointScripts.Add(script);
                wayPointId++;
            }

            return waypointScripts.ToArray();
        }
    }
}
