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

public class CityBuilder : MonoBehaviour
{
    public float minimumCitySpacing;
    public GameObject bottomLeftCorner;
    public GameObject topRightCorner;
    public List<City> cities;

    string[] names = {
        "Avaria",
        "Xygen",
        "Red Ward",
        "Oatbluff",
        "Raggen",
        "Bonefjord",
        "CoalSummit",
        "Greendale",
        "Sanctuary",
        "Stephens",
        "Owlpost",
        "Breywich",
        "Boreala",
        "Bullsgate",
        "DryFarm",
        "LongCreek",
        "Stormworth",
        "Sourdale",
        "AngelSpire",
        "Thurnprine",
        "SteamTown",
        "Cog Ridge",
        "Lambrock",
        "Bjarnhof",
        "Gudplan",
        "Harvest",
        "Chagnane",
        "Thalcone",
        "Adelina",
        "Volma",
        "EllsBridge"

    };

    // Start is called before the first frame update
    void Start()
    {
        bottomLeftCorner.SetActive(false);
        topRightCorner.SetActive(false);

        cities = new List<City>();
        foreach (var cityName in names)
        {
            BuildCity(cityName);
        }
        GameManager.Instance().AssignCities(cities);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public City BuildCity(string cityName)
    {
        var city = Utils.PrefabbedComponent<City>("_prefabs/City");
        city.SetName(cityName);

        bool invalidPosition = true;
        while (invalidPosition)
        {
            invalidPosition = false;
            var x = Random.Range(BottomLeft().x, TopRight().x);
            var y = Random.Range(BottomLeft().y, TopRight().y);
            city.transform.position = new Vector3(x, y, 0);
            foreach (var otherCity in cities)
            {
                if (otherCity.DistanceTo(city) < minimumCitySpacing)
                {
                    invalidPosition = true;
                }
            }
        }

        city.gameObject.SetActive(true);
        cities.Add(city);
        city.transform.SetParent(transform);
        return city;
    }

    Vector3 BottomLeft()
    {
        return bottomLeftCorner.transform.position;
    }

    Vector3 TopRight()
    {
        return topRightCorner.transform.position;
    }
}
