using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tile : MonoBehaviour
{
    Vector3 position;
    float borderNorth;
    float borderEast;
    float borderSouth;
    float borderWest;
    System.Random rand = new System.Random();
    string tilename;
    GameObject contentGenerator;

    List<GameObject> aliveEnemies = new List<GameObject>();
    List<GameObject> deadEnemies = new List<GameObject>();

    public void generateTile(Vector3 pos, string name)
    {
        tilename = name;
        contentGenerator = GameObject.Find("ContentGenerator");
        setPosition(pos);
        setBorders();
        generateContent();  
    }

    public void setBorders()
    {
        borderNorth = position.z * 100 + 50;
        borderEast = position.x * 100 + 50;
        borderSouth = position.z * 100 - 50;
        borderWest = position.x * 100 - 50;
    }

    public void generateContent()
    {
        if (!tilename.ToLower().Equals("City"))
        {
            foreach (Vector3 position in getSpawnLocations())
            {
                spawnObject(position);
            }
        }
    }

    public List<Vector3> getSpawnLocations()
    {
        List<Vector3> list = new List<Vector3>();
        int xChange = 0;
        int zChange = 0;
        for (float i = borderNorth - 10; i >= borderSouth + 10; i = i - 10)
        {
            for (float j = borderWest + 10; j <= borderEast - 10; j = j + 10)
            {
                xChange = rand.Next(-4, +4);
                zChange = rand.Next(-4, +4);
                list.Add(new Vector3(j + xChange, 5, i + zChange));
            }
        }
        return list;
    }

    public void spawnObject(Vector3 position)
    {
        int chance = rand.Next(1, 101);
        if (chance >= 50)
        {
            GameObject content = contentGenerator.GetComponent<ContentGenerator>().generateContent(tilename);
            if (content != null)
            {
                GameObject obj = Instantiate(content, position, Quaternion.identity);
                obj.transform.parent = gameObject.transform;
                if (obj.tag.Contains("Enemy"))
                {
                    aliveEnemies.Add(obj);
                }
            }
        }
    }

    public void setPosition(Vector3 position)
    {
        this.position = position;
    }

    public Vector3 getPosition()
    {
        return position;
    }

    public bool leftTile(float playerX, float playerZ)
    {
        bool result = false;
        if (playerX >= borderEast + 10 || playerX <= borderWest - 10 || playerZ >= borderNorth + 10 || playerZ <= borderSouth - 10)
        {
            changeRenderer();
            result = true;
        }
        return result;
    }

    public void changeRenderer()
    {
        foreach (Rigidbody rigid in gameObject.GetComponentsInChildren<Rigidbody>())
        {
            if (!deadEnemies.Contains(rigid.gameObject))
            {
                rigid.useGravity = !rigid.useGravity;
            }
        }

        foreach (Renderer rend in gameObject.GetComponentsInChildren<Renderer>())
        {
            if (!deadEnemies.Contains(rend.gameObject))
            {
                rend.enabled = !rend.enabled;
            }
        }

        foreach (Collider col in gameObject.GetComponentsInChildren<Collider>())
        {
            if (!deadEnemies.Contains(col.gameObject))
            {
                col.enabled = !col.enabled;
            }
        }
    }

    public void resetSpawn()
    {
        foreach (Rigidbody rigid in gameObject.GetComponentsInChildren<Rigidbody>())
        {
            rigid.useGravity = true;
        }

        foreach (Renderer rend in gameObject.GetComponentsInChildren<Renderer>())
        {
            rend.enabled = true;
        }

        foreach (Collider col in gameObject.GetComponentsInChildren<Collider>())
        {
            col.enabled = true;
        }
    }

    public bool enteredTile(float playerX, float playerZ)
    {
        bool result = false;
        if (playerX <= borderEast && playerX >= borderWest && playerZ <= borderNorth && playerZ >= borderSouth)
        {
            result = true;
        }
        return result;
    }

    public Vector3 needConnectedTile(float playerX, float playerZ)
    {
        Vector3 result = new Vector3(position.x, 1, position.z);
        if (playerX >= borderEast - 10)
        {
            result.x = result.x + 1;
            result.y = 0;
        }
        else if(playerX <= borderWest + 10)
        {
            result.x = result.x - 1;
            result.y = 0;
        }

        if (playerZ >= borderNorth - 10)
        {
            result.z = result.z + 1;
            result.y = 0;
        }
        else if(playerZ <= borderSouth + 10) 
        {
            result.z = result.z - 1;
            result.y = 0;
        }
        return result;
    }

    public bool removeConnectedTiles(float playerX, float playerZ)
    {
        bool result = false;
        if (playerX <= borderEast - 10 && playerX >= borderWest + 10 && playerZ <= borderNorth - 10 && playerZ >= borderSouth + 10)
        {
            result = true;
        }
        return result;
    }

    public void enemyKilled(GameObject enemy)
    {
        deadEnemies.Add(enemy);
        enemy.GetComponent<Rigidbody>().useGravity = false;
        enemy.GetComponent<Renderer>().enabled = false;
        enemy.GetComponent<Collider>().enabled = false;
        aliveEnemies.Remove(enemy);
    }
}
