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

public class Map : MonoBehaviour
{
    public Image city;
    public Image plains;
    public Image forest;
    public Image stone;
    public Image spawn;
    public Image[] mapTiles;

    public void prepare(Dictionary<Vector3, GameObject> tiles, GameObject currentTile)
    {
        destroyMap();
        generateMap(tiles, currentTile);
    }

    private void destroyMap()
    {
        for (int i = 0; i < mapTiles.Length; i++)
        {
            mapTiles[i].color = Color.white;
        }
    }

    private void generateMap(Dictionary<Vector3, GameObject> tiles, GameObject currentTile)
    {
        Vector3 current = currentTile.GetComponent<Tile>().getPosition();

        Vector3 position;
        Color color;

        int posX = (int)current.x - 2;
        int posZ = (int)current.z + 2;
        for (int i = 0; i < mapTiles.Length; i++)
        {
            if (i == 5 || i == 10 || i == 15 || i == 20)
            {
                posX = (int)current.x - 2;
                posZ--;
            }

            position = new Vector3(posX, 0, posZ);

            if (tiles.ContainsKey(position))
            {
                switch (tiles[position].name.Split('_')[0])
                {
                    case "StoneTile":
                        color = stone.color;
                        break;
                    case "CityTile":
                        color = city.color;
                        break;
                    case "Tile":
                        color = plains.color;
                        break;
                    case "TreeTile":
                        color = forest.color;
                        break;
                    default:
                        color = spawn.color;
                        break;
                }
                mapTiles[i].color = color;
            }
            posX++;
        }
    }
}
