import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Shit extends Obj implements java.io.Serializable {
	static final long serialVersionUID = 2L;
	
	// public variables
	public static final int SHIT_NORMAL = 0;
	public static final int SHIT_CRASHED = 1;
	public static final int SHIT_SHADOW = 2;
	public static final int NUM_OF_SHIT_STATE = 3;

	private static final int SHITLIMIT[] = {100*24*2, 100*24*4, 100*24*8};
	private Body.AgeState ageState;
	private int falldownDamage = 0;
	private int amount = 0;
	
	private static final int[] shitSize = {10, 20, 30};
	private static Image[][] images = new Image[NUM_OF_SHIT_STATE][3];
	
	public static void loadImages (ClassLoader loader) throws IOException {
		final String path = "images/";

		images[SHIT_NORMAL][Body.adultIndex] = ImageIO.read(loader.getResourceAsStream(path+"unun.png"));
		images[SHIT_CRASHED][Body.adultIndex] = ImageIO.read(loader.getResourceAsStream(path+"unun2.png"));
		images[SHIT_SHADOW][Body.adultIndex] = ImageIO.read(loader.getResourceAsStream(path+"unun-shadow.png"));
		
		for (int i=0; i < NUM_OF_SHIT_STATE; i++) {
			images[i][Body.adultIndex] = images[i][Body.adultIndex].getScaledInstance(shitSize[2], shitSize[2], Image.SCALE_AREA_AVERAGING);
			images[i][Body.childIndex] = images[i][Body.adultIndex].getScaledInstance(shitSize[1], shitSize[1], Image.SCALE_AREA_AVERAGING);
			images[i][Body.babyIndex] = images[i][Body.adultIndex].getScaledInstance(shitSize[0], shitSize[0], Image.SCALE_AREA_AVERAGING);
		}
	}
	
	public Image getImage() {
		return (images[getShitState()][ageState.ordinal()]);
	}
	
	public Image getShadowImage() {
		return (images[SHIT_SHADOW][ageState.ordinal()]);
	}
	
	public int getSize() {
		return shitSize[ageState.ordinal()];
	}

	public Shit (int initX, int initY, int initZ, Body.AgeState initAgeState) {
		objType = Type.SHIT;
		x = initX;
		y = initY;
		z = initZ;
		ageState = initAgeState;
		switch (initAgeState) {
		case BABY:
			amount = 100;
			break;
		case CHILD:
			amount = 100*2;
			break;
		case ADULT:
			amount = 100*4;
			break;
		}
		removed = false;
	}

	public Body.AgeState getAgeState() { return ageState; }

	public int getShitState() {
		if (age >= SHITLIMIT[ageState.ordinal()]/4) {
			return 1;
		}
		return 0;
	}

	public void eatShit(int eatAmount) {
		amount -= eatAmount;
		if (amount < 0) {
			amount = 0;
			removed = true;
		}
	}

	public void crushShit() {
		age += SHITLIMIT[ageState.ordinal()]/2;
	}

	public Event clockTick()
	{
		if (removed == false) {
			//age += TICK;
			if (age >= SHITLIMIT[ageState.ordinal()]) {
				removed = true;
			}
			if (!grabbed && z > 0) {
				z -= 2;
				falldownDamage += 2;
				if (z <= 0) {
					if (falldownDamage > 10) {
						crushShit();
					}
					z = 0;
					falldownDamage = 0;
				}
			}
			return Event.DONOTHING;
		}
		return Event.REMOVED;
	}
}
