package sample.game.manager;

import java.util.ArrayList;

import library.core.debug.CoreLog;
import sample.game.model.CharaModel;
import sample.game.model.CharaModel.TARGET_TYPE;





public class Island implements Cloneable{

  	protected ArrayList<CharaModel>  ministers;
  	protected ArrayList<CharaModel> wolfs;
  	protected ArrayList<CharaModel> cattles;
  	protected ArrayList<CharaModel> cabbages;

  	protected boolean nowTurn;

  	private int gameOverTypeId=0;	//ゲームオーバータイプ

	public Island(){
  		this.ministers=new ArrayList<CharaModel>();
  		this.wolfs=new ArrayList<CharaModel>();
  		this.cattles=new ArrayList<CharaModel>();
  		this.cabbages=new ArrayList<CharaModel>();

  	}

	@Override
  	public Island clone(){
  		Island is=null;


  		try {
			is=(Island)super.clone();

			is.ministers=new ArrayList<CharaModel>();
			for(int i=0;i<this.ministers.size();i++){
				is.ministers.add(this.ministers.get(i).clone());
			}
			is.wolfs=new ArrayList<CharaModel>();
			for(int i=0;i<this.wolfs.size();i++){
				is.wolfs.add(this.wolfs.get(i).clone());
			}

			is.cabbages=new ArrayList<CharaModel>();
			for(int i=0;i<this.cabbages.size();i++){
				is.cabbages.add(this.cabbages.get(i).clone());
			}

			is.cattles=new ArrayList<CharaModel>();
			for(int i=0;i<this.cattles.size();i++){
				is.cattles.add(this.cattles.get(i).clone());
			}


		} catch (CloneNotSupportedException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

  		return is;
  	}


 	public boolean getNowTurn() {
		return nowTurn;
	}

	public void setNowTurn(boolean nowTurn) {
		this.nowTurn = nowTurn;
	}



	/**
	 * 行動制限ルール 指定したタイプの属するすべてに行動制限をかける
	 * @param character
	 */
	public void setMoveLimitType(CharaModel character){
		switch (character.getType()) {
		case MINISTART:
			setMoveType1(false);
			break;
		case WOLF:
		case CATTLE:
		case CABBAGE:
			setMoveType2(false);
			break;
		}
	}




	public void addCharacter(CharaModel character){
		switch (character.getType()) {
		case MINISTART:
			addMinister(character);
			break;
		case WOLF:
			addWolf(character);
			break;
		case CATTLE:
			addCattle(character);
			break;
		case CABBAGE:
			addCabbage(character);
			break;
		}
	}


	public void removeCharacter(CharaModel character){
		switch (character.getType()) {
		case MINISTART:
			removeMinister(character);
			break;
		case WOLF:
			removeWolf(character);
			break;
		case CATTLE:
			removeCattle(character);
			break;
		case CABBAGE:
			removeCabbage(character);
			break;
		}
	}



	private void addMinister(CharaModel m){
  		this.ministers.add(m);
  	}

  	private void removeMinister(CharaModel m){
  		this.ministers.remove(m);
  	}

  	private void addWolf(CharaModel w){
  		this.wolfs.add(w);
  	}

  	private void removeWolf(CharaModel w){
  		this.wolfs.remove(w);
  	}


  	private void addCattle(CharaModel c){
  		this.cattles.add(c);
  	}

  	private void removeCattle(CharaModel c){
  		this.cattles.remove(c);
  	}


  	private void addCabbage(CharaModel c){
  		this.cabbages.add(c);
  	}

  	private void removeCabbage(CharaModel c){
  		this.cabbages.remove(c);
  	}


  	/**
  	 * 行動制御
  	 * @param canMove
  	 */
  	public void setMove(boolean canMove){
  		if(ministers.size()>0){
  			for (CharaModel b : ministers) {
				b.setCanMove(canMove);
			}
  		}

  		if(wolfs.size()>0){
  			for (CharaModel b : wolfs) {
				b.setCanMove(canMove);
			}
  		}

  		if(cattles.size()>0){
  			for (CharaModel b : cattles) {
				b.setCanMove(canMove);
			}
  		}

  		if(cabbages.size()>0){
  			for (CharaModel b : cabbages) {
				b.setCanMove(canMove);
			}
  		}

  	}


  	/**
  	 * 人ジャンルの行動制御
  	 * @param canMove
  	 */
  	public void setMoveType1(boolean canMove){
  		if(ministers.size()>0){
  			for (CharaModel b : ministers) {
				b.setCanMove(canMove);
			}
  		}

  	}

  	/**
  	 * 動物・野菜ジャンルの行動制御
  	 * @param canMove
  	 */
  	public void setMoveType2(boolean canMove){

  		if(wolfs.size()>0){
  			for (CharaModel b : wolfs) {
				b.setCanMove(canMove);
			}
  		}

  		if(cattles.size()>0){
  			for (CharaModel b : cattles) {
				b.setCanMove(canMove);
			}
  		}

  		if(cabbages.size()>0){
  			for (CharaModel b : cabbages) {
				b.setCanMove(canMove);
			}
  		}

  	}

  	public void checkMyData(){
  		if(ministers.size()>0){
  			for (CharaModel b : ministers) {
				CoreLog.debug("minister :"+b.isCanMove());
			}
  		}

  		if(wolfs.size()>0){
  			for (CharaModel b : wolfs) {
  				CoreLog.debug("wolf :"+b.isCanMove());
			}
  		}

  		if(cattles.size()>0){
  			for (CharaModel b : cattles) {
  				CoreLog.debug("cattle :"+b.isCanMove());
			}
  		}

  		if(cabbages.size()>0){
  			for (CharaModel b : cabbages) {
  				CoreLog.debug("cabbage :"+b.isCanMove());
			}
  		}
  	}



	/**
	 * ゲームオーバー判定
	 * @return
	 */
	public boolean cheakGameOver(){
		if(ministers.size()==0){
			if(wolfs.size()>0 && cattles.size()>0){
				this.gameOverTypeId=1;
				return true;
			}else if(cattles.size()>0 && cabbages.size()>0){
				this.gameOverTypeId=2;
				return true;
			}
		}
		return false;
	}

	/**
	 * 各キャラの所持数を確認
	 * @param type
	 * @return
	 */
	public int getCharaNum(TARGET_TYPE type){
		switch (type) {
		case MINISTART:
			return this.ministers.size();
		case WOLF:
			return this.wolfs.size();
		case CABBAGE:
			return this.cabbages.size();
		case CATTLE:
			return this.cattles.size();
		}
		return 0;
	}


	public ArrayList<CharaModel> getMinisters() {
		return ministers;
	}




	public ArrayList<CharaModel> getWolfs() {
		return wolfs;
	}




	public ArrayList<CharaModel> getCattles() {
		return cattles;
	}




	public ArrayList<CharaModel> getCabbages() {
		return cabbages;
	}


	//移動可能な牧師の数1
	public int canMoveMinisterNum(){
		int size=0;

		if(ministers.size()>0){
			for(int i=0;i<ministers.size();i++){
				if(ministers.get(i).isCanMove()){
					size++;
				}
			}
		}

		return size;
	}


	//移動可能な牧師の数1
	public int canMoveWolfNum(){
		int size=0;

		if(wolfs.size()>0){
			for(int i=0;i<wolfs.size();i++){
				if(wolfs.get(i).isCanMove()){
					size++;
				}
			}
		}

		return size;
	}


	//移動可能なキャベツの数1
	public int canMoveCabbageNum(){
		int size=0;

		if(cabbages.size()>0){
			for(int i=0;i<cabbages.size();i++){
				if(cabbages.get(i).isCanMove()){
					size++;
				}
			}
		}

		return size;
	}


	//移動可能な牛の数1
	public int canMoveCattleNum(){
		int size=0;

		if(cattles.size()>0){
			for(int i=0;i<cattles.size();i++){
				if(cattles.get(i).isCanMove()){
					size++;
				}
			}
		}

		return size;
	}

	public int getGameOverTypeId() {
		return gameOverTypeId;
	}



}






