import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class Beltconveyor 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 = 31;
	public static final int size = 128;
	private static final int images_num = 10; //このクラスの総使用画像数
	private static final int anime_pattern = 2; //このクラスのアニメーション数
	private static int AnimeImagesNum[] = {5,5};//アニメごとに何枚使うか
	private static Image[] images = new Image[images_num];

	private int beltSpeed;
	private static final int value = 2000;
	private static final int cost = 5;

	public static void loadImages (ClassLoader loader) throws IOException {
		final String path = "images/beltconveyor/";
		for( int i = 0; i < images_num ; i++ ){
			images[i] = ImageIO.read(loader.getResourceAsStream(path+"beltconveyor"+String.format("%03d",i+1)+".png"));		
			images[i] = images[i].getScaledInstance(size*9/8, size*3/4, Image.SCALE_AREA_AVERAGING);
		}
	}
	
	@Override
	public Image getImage() {
		switch ( direction ) {	//楽にアニメ指定できるようにしたいが後で
			case BACK:
			default:
				return images[(int)age/4%AnimeImagesNum[0]];	//4フレームに1回画像更新
			case THISSIDE:
				return images[4-((int)age/4%AnimeImagesNum[0])];
			case RIGHT:
				return images[9-(int)age/4%AnimeImagesNum[1]];
			case LEFT:
				return images[5+((int)age/4%AnimeImagesNum[1])];
		}
	}

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

	@Override
	public int getHitCheckObjType() {
		return hitCheckObjType;
	}
	
	@Override
	public int objHitProcess( Obj o ) {
		int objX = o.getX();
		int objY = o.getY();
		int objSize = o.getSize();
		if (  !Terrarium.onBarrier( objX, objY, objSize/2, objSize/4 ) ) {		//壁にひっかかっていれば移動させない
			switch ( getDirection() ) {	//移動させる方向
				case BACK:
				default:
					o.setY(objY-beltSpeed);
					break;
				case THISSIDE:
					o.setY(objY+beltSpeed);
					break;
				case RIGHT:
					o.setX(objX+beltSpeed);
					break;
				case LEFT:
					o.setX(objX-beltSpeed);
					break;
			}
		}
		return 0;
	}

	@Override
	public void upDate() {
		if ( age % 150 == 0 ){
			Cash.addCash(-getCost()/4);
		}
	}

	public int getBeltSpeed() {
		return beltSpeed;
	}

	@Override
	public int getValue() {
		return value;
	}

	@Override
	public int getCost() {
		return cost;
	}

	@Override
	public void buyObj() {
		Cash.addCash(-getValue());
	}

	@Override
	public void removeListData(){
		objEXList.remove(this);
	}
	
	public Beltconveyor(int initX, int initY, Direction initDir) {
		super(initX, initY, initDir);
		objEXList.add(this);
		objType = Type.PLATFORM;
		beltSpeed = 1;
	}
}