#include "BlueTooth.h"
#include "Runtime.h"


extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
	#include "task.h"
}

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

static const int xFirst	= 10;
static const int xSec	= 260;
static const int xThi	= 290;
static const int xFourth	= 430;
static const int xFifth	= 570;

void TBlueTooth::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	IsOnGoing = 0;
	ReadWrite =0;
	
	IsPermission =0;
	
	OnOff.Share =0;
	OnOff.Cmsn =0;
	OnOff.ReadOnly =1;
	OnOff.Mute =0;
	OnOff.Reset =0;
	
	State.DeviceOn =0;
	State.Connection =0;
	State.Rsv1 =1;
	State.Rsv2 =2;
}

void TBlueTooth::sDelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TBlueTooth::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TBlueTooth::RenderBackGround(unsigned int aClr)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, aClr);
}

void TBlueTooth::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "数据传输->蓝牙传输");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "蓝牙设置");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "工作模式");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "数据权限");
		TextRender_string24(Lv.Grid.Left[3] + 10, Lv.Grid.Top[3] +6, clNearWhite, "操作权限");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "开启", 38, clNearBlack);
		TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "关闭", 38, clNearBlack);
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[1] + 40, clNearWhite, "信息共享");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[1] + 40, clNearWhite, "调试");
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "只读");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "读写");
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[3] + 40, clNearWhite, "消音");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[3] + 40, clNearWhite, "复位");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Data Transmission->Blue Tooth");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Blue Tooth Setting");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Mode");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "Data Permission");
		TextRender_string24(Lv.Grid.Left[3] + 10, Lv.Grid.Top[3] +6, clNearWhite, "Operating Permission");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "Enable", 26, clNearBlack);
		TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "Disable", 26, clNearBlack);
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[1] + 40, clNearWhite, "Interconnection");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[1] + 40, clNearWhite, "Test Only");
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Read Only");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Read Write");
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[3] + 40, clNearWhite, "Mute");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[3] + 40, clNearWhite, "Reset");
	}
}

void TBlueTooth::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawFixText();
	Check4Selected();
	DrawSetting();
}

void TBlueTooth::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	DrawConnState();
}

void TBlueTooth::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		for(int i=0; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		DrawFixText();
		DrawConnState();
		DrawSetting();
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			Check4Selected();
		}
	}
}

void TBlueTooth::DrawConnState()
{
	if(LanguageEnCn==0){
		if(State.DeviceOn){
			TextRender_string24(Lv.Grid.Left[0] + 120, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, "设备已挂载,");	
			if(State.Connection){
				TextRender_string24(Lv.Grid.Left[0] + 250, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, "已连接");
			}else{
				TextRender_string24(Lv.Grid.Left[0] + 260, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, "未连接");
			}
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 120, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, "设备未挂载 ");
			TextRender_string24(Lv.Grid.Left[0] + 250, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, "      ");
		}
	}else{
		if(State.DeviceOn){
			TextRender_string24(Lv.Grid.Left[0] + 460, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, 	"Device Found    ");
			if(State.Connection){
				TextRender_string24(Lv.Grid.Left[0] + 680, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, 	"Connected");
			}else{
				TextRender_string24(Lv.Grid.Left[0] + 680, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, 		"Unconnect");
			}
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 460, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, 		"Device Not Found");
			TextRender_string24(Lv.Grid.Left[0] + 680, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, 	"         ");
		}
	}
}


//______________________________________________________________________________________________________________________________
void TBlueTooth::ClearSelected(void)
{
	switch(CtlIndex){
		case 0:	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "开启", 38, clNearBlack); 
				else 				TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "Enable", 26, clNearBlack);
		
			break;
		case 1:	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "关闭", 38, clNearBlack); 
				else 				TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "Disable", 26, clNearBlack); 
		
			break;
	
		case 2:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 3:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		
		case 4:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 5:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		
		case 6:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[3]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 7:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[3]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		default:

			break;
	}		
}

