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

import javax.imageio.ImageIO;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FoodMaker extends ObjEX implements java.io.Serializable {
	static final long serialVersionUID = 1L;

	public static List<ObjEX> objEXList = new ArrayList<ObjEX>();

	public int type = 0;
	public static final int hitCheckObjType = 7;
	public static final int size = 128;
	private static final int images_num = 6; //このクラスの総使用画像数
	private static final int anime_pattern = 1; //このクラスのアニメーション数
	private static int AnimeImagesNum[] = {6};//アニメごとに何枚使うか
	private static Image[] images = new Image[images_num];
	protected Random rnd = new Random();

	protected boolean processReady = true;
	private static int value = 30000;
	private static int cost = 20;
	protected int stockFood = -1;
	private static final int numOfBodyType = 5;
	private static final int numOfFoodType = 5;

											//Re(Wasa),Ma,Al,Ta,Hy,YF,BT,LP,HOT,VG,shit
	private static final int[][] makeTable = {{ 1,0,5,0,1,1,2,3,4,5,0},//Re(Wasa)
											  { 0,1,5,0,1,1,2,3,4,5,0},//Ma
											  { 5,5,1,0,1,5,2,5,4,5,0},//Al
											  { 0,0,0,2,1,2,0,2,0,0,0},//Ta
											  { 1,1,1,1,3,1,1,1,1,1,1},//Hy
											  { 1,1,5,2,1,1,1,1,4,1,0},//YF
											  { 2,2,2,0,1,1,2,0,0,0,0},//BT
											  { 3,3,5,2,1,1,0,3,4,0,0},//LP
											  { 4,4,4,0,1,4,0,4,4,0,0},//HOT
											  { 5,5,5,0,1,1,0,0,0,5,0},//VG
											  { 0,0,0,0,1,0,0,0,0,0,0}};//shit
	
	private static final Food.type[] foodTable =	{Food.type.SHIT,
													 Food.type.YUKKURIFOOD,
													 Food.type.BITTER,
													 Food.type.LEMONPOP,
													 Food.type.HOT,
													 Food.type.VIYUGRA};

	public static void loadImages (ClassLoader loader) throws IOException {
		final String path = "images/foodmaker/";
		for( int i = 0; i < images_num ; i++ ){
			images[i] = ImageIO.read(loader.getResourceAsStream(path+"foodmaker"+String.format("%03d",i+1)+".png"));		
			images[i] = images[i].getScaledInstance(size*14/8, size*6/8, Image.SCALE_AREA_AVERAGING);
		}
	}

	public Image getImage(){
		return images[(int)age/2%AnimeImagesNum[0]];
	}

	@Override
	public int getSize() {
		return size;
	}

	@Override
	public int getHitCheckObjType() {
		return hitCheckObjType;
	}

	@Override
	public int checkHitObj( Obj o ) {
		int objX = o.getX();
		int objY = o.getY();
		int objZ = o.getZ();
		int platformSize = getSize()/2;
		int platformX = getX();
		int platformY = getY();
		int platformZ = getZ();
		int offsetX = (Body.MAXSIZE - getSize())*7/32-28;
		int offsetY = (Body.MAXSIZE - getSize())*6/16-40;
			if ( objZ == 0 ){	//空中の物は移動させない
				if ((platformX + offsetX)<=(objX) && (objX)<=(platformX + offsetX + platformSize)){	//X軸比較
					if ((platformY + offsetY)<=(objY) && (objY)<=(platformY + offsetY + platformSize)){	//Y軸比較
						objHitProcess( o );
						return 1;
					}
				}
			}
		return 0;
	}

	@Override	
	public int objHitProcess( Obj o ) {
		if ( !processReady ){
			return 0;
		}
		Food.type foodType = null;
		if (stockFood == -1){
			if ( o.objType == Obj.Type.YUKKURI ){
				Body b = (Body)o;
				if ( b.isCrashed() == true || b.isBaby() == true ) {
					if ( b.getType() == 3 ) {
						stockFood = 0;
					}else if ( b.getType() == 4 ) {
						stockFood = 3;
					}else if( b.getType() >= 10000 ){
						stockFood = 4;
					}else{
						stockFood = b.getType();
					}
					if ( b.isAdult() == true ) {
						objHitProcess( o );
					}else{
						processReady = false;
						b.remove();
					}
				}
			}else if ( o.objType == Obj.Type.FOOD ){
				Food f = (Food)o;
				switch(f.getFoodType()){
					case YUKKURIFOOD:
						stockFood = numOfBodyType;
						break;
					case BITTER:
						stockFood = numOfBodyType + 1;
						break;
					case SELFFEEDER:
						stockFood = numOfBodyType;
						break;
					case LEMONPOP:
						stockFood = numOfBodyType + 2;
						break;
					case HOT:
						stockFood = numOfBodyType + 3;
						break;
					case VIYUGRA:
						stockFood = numOfBodyType + 4;
						break;
					default:
						break;
				}
				processReady = false;
				f.remove();
			}else if ( o.objType == Obj.Type.SHIT ){
				Shit s = (Shit)o;
				stockFood = numOfBodyType+numOfFoodType;
				processReady = false;
				s.remove();
			}
		}else{
			if ( o.objType == Obj.Type.YUKKURI ){
				Body b = (Body)o;
				if ( b.isCrashed() == true || b.isBaby() == true ) {
					if ( b.getType() == 3 ) {
						foodType = foodTable[makeTable[stockFood][0]];
					}else if ( b.getType() == 4 ) {
						foodType = foodTable[makeTable[stockFood][3]];
					}else if( b.getType() >= 10000 ){
						foodType = foodTable[makeTable[stockFood][4]];
					}else{
						foodType = foodTable[makeTable[stockFood][b.getType()]];
					}
					processReady = false;
					Cash.addCash(-getCost());
					b.remove();
				}
			}else if ( o.objType == Obj.Type.FOOD ){
				Food f = (Food)o;
				switch(f.getFoodType()){
					case YUKKURIFOOD:
						foodType = foodTable[makeTable[stockFood][numOfBodyType]];
						break;
					case BITTER:
						foodType = foodTable[makeTable[stockFood][numOfBodyType + 1]];
						break;
					case SELFFEEDER:
						foodType = foodTable[makeTable[stockFood][numOfBodyType]];
						break;
					case LEMONPOP:
						foodType = foodTable[makeTable[stockFood][numOfBodyType + 2]];
						break;
					case HOT:
						foodType = foodTable[makeTable[stockFood][numOfBodyType + 3]];
						break;
					case VIYUGRA:
						foodType = foodTable[makeTable[stockFood][numOfBodyType + 4]];
						break;
					default:
						break;
				}
				processReady = false;
				Cash.addCash(-getCost());
				f.remove();
			}else if ( o.objType == Obj.Type.SHIT ){
				Shit s = (Shit)o;
				foodType = foodTable[makeTable[stockFood][numOfBodyType+numOfFoodType]];
				processReady = false;
				Cash.addCash(-getCost());
				s.remove();
			}
			if (foodType == null) {
				return 0;
			}
			if (foodType == Food.type.SHIT){
				SimYukkuri.mypane.terrarium.addShit(x+52, y, 0, Body.AgeState.ADULT);
			}else{
				SimYukkuri.mypane.terrarium.addFood(x+52, y, foodType);
			}
			stockFood = -1;
		}
		return 0;
	}

	@Override
	public void upDate() {
		if ( age % 4 == 0 && processReady == false ){
			processReady = true;
		}
	}
	
	@Override
	public int getValue() {
		return value;
	}

	@Override
	public int getCost() {
		return cost;
	}
	
	@Override
	public void removeListData(){
		objEXList.remove(this);
	}

	public FoodMaker(int initX, int initY, Direction initDir) {
		super(initX, initY, ObjEX.Direction.BACK);
		objEXList.add(this);
		objType = Type.PLATFORM;
		Cash.addCash(-getValue());
	}
}