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

public class LifeView : MonoBehaviour {
    
    public int maxLifeUnits = 10;
    public float distanceFromCenter = 0.4f;
    public GameObject dumbPrefab;
    public GameObject feralPrefab;
    public GameObject primitivePrefab;
    public GameObject advancedPrefab;

    public GameObject hutPrefab;
    public GameObject cityPrefab;

    Dictionary<Tile.Intelligence, GameObject> prefabMap;
    Dictionary<Tile.Infrastructure, GameObject> infraMap;

    int _currentlyDisplayedCount = 0;
    Tile.Intelligence _currentIntelligence = Tile.Intelligence.Primordial;
    Tile tile {  get { if (_tile == null) { _tile = GetComponentInParent<Tile>(); } return _tile; } }
    Tile _tile;
    Tile.Infrastructure _currentInfrastructure = Tile.Infrastructure.None;

    GameObject infrastructureGO;

	void Start ()
    {
        prefabMap = new Dictionary<Tile.Intelligence, GameObject>()
        {
            { Tile.Intelligence.Primordial, dumbPrefab },
            { Tile.Intelligence.Feral, feralPrefab },
            { Tile.Intelligence.Primitive, primitivePrefab },
            { Tile.Intelligence.Advanced, advancedPrefab },
        };
        infraMap = new Dictionary<Tile.Infrastructure, GameObject>()
        {
            {Tile.Infrastructure.None, null },
            {Tile.Infrastructure.Farm, hutPrefab },
            {Tile.Infrastructure.City, cityPrefab }
        };

        if (tile.abundance > 0) UpdateLife();
        if (!tile.isPlayerTile) this.enabled = false;
    }


    void Update()
    {
        if (tile.abundance > 0 || _currentlyDisplayedCount > 0)
            UpdateLife();
        if (tile.infrastructure != Tile.Infrastructure.None || _currentInfrastructure != Tile.Infrastructure.None)
            UpdateInfrastructure();
    }
    void UpdateLife()
    {
        if (_currentIntelligence != tile.intelligence)
        {
            foreach (Transform child in transform)
            {
                if (child.gameObject == infrastructureGO) continue;
                Destroy(child.gameObject);
            }
            for (int i=0; i<_currentlyDisplayedCount; i++)
            {
                var newLife = Instantiate(prefabMap[tile.intelligence], transform);
                newLife.transform.localPosition = new Vector3(Random.Range(-distanceFromCenter, distanceFromCenter), 0, Random.Range(-distanceFromCenter, distanceFromCenter));
                newLife.transform.Rotate(Vector3.forward, Random.Range(0, 360));
            }
            _currentIntelligence = tile.intelligence;
        }

        int abundance = Mathf.CeilToInt(tile.abundance * maxLifeUnits);
		if (abundance > _currentlyDisplayedCount)
        {
            for (int i = _currentlyDisplayedCount; i < abundance; i++)
            {
                var newLife = Instantiate(prefabMap[_currentIntelligence], transform);
                newLife.transform.localPosition = new Vector3(Random.Range(-distanceFromCenter, distanceFromCenter), 0, Random.Range(-distanceFromCenter, distanceFromCenter));
                newLife.transform.Rotate(Vector3.forward, Random.Range(0, 360));
            }
            _currentlyDisplayedCount = abundance;
        }
        if (abundance < _currentlyDisplayedCount)
        {
            for (int i = _currentlyDisplayedCount; i > abundance; i--)
            {
                var life = transform.GetChild(0);
                if (life.gameObject == infrastructureGO) { i++; continue; }
                Destroy(life.gameObject); // MYAAHAHHAHAA
            }
            _currentlyDisplayedCount = abundance;
        }
	}

    void UpdateInfrastructure()
    {
        if (tile.infrastructure == _currentInfrastructure)
            return;
        if (infrastructureGO != null)
            Destroy(infrastructureGO);
        infrastructureGO = Instantiate(infraMap[tile.infrastructure]);
        infrastructureGO.transform.SetParent(transform);
        infrastructureGO.transform.localPosition = Vector3.zero;
        infrastructureGO.transform.localRotation = Quaternion.Euler(-90, 0, 0) ;
        _currentInfrastructure = tile.infrastructure;
    }
}
