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) {
	
		// Wall images
		case Config.wayChar:
			imageFileName="de/heatup/resources/images/25x25_black.png";
			break;
		case Config.wallChar:
			imageFileName="de/heatup/resources/images/grid_1.png";
			break;

		case 'm':
			imageFileName="de/heatup/resources/images/grid_3.png";
			break;
		case 'n':
			imageFileName="de/heatup/resources/images/grid_2.png";
			break;

			
		// Opponent images w - z with animation.
		case 'w':
			imageFileName="de/heatup/resources/images/opponent_f1.png";
			break;
		case 'x':
			imageFileName="de/heatup/resources/images/opponent_f2.png";
			break;
		case 'y':
			imageFileName="de/heatup/resources/images/opponent_f3.png";
			break;
		case 'z':
			imageFileName="de/heatup/resources/images/opponent_f4.png";
			break;

		// Image for power up	
		case 'b':
			imageFileName="de/heatup/resources/images/25x25_powerup.png";
			break;
			
		// Player
		case 'p':
			imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png";
			break;

		case '0':
			//imageFileName="de/heatup/resources/images/hardware/rectangular_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_1.png";
			break;
		case '1':
			//imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
			break;
		case '2':
			//imageFileName="de/heatup/resources/images/hardware/rectangular_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
			break;
		case '3':
			//imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
			break;
		case '4':
			//imageFileName="de/heatup/resources/images/hardware/rectangular_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_1.png";
			break;
		case '5':
			//imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
			break;
		case '6':
			//imageFileName="de/heatup/resources/images/hardware/rectangular_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_1.png";
			break;
		case '7':
			//imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
			break;
		case '8':
			//imageFileName="de/heatup/resources/images/hardware/rectangular_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_1.png";
			break;
		case '9':
			//imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
			imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
			break;	
		// Default images if no other is found
		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";
//				imageFileName="de/heatup/resources/images/hardware/plain_chip_1.png";
//				break;
//			case 1:
//				//imageFileName="de/heatup/resources/images/hardware/square_chip.jpg";
//				imageFileName="de/heatup/resources/images/hardware/plain_chip_2.png";
//				break;
//			default:
//				break;
//			}
//		}
		BufferedImage loadedBufferedImage = null;
		
		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;
	}
	
}
