package sample.game.model;

import java.util.ArrayList;

import library.core.model.CoreAnimation;
import library.core.model.CoreAnimationModel;
import sample.game.model.CharaModel.TARGET_TYPE;


/***
 *
 * isMountTarget()いらない？
 *
 * @author 貴裕
 *
 */
public class BoatModel extends CoreAnimationModel {




	private ArrayList<CharaModel> rideMenbers;
	private int maxMenberNum;	//乗船人数の制限

	private static final int BOAT_RIGHT_X=800;
	private static final int BOAT_LEFT_X=500;
	private static final int BOAT_DIFER=300;

	private float time=0;
	private int count=0;

	private int moveCount=10;
	private int countMaxDefo=10;
	private int moveSpeed=4;
	private int moveSpeedDefo=4;
	private int moveChangeSpeed;

	private int moveNum=0;	//移動回数

	private boolean nowRight;




	public BoatModel(int maxNum,ArrayList<CoreAnimation> animations,
			int startTargetAnimIndex, float centerX, float centerY,
			float width, float height) {
		super(animations, startTargetAnimIndex, centerX, centerY, width, height);

		this.maxMenberNum=maxNum;
		this.rideMenbers=new ArrayList<CharaModel>();

		this.nowRight=true;
		this.moveChangeSpeed=moveCount;

		moveNum=0;	//移動回数
	}



	@Override
	public BoatModel clone(){

		BoatModel b=null;

		b=(BoatModel)super.clone();
		b.rideMenbers=new ArrayList<CharaModel>(this.rideMenbers);

		return b;
	}

	/**
	 * 船にキャラクターを乗せる
	 * @param c
	 * @return
	 */
	public boolean addCharacter(CharaModel c){

		if(maxMenberNum<=this.rideMenbers.size()){
			return false;
		}

		this.rideMenbers.add(c);

		return true;
	}

	public int getRideMenberNum(){
		return this.rideMenbers.size();
	}

	public int getRideMenberNum(TARGET_TYPE type){
		int num=0;

		for(int i=0;i<rideMenbers.size();i++){
			if(rideMenbers.get(i).getType()==type){
				num++;
			}
		}


		return num;
	}


	/**
	 * 船の移動速度変更
	 */
	public void changeAiMoveCharaSpeed(){
		moveSpeed--;
		if(moveSpeed<1) moveSpeed=4;
		this.moveChangeSpeed=(int) (countMaxDefo*(moveSpeed/(float)moveSpeedDefo));
	}


	////////////////////////////////////////////////////
	/**
	 * キャラクターが乗ったかどうかの判定
	 * @return
	 */
	public boolean isMountTarget(){


		return false;
	}
////////////////////////////////////////


	/**
	* 移動状態の初期化
	*/
	public void moveInit(){
		time=0;
		count=0;

		if(moveCount!=moveChangeSpeed) moveCount=moveChangeSpeed;
	}


	/**
	 * 移動処理
	 * @param deltaTime
	 * @return
	 */
	public boolean move(float deltaTime){

		boolean r;

		if(!nowRight){
			r=moveRight(deltaTime);
		}else{
			r=moveLeft(deltaTime);
		}

		return r;
	}

	private boolean moveRight(float deltaTime){


		time+=deltaTime;

		if(time>0.05){
			time=0;

			this.setCenterX((int) (BOAT_LEFT_X+BOAT_DIFER*((float)count/moveCount)));

			//乗船している対象の移動
			for(int i=0;i<this.getRideMenberNum();i++){
				this.getRideMenber(i).move(BOAT_DIFER*(1f/moveCount), 0);
			}

			count++;
			if(count>moveCount){
				moveInit();
				nowRight=true;
				return true;
			}
		}


		return false;
	}


	private boolean moveLeft(float deltaTime){

		time+=deltaTime;
		if(time>0.05){
			time=0;

			this.setCenterX((int) (BOAT_RIGHT_X-BOAT_DIFER*((float)count/moveCount)));

			//乗船している対象の移動
			for(int i=0;i<this.getRideMenberNum();i++){
				this.getRideMenber(i).move(-BOAT_DIFER*(1f/moveCount), 0);
			}

			count++;
			if(count>moveCount){
				moveInit();
				nowRight=false;
				return true;
			}
		}


		return false;
	}



	public ArrayList<CharaModel> getRideMenbers(){
		return this.rideMenbers;
	}

	public CharaModel getRideMenber(int index){
		return this.rideMenbers.get(index);
	}

	public void clearRideMenber(){
		this.rideMenbers.clear();
	}


	public int getRideMaxMenberNum(){
		return this.maxMenberNum;
	}

	public void difMoveNum(){
		this.moveNum--;
	}

	public void addMoveNum(){
		this.moveNum++;
	}

	public int getMoveNum(){
		return this.moveNum;
	}



	@Override
	public void endAnimationEvent(CoreAnimation endAnim) {


	}




}
