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 = 1;

	private static final int SCALE = 6;

	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() * SCALE, buff.getHeight() * SCALE, BufferedImage.TYPE_INT_ARGB);
			AffineTransform at = new AffineTransform();
			at.scale(SCALE, SCALE);
			AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
			after = scaleOp.filter(buff, after);
			int x = 0;
			int y = 0;
			if (id == WALL) {
				y = -after.getHeight() + 16 * SCALE;
			}
			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:
			return "/res/images/tiles/wall.png";
		}
		return null;
	}

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