using System.Collections;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using UnityEngine;

public class Bartek : MonoBehaviour
{
    public CutsceneController cutscene;
    public Rigidbody2D tilemapRb;
    [SerializeField] private Collider2D trigger;

    [SerializeField]
    private GameObject matrix;

    [SerializeField]
    private List<Transform> TopPatrolPoints;

    [SerializeField]
    private List<Transform> MiddlePatrolPoints;

    [SerializeField]
    private List<Transform> BottomPatrolPoints;

    [SerializeField]
    private GameObject[] TopSpawners;

    [SerializeField]
    private GameObject[] MiddleSpawners;

    [SerializeField]
    private GameObject[] BottomSpawners;



    int[] howManyMatrixesToSpawn = { 2, 4, 6 };

    [HideInInspector]
    public int whereToSpawnMatrixes=-1;
    /*
     0 = Top
     1 = Middle
     2 = Bottom
     */

    void Start()
    {
        whereToSpawnMatrixes = 0;
        StartCoroutine(SpawnMatrixes());
    }

    void SpawnEnemies()
    {
        
    }

    IEnumerator SpawnMatrixes()
    {
        while (true)
        {
            yield return new WaitForSeconds(3);

            if (whereToSpawnMatrixes != -1)
            {
                //Debug.LogWarning();
                bool[] spawned = new bool[howManyMatrixesToSpawn[whereToSpawnMatrixes]];
                int howManySpawnPoints;

                if (whereToSpawnMatrixes == 0)
                    howManySpawnPoints = TopSpawners.Length;
                else if (whereToSpawnMatrixes == 1)
                    howManySpawnPoints = MiddleSpawners.Length;
                else
                    howManySpawnPoints = BottomSpawners.Length;


                for (int i = 0; i < howManyMatrixesToSpawn[whereToSpawnMatrixes]; i++)
                {
                    int spawnIndex = Random.Range(0, howManyMatrixesToSpawn[whereToSpawnMatrixes] - 1);
                    if (spawned[spawnIndex] == true)
                    {
                        while (spawned[spawnIndex] == true)
                        {
                            spawnIndex++;
                            spawnIndex %= howManySpawnPoints;
                        }
                    }

                    spawned[spawnIndex] = true;


                    if (whereToSpawnMatrixes == 0)
                    {
                        matrix.GetComponent<Enemy>().patrolPoints = TopPatrolPoints;

                        try
                        {
                            matrix.GetComponent<Enemy>().abc = this.TopPatrolPoints[0]; ;
                        } catch(System.Exception e) {
                            Debug.LogWarning(e.Message);
                        }

                        Debug.Log(this.TopPatrolPoints[0].position.x); // This line generates error
                        matrix.transform.position = TopSpawners[spawnIndex].transform.position;
                        

                    }
                    else if (whereToSpawnMatrixes == 1)
                    {
                        matrix.transform.position = MiddleSpawners[spawnIndex].transform.position;
                        matrix.GetComponent<Enemy>().patrolPoints = MiddlePatrolPoints;
                    }
                    else
                    {
                        matrix.transform.position = BottomSpawners[spawnIndex].transform.position;
                        matrix.GetComponent<Enemy>().patrolPoints = BottomPatrolPoints;
                    }
                    GameObject spawnedMatrix = Instantiate(matrix);
                }
            }
        }
    }

    // Update is called once per frame
    public void FallTilemap()
    {
        tilemapRb.bodyType = RigidbodyType2D.Dynamic;
        Invoke("DestroyTilemap", 3.5f);
    }
    void DestroyTilemap()
    {
        Destroy(tilemapRb.gameObject);
        Destroy(this);
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            cutscene.Begin();
            Destroy(trigger);
        }
    }
}