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

public class DisplayTile : MonoBehaviour
{
    private Image road;
    public Transform roadPrefab;

    public void SetInfo(Tile tile)
    {
        road = transform.Find("Road").GetComponent<Image>();
        road.sprite = Resources.Load<Sprite>(string.Format("Tiles/Road/road_tile_{0,2:D2}",tile.format));

        var image = transform.GetComponent<Image>();
        image.sprite = Resources.Load<Sprite>(string.Format("Tiles/{0}_tile_{1,2:D2}", tile.style, tile.styleModel));

        if (Constants.DEBUG_MODE)
        {
            var textGo = new GameObject("DebugText");
            textGo.transform.SetParent(transform);
            var text = textGo.AddComponent<Text>();
            text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
            text.fontSize = 28;
            text.color = new Color32(0,0,0,255);

            text.text = tile.code;
            text.transform.localRotation = Quaternion.Euler(0, 0, -tile.rotation);

            var tileSize = Constants.TILE_SIZE;
            var roadSize = Constants.ROAD_SIZE;

            foreach (var road in tile.tileFormat.roads)
            {
                Transform obj = Instantiate(roadPrefab, Vector3.zero, Quaternion.identity);
                RectTransform rect = obj.GetComponent<RectTransform>();
                obj.SetParent(transform);
                obj.name = "Road "+road.point1+", "+road.point2;

                var sizeX = road.point1.x - road.point2.x;
                var sizeY = road.point1.y - road.point2.y;
                obj.localPosition = new Vector2(tileSize * (road.point1.x - sizeX / 2 - 0.5f), tileSize * (road.point1.y - sizeY / 2 - 0.5f));
                obj.localScale = new Vector3(1, 1, 1);
                rect.sizeDelta = new Vector2(Mathf.Max(Mathf.Abs(sizeX) * tileSize, roadSize), Mathf.Max(Mathf.Abs(sizeY) * tileSize, roadSize));
            }
        }

        transform.localRotation = Quaternion.Euler(0, 0, tile.rotation);
    }
}
