import { Bounds, boundsPool } from "./../../../physics/bounds.ts";
import TMXLayer from "./../TMXLayer.js";

/**
 * The map renderer base class
 * @category Tilemap
 */
export default class TMXRenderer {
	/**
	 * @param {number} cols - width of the tilemap in tiles
	 * @param {number} rows - height of the tilemap in tiles
	 * @param {number} tilewidth - width of each tile in pixels
	 * @param {number} tileheight - height of each tile in pixels
	 */
	constructor(cols, rows, tilewidth, tileheight) {
		this.cols = cols;
		this.rows = rows;
		this.tilewidth = tilewidth;
		this.tileheight = tileheight;
		this.bounds = new Bounds();
	}

	/**
	 * return true if the renderer can render the specified map or layer
	 * @param {TMXTileMap|TMXLayer} component - TMX Map or Layer
	 * @returns {boolean}
	 */
	canRender(component) {
		return (
			/*
            // layers can have different size within
            // the same maps, so commenting these two lines
            (this.cols === component.cols) &&
            (this.rows === component.rows) &&
            */
			this.tilewidth === component.tilewidth &&
			this.tileheight === component.tileheight
		);
	}

	/**
	 * return the bounding rect for this map renderer
	 * @param {TMXLayer} [layer] - calculate the bounding rect for a specific layer (will return a new bounds object)
	 * @returns {Bounds}
	 */
	getBounds(layer) {
		const bounds = layer instanceof TMXLayer ? boundsPool.get() : this.bounds;
		bounds.setMinMax(
			0,
			0,
			this.cols * this.tilewidth,
			this.rows * this.tileheight,
		);
		return bounds;
	}

	/**
	 * return the tile position corresponding to the specified pixel
	 * @param {number} x - X coordinate
	 * @param {number} y - Y coordinate
	 * @param {Vector2d} [v] - an optional vector object where to put the return values
	 * @returns {Vector2d}
	 */
	pixelToTileCoords(x, y, v) {
		return v;
	}

	/**
	 * return the pixel position corresponding of the specified tile
	 * @param {number} col - tile horizontal position
	 * @param {number} row - tile vertical position
	 * @param {Vector2d} [v] - an optional vector object where to put the return values
	 * @returns {Vector2d}
	 */
	tileToPixelCoords(col, row, v) {
		return v;
	}

	/**
	 * fix the position of Objects to match
	 * the way Tiled places them
	 * @param {object} obj - a TMX Object with gid, x, y, height properties
	 */
	adjustPosition(obj) {
		// only adjust position if obj.gid is defined
		if (typeof obj.gid === "number") {
			// Tiled objects origin point is "bottom-left" in Tiled,
			// "top-left" in melonJS
			obj.y -= obj.height;
		}
	}

	/**
	 * draw the given tile at the specified layer
	 * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer object
	 * @param {number} x - X coordinate where to draw the tile
	 * @param {number} y - Y coordinate where to draw the tile
	 * @param {Tile} tile - the tile object to draw
	 */
	drawTile() {}

	/**
	 * draw the given TMX Layer for the given area
	 * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer object
	 * @param {TMXLayer} layer - a TMX Layer object
	 * @param {Rect} rect - the area of the layer to draw
	 */
	drawTileLayer() {}
}
