using UnityEngine;
using mgaia.Utils;

namespace mgaia.tk2d
{
    /// <summary>
    /// Extensões para o tk2dTileMap
    /// </summary>
    public static class Tk2dTileMapExtensions
    {
        /// <summary>
        /// Calcula os limites do tilemap em uma nova posição
        /// </summary>
        /// <owner>Victor</owner>
        public static Vector3 GetSize(this tk2dTileMap tileMap)
        {
            return tileMap.renderData.GetSize(true, true);
        }

        /// <summary>
        /// Calcula os limites do tilemap na posição atual
        /// </summary>
        /// <owner>Victor</owner>
        public static Bounds? GetBounds(this tk2dTileMap tileMap)
        {
            Vector3? size = tileMap.GetSize();

            if (size.HasValue)
            {
                Vector3 tileSize = tileMap.GetScaledTileSize();

                Vector3 bottomLeft = new Vector3(tileMap.transform.position.x - tileSize.x / 2, tileMap.transform.position.y - tileSize.y / 2, tileMap.transform.position.z);

                return new Bounds(bottomLeft + size.Value / 2, size.Value);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Retorna o tilesize já considerando o scale aplicado no tileMap
        /// </summary>
        public static Vector3 GetScaledTileSize(this tk2dTileMap tileMap)
        {
            tk2dTileMapData data = tileMap.data;

            Vector3 tileSize = new Vector3(data.tileSize.x * tileMap.transform.lossyScale.x, data.tileSize.y * tileMap.transform.lossyScale.y, data.tileSize.z * tileMap.transform.lossyScale.z);

            return tileSize;
        }
    }
}