/**
 * Sprites.java
 * Cuts up the main sprite sheet
 *
 * @author  Paul Krishnamurthy
 */

package com.example.flappybird;

import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.net.URL;
import java.util.HashMap;

public class Sprites {

    // HashMap of texture objects
	private static final HashMap<String, Texture> textures = new HashMap<String, Texture>();

	// Sprite sheet
	protected BufferedImage spriteSheet;

	public Sprites() {
		// Try to load sprite sheet, exit program if cannot
		try {
			String fontPath = "/res/img/spriteSheet.png";
			URL fontUrl = this.getClass().getResource(fontPath);
			assert fontUrl != null;

			spriteSheet = ImageIO.read(fontUrl);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Could not load sprite sheet.");
			System.exit(-1); // Exit program if file could not be found
			return;
		}

		// Backgrounds
		putTexture("background1", 0, 0, 0, 0, 144, 256);
		putTexture("background2", 146, 0, 0, 0, 144, 256);

		// Pipes
		putTexture("pipe-top", 56, 323, 0, 0, 26, 160);
		putTexture("pipe-bottom", 84, 323, 0, 0, 26, 160);

		// Birds
		putTexture("yellowBird1", 31, 491, 172, 250, 17, 12);
		putTexture("yellowBird2", 59, 491, 172, 250, 17, 12);
		putTexture("yellowBird3", 3, 491, 172, 250, 17, 12);

		putTexture("blueBird1", 115, 329, 172, 250, 17, 12);
		putTexture("blueBird2", 115, 355, 172, 250, 17, 12);
		putTexture("blueBird3", 87, 491, 172, 250, 17, 12);

		putTexture("redBird1", 115, 407, 172, 250, 17, 12);
		putTexture("redBird2", 115, 433, 172, 250, 17, 12);
		putTexture("redBird3", 115, 381, 172, 250, 17, 12);

		// Buttons
		putTexture("playButton", 354, 118, 34, 448, 52, 29);
		putTexture("leaderboard", 414, 118, 203, 448, 52, 29);
		putTexture("rateButton", 465, 1, 147, 355, 31, 18);

		// Helpful / Text
		putTexture("newHighscore", 112, 501, 210, 305, 16, 7);
		putTexture("titleText", 351, 91, 72, 100, 89, 24);
		putTexture("getReadyText", 295, 59, 68, 180, 92, 25);
		putTexture("gameOverText", 395, 59, 62, 100, 96, 21);
		putTexture("instructions", 292, 91, 113, 300, 57, 49);

		// SCORE IMAGES
		putTexture("score-0", 496, 60, 0, 0, 12, 18);
		putTexture("score-1", 136, 455, 0, 0, 8, 18);

		int score = 2;
		for (int i = 292; i < 335; i += 14) {
			putTexture("score-" + score, i, 160, 0, 0, 12, 18);
			putTexture("score-" + (score + 4), i, 184, 0, 0, 12, 18);
			score++;
		}

		// Mini numbers
		score = 0;
		for (int i = 323; score < 10; i += 9) {
			putTexture("mini-score-" + score, 138, i, 0, 0, 10, 7);
			score++;
			if (score % 2 == 0) {
				i += 8;
			}
		}

		// Medals
		putTexture("bronze", 112, 477, 73, 285, 22, 22);
		putTexture("silver", 112, 453, 73, 285, 22, 22);
		putTexture("gold", 121, 282, 73, 285, 22, 22);
		putTexture("platinum", 121, 258, 73, 285, 22, 22);

		// Other assets
		putTexture("base", 292, 0, 0, 521, 168, 56);
		putTexture("scoreCard", 3, 259, 40, 230, 113, 57);
	}

	private void putTexture(String name, int texX, int texY, int panX, int panY, int width, int height) {
		BufferedImage subimage = spriteSheet.getSubimage(texX, texY, width, height);
		Texture texture = new Texture(resize(subimage), panX, panY);
		textures.put(name, texture);
	}

	/**
	 * Resizes a BufferedImage
	 * 
	 * @param  image     BufferedImage object
	 * @return           New resized image
	 */
	private static BufferedImage resize(BufferedImage image) {
		// New width and height
        // Resize factor to match frame size
        double RESIZE_FACTOR = 2.605;
        int newWidth = (int) (image.getWidth() * RESIZE_FACTOR);
		int newHeight = (int) (image.getHeight() * RESIZE_FACTOR);

		// Create new BufferedImage with updated width and height
	    BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
	    Graphics2D g = resizedImage.createGraphics();
	    g.drawImage(image, 0, 0, newWidth, newHeight, null);
	    g.dispose();

	    return resizedImage;
	}

	/**
	 * Public getter method for Textures HashMap
	 * 
	 * @return     Texture
	 */
	public HashMap<String, Texture> getGameTextures() {
		return textures;
	}

}

