package  Sprites;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
//import java.io.File;
import java.io.IOException;
//import java.net.URL;


public abstract class Sprite {
	protected BufferedImage bImage;
	protected int imageWidth, imageHeight; // image dimensions

	protected double locX, locY;
	protected int acceleration;
	protected int pWidth, pHeight, size;  // panel's dimensions

	protected double angle;
//	private final String BASE_PATH = System.getProperty("user.dir") + File.separator +"src"+ File.separator;


	public Sprite(int x, int y, int w, int h, int acc, String imgName, double angle, int size)
	{
		this.size = size;
		locX = x;
		locY = y;
		acceleration = acc;
		pWidth = w;
		pHeight = h;
		this.angle = angle;

		//load image from source files
		try {
			bImage = ImageIO.read(getClass().getResource("/Images/"+imgName));
		}catch (IOException pin){
			pin.printStackTrace();
			bImage = null;
		}

		setImageDimensions();
	}


	/*
	 * resizes image to a set size
	 */
	private void setImageDimensions()
	{
		Image tmp = bImage.getScaledInstance(size,size, Image.SCALE_SMOOTH);
		BufferedImage bi = new BufferedImage(size,size, BufferedImage.TYPE_INT_ARGB);

		Graphics2D g2d = bi.createGraphics();
		g2d.drawImage(tmp,0,0,null);
		g2d.dispose();
		bImage = bi;
	}
	
	/**
	 * Abstract method to update sprite.
	 */
	public abstract void update();

	/**
	 * returns sprite x position
	 * @return
	 * double
	 */
	public double getLocX() {return locX;}
	
	/**
	 * returns sprite y position
	 * @return
	 * double
	 */
	public double getLocY() {return locY;}
	
	/**
	 * returns sprite acceleration
	 * @return
	 * int
	 */
	public int getAcceleration() {return acceleration;}
	
	/**
	 * returns sprite size
	 * @return
	 * int
	 */
	public BufferedImage getbImage() {return bImage;}
	
	/**
	 * returns image width
	 * @return
	 * int
	 */
	public int getImageWidth() {return imageWidth;}
	
	/**
	 * returns image height
	 * @return
	 * int
	 */
	public int getImageHeight() {return imageHeight;}
	
	/**
	 * returns sprite angle
	 * @return
	 * double
	 */
	public double getAngle() {return angle;}
	
	/**
	 * returns sprite size
	 * @return
	 * int
	 */
	public int getSize() {return size;}

	
	/**
	 * returns shape location and dimensions as a Rectangle.
	 * @return
	 * Rectangle
	 */
	public Rectangle getBounds() {
		return new Rectangle((int)locX, (int)locY, size, size);
		}

	
	/**
	 * its not a bug it's  a feature. actually it just moves a shape that goes beyond the screen to the other side.
	 */
	protected void outOfScreeFix(){ 
		if(locX < 0 - size)
			locX = pWidth;
		else if (locX > pWidth+size)
			locX = 0-size;

		if(locY < 0 - size)
			locY = pHeight;
		else if(locY > pHeight+size)
			locY = 0-size ;
	}

	/**
	 * abstract method for drawing sprite.
	 * @param g
	 * @param p
	 */
	public abstract void drawSprite(Graphics g, JPanel p);

}
