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

public class DisplayMap : MonoBehaviour
{
    public Transform tilePrefab;
    public Transform monumentPrefab;

    private MapData mapData;
    private Transform canvasMap;

    public void SetData(MapData mapData)
    {
        this.mapData = mapData;

        canvasMap = GameObject.Find("Canvas/Map").transform;

        var tileSize = Constants.TILE_SIZE;
        var tiles = mapData.tiles;
        var yLength = tiles.GetLength(1);
        for (int i = 0; i < tiles.GetLength(0); i++)
            for (int j = 0; j < yLength; j++)
            {
                Transform obj = Instantiate(tilePrefab, Vector3.zero, Quaternion.identity);
                obj.name = "Tile "+i+"x"+j+": "+tiles[i,j].code;
                obj.SetParent(canvasMap);
                obj.GetComponent<DisplayTile>().SetInfo(tiles[i, j]);

                //y goes up on Unity
                obj.localPosition = new Vector2(tileSize * i, tileSize * (yLength - j - 1));
                obj.localScale = new Vector3(1, 1, 1);
            }

        CreateEnd(mapData.end);

        byte signsAlpha = 0;
        if (Constants.DEBUG_MODE)
        {
            CreateMonument(mapData.start, new Color32(255,0,0,150));
            CreateMonument(mapData.end, new Color32(0,0,255,150));
            signsAlpha = 150;
        }

        foreach (var sign in mapData.signs)
            CreateMonument(sign, new Color32(0,255,0,signsAlpha));
    }

    private void CreateEnd(Monument monument)
    {
        var tileSize = Constants.TILE_SIZE;
        var yLength = mapData.tiles.GetLength(1);
        Transform trans = Instantiate(monumentPrefab, Vector3.zero, Quaternion.identity);

        var image = trans.GetComponent<Image>();
        image.sprite = Resources.Load<Sprite>(monument.image);

        trans.SetParent(canvasMap);
        trans.name = monument.image+" "+monument.tileIndex[0]+"x"+monument.tileIndex[1]+": "+monument.positionOnTile;
        trans.localPosition = new Vector2(tileSize * (monument.tileIndex[0] + monument.positionOnTile.x - 0.5f), tileSize * (yLength - monument.tileIndex[1] - 1 + monument.positionOnTile.y - 0.5f));
    }

    private void CreateMonument(Monument monument, Color32 color)
    {
        var tileSize = Constants.TILE_SIZE;
        var yLength = mapData.tiles.GetLength(1);
        Transform trans = Instantiate(monumentPrefab, Vector3.zero, Quaternion.identity);
        trans.GetComponent<SignScript>().SetData(monument);
        trans.SetParent(canvasMap);
        trans.name = monument.image+" "+monument.tileIndex[0]+"x"+monument.tileIndex[1]+": "+monument.positionOnTile;

        trans.localPosition = new Vector2(tileSize * (monument.tileIndex[0] + monument.positionOnTile.x - 0.5f), tileSize * (yLength - monument.tileIndex[1] - 1 + monument.positionOnTile.y - 0.5f));
        trans.localScale = new Vector3(1, 1, 1);
        trans.GetComponent<Image>().color = color;
    }
}