package de.heatup.objects;

import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import de.heatup.cfg.Config;


public class ImageLoader {

	/*
	 * TODO: Better solution like try catch if image not found
	 */
	/*
	 * #: Path
	 * -: Wall
	 * 0 - 9: Hardware
	 */
	public BufferedImage getBufferedImage(char key) {
		String imageFileName;
		switch (key) {
		case Config.wayChar:
			imageFileName="de/heatup/resources/images/25x25_black.png";
			break;
		case Config.wallChar:
			imageFileName="de/heatup/resources/images/25x25_wall_orange_2.png";
			break;
		case 'b':
			imageFileName="de/heatup/resources/images/25x25_powerup.png";
			break;
		case 'u':
			imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png";
			break;
		case 'd':
			imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png";
			break;
		case 'l':
			imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png";
			break;
		case 'r':
			imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png";
			break;
		case 'p':
			imageFileName="de/heatup/resources/images/little_blue_guy_verysmall.png";
			//imageFileName="de/heatup/resources/images/red_bug_25x25.png";
			break;		
		case 'q':
			//imageFileName="de/heatup/resources/images/little_blue_guy_verysmall.png";
			imageFileName="de/heatup/resources/images/little_bug_25x25_1.png";
			break;
		case 's':
			//imageFileName="de/heatup/resources/images/little_blue_guy_verysmall.png";
			imageFileName="de/heatup/resources/images/little_bug_25x25_2.png";
			break;
		case 't':
			//imageFileName="de/heatup/resources/images/little_blue_guy_verysmall.png";
			imageFileName="de/heatup/resources/images/little_bug_25x25_3.png";
			break;
		default:
			imageFileName="de/heatup/resources/images/dummy_image.png";
			break;
		}
		if (isHardware(key)) {
			Random rand = new Random();
			switch (rand.nextInt(2)) {
			case 0:
				imageFileName="de/heatup/resources/images/hardware/rectangular_chip.jpg";
				break;
			case 1:
				imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
				break;
			default:
				break;
			}
		}
		BufferedImage loadedBufferedImage = null;
		//System.out.println(imageFileName);
		
		try {
			loadedBufferedImage = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(imageFileName));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		// loadedBufferedImage shouldn't be null here anymore, if so an error occurred
		if (loadedBufferedImage == null) {
			System.out.println("No texture defined, failed to use default texture."); 
		}
		return loadedBufferedImage;
		
	}
	private boolean isHardware(char c) {
		for (int i = Config.firstHardwareChar; i < Config.lastHardwareChar; i++) {
			if (c == i) {
				return true;
			}
		}
		return false;
	}
	
}