void TBlueTooth::Check4Selected(void)
{
	switch(CtlIndex){
		case 0:	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, "开启", 38, clNearWhite); 
				else				TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, "Enable", 26, clNearWhite);
			break;
		case 1:	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, "关闭", 38, clNearWhite); 
				else 				TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, "Disable", 26, clNearWhite); 
			break;
	
		case 2:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 3:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		
		case 4:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 5:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		
		case 6:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[3]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 7:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[3]+40, dWWidth, dWHeigh, 4, clNearWhite); break;

		default:

			break;
	}		
}

void TBlueTooth::PermissionCmdReturnDraw()
{
	RectFillRender(0, Lv.Grid.Top[1], 799, Lv.Grid.Bottom[2], ContentColor);
	for(int i=1; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	DrawFixText();
	DrawSetting();
	Check4Selected();
}

void TBlueTooth::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Go Active
		IsOnGoing = 1;
	}else
	if(PermissionCmd == 2){
		//Do Close
		IsOnGoing = 0;
	}
}

void TBlueTooth::DrawSetting()
{
	if(OnOff.Share){
		TRoundSquare::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xFirst,   Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Cmsn){
		TRoundSquare::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xSec,   	Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.ReadOnly){
		TRoundSquare::sDrawBySize(xFirst, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xSec,   	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else{
		TRoundSquare::sDrawBySize(xFirst, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xSec,   	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}
	if(OnOff.Mute){
		TRoundSquare::sDrawBySize(xFirst, 	Lv.Grid.Top[3] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xFirst,   Lv.Grid.Top[3] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Reset){
		TRoundSquare::sDrawBySize(xSec, 	Lv.Grid.Top[3] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xSec,   	Lv.Grid.Top[3] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TBlueTooth::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned int aAddr, HadPrc;
	TDMix aFullPath;

	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}else{
		switch(aKey){
			case VK_RIGHT:
				switch(CtlIndex){
					case 0:
					case 2:
					case 4:
					case 6:
						ClearSelected();
						CtlIndex++;
						Check4Selected();
						break;
					default:
						break;
				}
				break;				
			case VK_LEFT:
				switch(CtlIndex){
					case 1:
					case 3:
					case 5:
					case 7:
						ClearSelected();
						CtlIndex--;
						Check4Selected();
						break;
					default:
						break;
				}
				break;
			case VK_UP:
				if(CtlIndex > 1){
					ClearSelected();
					CtlIndex-=2;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex < 6){
					ClearSelected();
					CtlIndex+=2;
					Check4Selected();
				}
				break;
			case  dfKEY_SETUP:
				if(CtlIndex == 2){
					OnOff.Share = OnOff.Share ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 3){
					OnOff.Cmsn = OnOff.Cmsn ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 4){
					OnOff.ReadOnly = OnOff.ReadOnly ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 5){
					OnOff.ReadOnly = OnOff.ReadOnly ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 6){
					OnOff.Mute = OnOff.Mute ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 7){
					OnOff.Reset = OnOff.Reset ? 0 : 1;
					DrawSetting();
				}
				break;
			case VK_TAB:
				
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					//Active
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Top[1] + 12, 600 , 120, 3);
					}
				}else
				if(CtlIndex == 1){
					//Close
					PermissionCmd = 2;
					PermissionCmdGO();
				}
				if(CtlIndex == 2){
					OnOff.Share = OnOff.Share ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 3){
					OnOff.Cmsn = OnOff.Cmsn ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 4){
					OnOff.ReadOnly = OnOff.ReadOnly ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 5){
					OnOff.ReadOnly = OnOff.ReadOnly ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 6){
					OnOff.Mute = OnOff.Mute ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 7){
					OnOff.Reset = OnOff.Reset ? 0 : 1;
					DrawSetting();
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

void TBlueTooth::sRecoverSetDefault()
{
	
	SysLoopTick = 0;
}

int TBlueTooth::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	return 0;
}

void TBlueTooth::Task1000Ms()
{
	
}






