package main;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import ea.Bild;
import ea.Knoten;

public class Tile extends Knoten {

	public static final int GRASS = 0;
	public static final int WALL_TOP = 1;
	public static final int WALL_BOTTOM = 2;

	private Bild img;

	public Tile(int id) {
		try {
			BufferedImage buff = ImageIO.read(Tile.class.getResourceAsStream(getPathFromId(id)));
			if (id == GRASS) {
				buff = buff.getSubimage(16 * (int) (Math.random() * 8), 0, 16, 16);
			}
			BufferedImage after = new BufferedImage(buff.getWidth() * World.SCALE, buff.getHeight() * World.SCALE, BufferedImage.TYPE_INT_ARGB);
			AffineTransform at = new AffineTransform();
			at.scale(World.SCALE, World.SCALE);
			AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
			after = scaleOp.filter(buff, after);
			int x = 0;
			int y = 0;
			img = new Bild(x, y, after);
		} catch (IOException e) {
			e.printStackTrace();
		}
		add(img);
	}

	private String getPathFromId(int id) {
		switch (id) {
		case GRASS:
			return "/res/images/tiles/grass.png";
		case WALL_TOP:
			return "/res/images/tiles/wall_top.png";
		case WALL_BOTTOM:
			return "/res/images/tiles/wall_bottom.png";
		}
		return null;
	}

	public static int getSize() {
		return 16 * World.SCALE;
	}
}
