///////////////////////////////////////////////////////////
//  UI.cpp
//  Implementation of the Class UI
//  Created on:      09-7-2011 20:24:52
//  Original author: u90499
///////////////////////////////////////////////////////////

#include "UI.h"
#include "TouchSensor.h"
#include "Clock.h"
#include "Nxt.h"
#include "Lcd.h"

#include <string.h>
#include <stdio.h>


#define BACKGROUND_PERIOD (48)


Clock clock;


UI::UI(){
	m_monitor = new Monitor();
}



UI::~UI(){
	delete m_monitor;
}




void UI::printCourceSelect(){
	m_monitor->printData("Select Couse\nIn/Out",2000);
}

void UI::selectCource(bool& bInOutFlag, bool& bDifficultStageFlag){

	Nxt nxt;
	Clock clock;
	Lcd lcd;

	bool bOKNGflag = false;
	
	do{
		
		//
		// Cource select
		//
		printCourceSelect();
		bInOutFlag = selectBotton("In","Out");
		
		//
		// DifficultStage select
		//
		printDifficultStage();
		bDifficultStageFlag = selectBotton("Exec","None");

		//
		// OK/NG select
		//
		
		printResult(bInOutFlag, bDifficultStageFlag);
		bOKNGflag = selectOKNG();

	}while(!bOKNGflag);
}

void UI::waitForTouchSenserOn(){
	m_monitor->printData("TOUCH:START/STOP",1000);

	bool bStartFlag = false;
	while(1)
	{
		// タッチセンサの状態を検出
		if (m_touch->isPressed() == true){
			
			bStartFlag = (bStartFlag == false) ? true : false;
			m_DriverMgr->notifyStart(bStartFlag);
		} 
		clock.wait(BACKGROUND_PERIOD);
	}
}


bool UI::selectOKNG(){
	m_monitor->printData("Result Select\nOK/NG?",2000);
	bool bOKNGflag = selectBotton("OK","NG");
	
	if(bOKNGflag == true) m_monitor->printData("GOGO!",1000);
	else m_monitor->printData("ReENTR ;;",1000);
	return bOKNGflag;
}




void UI::notifyTouchSenserPut(){

}

void UI::printDifficultStage(){
	m_monitor->printData("Select\nDifficult Stage\nExec/None", 2000);
}

void UI::printResult(bool bInOutFlag, bool bDifficultStageFlag){

	
	CHAR chComment[32] = {""};
	
	if(bInOutFlag == true)	sprintf(chComment,"Cource:In\n");
	else sprintf(chComment,"Cource:Out\n");
		
	if(bDifficultStageFlag == true)	strcat(chComment,"Nansho:Exec");
	else strcat(chComment,"Nansho:None");
	
	m_monitor->printData(chComment, 2000);
}




bool UI::selectBotton(CHAR chTrueData[],CHAR chFalseData[]){
	Nxt nxt;
	Clock clock;

	bool bflag  = true;
	m_monitor->printData(chTrueData,0);

	while(1){

		
		if (nxt.getButtons() == Nxt::RUN_ON){
			if(bflag == true){
				 bflag  = false;
			}
			else{
				bflag  = true;
			}
			if(bflag == true){
				m_monitor->printData(chTrueData, 0);
			}else{
				m_monitor->printData(chFalseData, 0);
			}
		}
		clock.wait(1000);
		if (nxt.getButtons() == Nxt::ENTR_ON){
			break;
		}
	}
	return bflag;
}

void UI::printcalibration(){
	m_monitor->printData("calibration Exec",2000);
	
	Nxt nxt;
	Clock clock;
	Lcd lcd;

	bool bOKNGflag = false;
	do{

		S16 sBlack = calibrate("Black:");
		S16 sWhite = calibrate("White:");

		lcd.clear();
		lcd.putf("sdn", "Black:", long(sBlack), 0);
		lcd.putf("sdn", "White:", long(sWhite), 0);
		lcd.disp();
		clock.wait(2000);
		
		bOKNGflag = selectOKNG();
		

		clock.wait(3000);
	}while(!bOKNGflag);
}


S16 UI::calibrate(char* chData){
	Nxt nxt;
	Clock clock;
	S16 sBrightData = 0;
	while(1){
		sBrightData = m_light->getBrightness();
		m_monitor->printData(chData,sBrightData, 0);

		clock.wait(1000);
		if (nxt.getButtons() == Nxt::ENTR_ON){
			break;
		}
	}
	clock.wait(2000);
	return sBrightData;
}

