package sample.game.controller;

import java.util.ArrayList;

import library.core.mainAI.RiverCrossingAI;
import library.core.model.CoreModel;
import sample.game.main.UserGameTitle;
import sample.game.model.title.HowToPlayButton;
import sample.game.model.title.LevelButton;
import sample.game.model.title.SoundButton;
import sample.game.model.title.TitleAnimationModel;


/**
 * ユーザーが操作するタイトルのコントローラー
 * @author 貴裕
 *
 */
public class TitleController {

	public enum Level{
		easy,
		normal,
		hard
	};


	private Level targetLevel=Level.easy;
	private UserGameTitle palent;

	private CoreModel backScreen;
	private CoreModel backScreen2;	//ループの後追い
	private CoreModel logo;
	private ArrayList<LevelButton> levelButtons;
	private TitleAnimationModel easyCharaAnim;
	private TitleAnimationModel normalCharaAnim;
	private TitleAnimationModel hardCharaAnim;
	private SoundButton soundButton;
	private HowToPlayButton howToPlayButton;
	private CoreModel fadeMask;

	private float time;
	private float backScreenMoveTime;

	private boolean isSelectStage=false;
	private boolean nowShowInitProductPhase=true;
	private boolean moveBackScreen=false;
	private boolean isSelectHowToPlay=false;

	public TitleController(UserGameTitle palent){
		this.palent=palent;

		time=0;
		backScreenMoveTime=0;

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


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

		lockButtons();

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

		this.nowShowInitProductPhase=true;
		this.isSelectHowToPlay=false;
		time=0;

		moveBackScreen=true;
	}


	public void update(float deltaTime){


		//突入時のフェイド処理
		if(nowShowInitProductPhase){
			time+=deltaTime;

			//CoreLog.debug("time: "+time);

			float rate=time/1f;

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

			if(time>=1f){
				fadeMask.setShowView(false);
				unlockButtons();
				this.nowShowInitProductPhase=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;
				if(isSelectHowToPlay){
					palent.loadHowToPlay();
				}else{
					goGameScreen();
				}
			}
		}

	}


	/**
	 *　黒画像でやった方がいいかも
	 * @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);


		/*backScreen.setAlpha(alpha);
		backScreen2.setAlpha(alpha);
		for (LevelButton l : levelButtons) {
			l.setAlpha(alpha);
		}
		easyCharaAnim.setAlpha(alpha);
		normalCharaAnim.setAlpha(alpha);
		hardCharaAnim.setAlpha(alpha);
		soundButton.setAlpha(alpha);
		howToPlayButton.setAlpha(alpha);
		logo.setAlpha(alpha);*/
	}



	/**
	 * 処理順の都合により分離
	 * @param buttons
	 */
	public void setLevelButtons(ArrayList<LevelButton> buttons){
		this.levelButtons=buttons;
	}

	/**
	 * 処理順の都合により分離
	 * @param buttons
	 */
	public void setSoundButton(SoundButton soundButton) {
		this.soundButton = soundButton;
	}

	/**
	 * 処理順の都合により分離
	 * @param buttons
	 */
	public void setHowToPlayButton(HowToPlayButton howToPlayButton) {
		this.howToPlayButton = howToPlayButton;
	}

	public void setEasyCharaAnim(TitleAnimationModel anim){
		this.easyCharaAnim=anim;
	}

	public void setNormalCharaAnim(TitleAnimationModel anim){
		this.normalCharaAnim=anim;
	}

	public void setHardCharaAnim(TitleAnimationModel anim){
		this.hardCharaAnim=anim;
	}

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

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

	public void setLogo(CoreModel logo) {
		this.logo = logo;
	}

	/**
	 * レベルの選択（ボタン入力）（ゲームモードの決定）
	 * @param level
	 */
	public void selectLevet(Level level){
		time=0;

		lockButtons();
		this.targetLevel=level;
		isSelectStage=true;

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

		//アニメーションの切り替え
		/*switch (level) {
		case easy:
			easyCharaAnim.changeAnimation(1);
			break;
		case normal:
			normalCharaAnim.changeAnimation(1);
			break;
		case hard:
			hardCharaAnim.changeAnimation(1);
			break;
		}*/
		easyCharaAnim.changeAnimation(1);
		normalCharaAnim.changeAnimation(1);
		hardCharaAnim.changeAnimation(1);

		moveBackScreen=false;
	}

	/**
	 * ゲームシーンへの遷移
	 */
	public void goGameScreen(){
		this.palent.loadGameScreen(targetLevel);
	}


	/**
	 * 遊び方を選んだ場合
	 */
	public void selectHowToPlay(){

		time=0;

		lockButtons();
		isSelectStage=true;
		isSelectHowToPlay=true;

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

		easyCharaAnim.changeAnimation(1);
		normalCharaAnim.changeAnimation(1);
		hardCharaAnim.changeAnimation(1);

		moveBackScreen=false;


	}



	/**
	 * ボタンの無効化
	 */
	private void lockButtons(){
		for(int i=0;i<levelButtons.size();i++){
			levelButtons.get(i).setCanUse(false);
		}

		soundButton.setCanUse(false);
		howToPlayButton.setCanUse(false);
	}

	/**
	 * ボタンの有効化
	 */
	private void unlockButtons(){
		for(int i=0;i<levelButtons.size();i++){
			levelButtons.get(i).setCanUse(true);
		}

		soundButton.setCanUse(true);
		howToPlayButton.setCanUse(true);
	}



	public Level getTargetLevel() {
		return targetLevel;
	}

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



}
