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

public class LevelGeneration : MonoBehaviour
{
    //bos logic
    public Transform[] spawns;
    public GameObject bossRoom;

    public Transform[] startingPositions;
    public GameObject[] rooms; // index 0 = LR, index 1 = LRB, index 2 = LRT, indes 3 = ALL (LRTB)
    public LayerMask room;
    private int direction;
    private int downcounter;
    public bool stopGen = false;
    public float moveAmount;

    private float timebtwrooms;
    public float starttimebtwrooms = 0.25f;
    public float minX;
    public float maxX;
    public float minY;

    // Start is called before the first frame update
    void Start()
    {
        int randStartpos = Random.Range(0, startingPositions.Length);
        transform.position = startingPositions[randStartpos].position;
        Instantiate(rooms[0], transform.position, Quaternion.identity);
        direction = Random.Range(1, 6);
    }

    private void Move()
    {
        Collider2D roomDetection = Physics2D.OverlapCircle(transform.position, 1f, room);
        if (direction == 1 || direction == 2) // move right
        {
            if (transform.position.x <= maxX)
            {
                downcounter = 0;
                Vector2 newPos = new Vector2(transform.position.x + moveAmount, transform.position.y);
                transform.position = newPos;

                int rand = Random.Range(0, rooms.Length);
                Instantiate(rooms[rand], transform.position, Quaternion.identity);

                direction = Random.Range(1, 6);


                if (direction == 3)
                {
                    direction = 2;
                }
                if (direction == 4)
                {
                    direction = 5;
                }
            }
            else
            {
                direction = 5;
            }
        }
        else if (direction == 3 || direction == 4) // move left
        {
            if (transform.position.x >= minX)
            {
                downcounter = 0;
                Vector2 newPos = new Vector2(transform.position.x - moveAmount, transform.position.y);
                transform.position = newPos;


                int rand = Random.Range(0, rooms.Length);
                Instantiate(rooms[rand], transform.position, Quaternion.identity);


                direction = Random.Range(3, 6);
            }
            else
            {
                direction = 5;
            }

        }
        else if (direction == 5) // move down
        {
            downcounter++;
            if (transform.position.y >= minY)
            {

                if (roomDetection != null)
                {
                    if (roomDetection.GetComponent<RoomType>().type != 1 && roomDetection.GetComponent<RoomType>().type != 3)
                    {
                        if (downcounter >= 2)
                        {
                            roomDetection.GetComponent<RoomType>().RoomDestruction();
                            Instantiate(rooms[3], transform.position, Quaternion.identity);
                        }
                        else
                        {
                            roomDetection.GetComponent<RoomType>().RoomDestruction();

                            int randbottomRoom = Random.Range(1, 4);
                            if (randbottomRoom == 2)
                            {
                                randbottomRoom = 1;
                            }
                            Instantiate(rooms[randbottomRoom], transform.position, Quaternion.identity);
                        }
                    }

                    Vector2 newPos = new Vector2(transform.position.x, transform.position.y - moveAmount); ;
                    transform.position = newPos;

                    int rand = Random.Range(2, 4);
                    Instantiate(rooms[rand], transform.position, Quaternion.identity);

                }



                direction = Random.Range(1, 6);
            }

            else
            {
                stopGen = true;
                Debug.Log(stopGen);
            }
            // the problem is over here!
            if (roomDetection != null && stopGen)
            {
                roomDetection.GetComponent<RoomType>().RoomDestruction();
                int rand = Random.Range(12, 16);
                Instantiate(bossRoom, spawns[rand].position, Quaternion.identity);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (timebtwrooms <= 0 && stopGen == false)
        {
            Move();
            timebtwrooms = starttimebtwrooms;
        }
        else
        {
            timebtwrooms -= Time.deltaTime;
        }
    }
}