using System.Collections.Generic;
using UnityEngine;

public class CarManagerScript : MonoBehaviour
{
    [SerializeField] private int _MaxSpeed;

    private int _Speed;

    [SerializeField] private List<GameObject> _CarPrefs;

    [SerializeField] private GameObject[] _Points = new GameObject[2];

    [SerializeField] bool _LeftToRight;

    private void Start()
    {
        List<int> speedVar = new List<int>();

        speedVar.Add(_MaxSpeed);

        //for(int i = _MaxSpeed/2; i <= _MaxSpeed; i++)
        //{
        //    speedVar.Add(i);
        //}

        _Speed = GetRandomObject<int>(speedVar);

        //Debug.Log($"Speed - {_Speed}");

        //_LeftToRight = GetRandomObject<bool>(new List<bool> { true, false });

        float time = GetRandomObject<float>(new List<float> { 0f, 1f, 2f, 3f, 4f, 5f });

        //Debug.Log(time);

        Invoke("StartMove", time);

        //Debug.Log("was spawned");
    }

    private void FixedUpdate()
    {
        if (_Car != null)
        {
            if (_LeftToRight)
            {
                _Car.transform.Translate(Vector3.forward * Time.deltaTime * _Speed);
            }
            else if (!_LeftToRight)
            {
                _Car.transform.Translate(Vector3.forward * Time.deltaTime * _Speed);
            }

            if (_Car.transform.position.x >= _Points[1].transform.position.x && _LeftToRight)
            {
                DeleteCar();
            }
            else if (_Car.transform.position.x <= _Points[0].transform.position.x && !_LeftToRight)
            {
                DeleteCar();
            }
        }
    }


    private GameObject _Car;
    private void StartMove()
    {
        Vector3 pos;
        Quaternion rot;

        // разница до колеса

        if (_LeftToRight)
        {
            pos = new Vector3(_Points[0].transform.position.x, _Points[0].transform.position.y, _Points[0].transform.position.z);
            rot = Quaternion.Euler(0, 90, 0);

            _Car = Instantiate(GetRandomObject<GameObject>(_CarPrefs), pos, rot);
        }
        else if (!_LeftToRight)
        {
            pos = new Vector3(_Points[1].transform.position.x, _Points[1].transform.position.y, _Points[1].transform.position.z);
            rot = Quaternion.Euler(0, -90, 0);

            _Car = Instantiate(GetRandomObject<GameObject>(_CarPrefs), pos, rot);
        }

        float high = _Car.GetComponent<CarControllerScript>().High;

        _Car.transform.position = pos = new Vector3(_Car.transform.position.x, _Car.transform.position.y + high, _Car.transform.position.z);

        _Car.transform.SetParent(gameObject.transform);

    }

    public void DeleteCar()
    {
        Destroy(_Car);
        StartMove();
    }

    T GetRandomObject<T>(List<T> list)
    {
        // проверка на пустоту списка
        if (list == null || list.Count == 0)
        {
            return default;
        }

        // Генерация случайного индекса
        int randomIndex = UnityEngine.Random.Range(0, list.Count);
        return list[randomIndex];
    }
}
