package sample.game.controller;

import java.util.ArrayList;

import library.core.mainAI.RiverCrossingAI;
import library.core.model.CoreModel;
import sample.game.main.AiGameTitle;
import sample.game.model.titleAi.NumberButton;
import sample.game.model.titleAi.TitleAiNextButton;

public class TitleAiController {


	private int boatSize=2;
	private int ministerNum=1;
	private int wolfNum=1;
	private int cabbageNum=1;
	private int cattleNum=1;

	private AiGameTitle palent;


	private ArrayList<NumberButton> numberButtons;

	private CoreModel backScreen;
	private CoreModel backScreen2;
	private TitleAiNextButton nextButton;
	private CoreModel fadeMask;

	private float time=0;
	private float backScreenMoveTime;

	private boolean nowInitProductPhase;
	private boolean moveBackScreen;

	private boolean isSelectStage=false;


	public TitleAiController(AiGameTitle palent){
		this.palent=palent;

		this.backScreenMoveTime=0;
		this.isSelectStage=false;

		numberButtons=new ArrayList<NumberButton>();

		RiverCrossingAI.getBgmPlayer().play("bgm/title.ogg", 0);
	}


	/**
	 * 初期演出の設定
	 */
	public void checkInitProduction(){

		lockButton();

		this.fadeMask.setAlpha(255);
		this.fadeMask.setShowView(true);

		this.nowInitProductPhase=true;
		time=0;

		this.moveBackScreen=true;


	}

	public void update(float deltaTime){

		//開始演出
		if(nowInitProductPhase){
			time+=deltaTime;

			float rate=time/1f;

			if(rate>1) rate=1;

			this.fadeMask.setAlpha((int)(255*(1f-rate)));
			RiverCrossingAI.getBgmPlayer().setVolume(rate);

			if(time>=1f){
				RiverCrossingAI.getBgmPlayer().setVolume(1f);
				fadeMask.setShowView(false);
				unlockButton();
				this.nowInitProductPhase=false;
				time=0;
			}

			return;

		}



		//背景のループ
		if(moveBackScreen){
			backScreenMoveTime+=deltaTime;
			if(backScreenMoveTime>=0.01f){
				backScreenMoveTime=0;
				backScreen.move(4f, 0);
				backScreen2.move(4f, 0);

				if(backScreen.getCenterX()>=1920){
					backScreen.setCenterX(-640);
				}

				if(backScreen2.getCenterX()>=1920){
					backScreen2.setCenterX(-640);
				}
			}
		}



		//ゲーム画面への移動演出
		if(isSelectStage){

			time+=deltaTime;

			if(time>=0.5f){
				fadeout((1.5f-time)/1f);
			}

			if(time>=1.5f){
				time=-100;
				RiverCrossingAI.getBgmPlayer().stop();
				startGame();
			}
		}

	}



	/**
	 *　フェイド処理
	 * @param rate 0で完全に透明
	 */
	public void fadeout(float rate){
		int alpha=(int)((rate)*255);
		if(alpha<0) alpha=0;

		if(rate<0) rate=0;
		RiverCrossingAI.getBgmPlayer().setVolume(rate);

		fadeMask.setAlpha(255-alpha);
	}


	/**
	 * 自身と同じ属性のボタン以外を非表示にする
	 * @param button
	 */
	public void elseButtonOff(NumberButton button){
		for(int i=0;i<numberButtons.size();i++){
			if(!button.equals(numberButtons.get(i))){
				numberButtons.get(i).setOffImage(button.getType());
			}
		}
	}

	/**
	 * ゲーム開始演出
	 */
	public void startGameProduct(){

		time=0;
		isSelectStage=true;
		lockButton();

		fadeMask.setAlpha(0);
		fadeMask.setShowView(true);

		moveBackScreen=false;
	}


	/**
	 * ゲーム画面への遷移
	 */
	private void startGame(){
		palent.startGame(boatSize, ministerNum, wolfNum, cabbageNum, cattleNum);
	}

	/**
	 * ボタンの封印
	 */
	public void lockButton(){
		for(int i=0;i<numberButtons.size();i++){
			numberButtons.get(i).setCanUse(false);
		}
		nextButton.setTouch(false);
	}

	/**
	 * ボタンの解放
	 */
	public void unlockButton(){
		for(int i=0;i<numberButtons.size();i++){
			numberButtons.get(i).setCanUse(true);
		}
		nextButton.setTouch(true);
	}



	/**
	 * 数字ボタンの設定
	 * @param button
	 */
	public void setNumberButtons(ArrayList<NumberButton> button){
		this.numberButtons=button;
	}

	public int getBoatSize() {
		return boatSize;
	}

	public void setBoatSize(int boatSize) {
		this.boatSize = boatSize;
	}

	public int getMinisterNum() {
		return ministerNum;
	}

	public void setMinisterNum(int ministerNum) {
		this.ministerNum = ministerNum;
	}

	public int getWolfNum() {
		return wolfNum;
	}

	public void setWolfNum(int wolfNum) {
		this.wolfNum = wolfNum;
	}

	public int getCabbageNum() {
		return cabbageNum;
	}

	public void setCabbageNum(int cabbageNum) {
		this.cabbageNum = cabbageNum;
	}

	public int getCattleNum() {
		return cattleNum;
	}

	public void setCattleNum(int cattleNum) {
		this.cattleNum = cattleNum;
	}

	public void setBackScreen(CoreModel backScreen) {
		this.backScreen = backScreen;
	}

	public void setBackScreen2(CoreModel backScreen2) {
		this.backScreen2 = backScreen2;
	}

	public void setNextButton(TitleAiNextButton nextButton) {
		this.nextButton = nextButton;
	}

	public void setFadeMask(CoreModel fadeMask) {
		this.fadeMask = fadeMask;
	}






}
