package sample.game.controller;

import library.core.mainAI.RiverCrossingAI;
import library.core.model.CoreModel;
import sample.game.main.HowToPlay;
import sample.game.model.hotToPlay.HowToPlayScreen;
import sample.game.model.hotToPlay.HowToPlayText;

public class HowToPlayController {


	private HowToPlay palent;


	private HowToPlayScreen screen;
	private HowToPlayText text;
	private CoreModel nextIcon;
	private CoreModel fadeMask;


	private float time=0;
	private float iconMoveTime=0;
	private float iconMoveCount=0;
	private float iconMoveCountMax=5;
	private float iconMoveLen=2;

	private boolean nowShowInitProductPhase=true;
	private boolean nowEndProductionPhase=false;


	public HowToPlayController(HowToPlay palent){
		this.palent=palent;

		time=0;
		this.iconMoveTime=0;
		this.iconMoveCount=0;

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

	public void checkInitProduction(){

		lockButtons();

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

		this.nowShowInitProductPhase=true;
		this.nowEndProductionPhase=false;

		time=0;
	}


	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(nowEndProductionPhase){

			time+=deltaTime;

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

			if(time>=1.5f){
				time=-100;
				this.goTitle();
			}
			return;
		}


		this.iconMoveTime+=deltaTime;
		if(iconMoveTime>=0.01f){
			iconMoveTime=0;
			iconMoveCount++;
			if(iconMoveCount>=iconMoveCountMax){
				iconMoveCount=0;
				iconMoveLen=-iconMoveLen;
			}
			nextIcon.move(0, iconMoveLen);
		}




	}

	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);
	}

	public void goEndPhase(){

		this.time=0;

		lockButtons();

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

		this.nowEndProductionPhase=true;
	}


	private void goTitle(){
		palent.startTitle();
	}


	public void lockButtons(){
		this.text.setTouch(false);
	}

	public void unLockButtons(){
		this.text.setTouch(true);
	}

	public void nextScreen(){
		screen.nextScreen();
	}


	public void setScreen(HowToPlayScreen screen) {
		this.screen = screen;
	}

	public void setText(HowToPlayText text) {
		this.text = text;
	}

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

	public void setNextIcon(CoreModel nextIcon) {
		this.nextIcon = nextIcon;
	}



}
