#include "app_game.h"
#include "usr_config.h"
#include "bsp_time.h"
#include "system.h"
#include "hal_mt.h"
#include "hal_battery.h"
#include "app_charge.h"
#include "hal_ble_client.h"
#include "hal_ble_host.h"
#include "nrf_delay.h"
#include "app_flash.h"
#include "ble_comm.h"
#include "exception.h"
#include "hal_ser_imu_mode_manage.h"

/********************** 函数声明区 *************************/

typedef struct game_manager{
 uint8_t clientCnt;       //右鞋游戏模式的标志位
 uint8_t right_discnt;    //右鞋断开连接计数器
 uint8_t client_discnt;   //主机连接断开计数器
 uint8_t Signal;      //收到主机的游戏指令
 uint8_t HeartCnt;//游戏模式心跳维护,左鞋
}game_manager_t;

static game_manager_t game_manager ={0};

void app_game_SetClientGameMode(void)
{
	if(game_manager.clientCnt != 3) game_manager.clientCnt = 3;
}

static void cb_BLE_Client_R_GAMEMODE(void* handle)
{
	BLE_Client_Rx_t* target = handle;
	game_manager.Signal = target->pDat[0];
	if(game_manager.Signal){
		hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
		//是否上传原始数据
		if(1 == game_manager.Signal)IMU_Dtalige_Rowdata_OFF();
		else if(2 == game_manager.Signal)IMU_Dtalige_Rowdata_ON();
	}else{
		hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
	}
	
	BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.Signal,1);
	SEGGER_RTT_printf(0,">>>>>>>>>>cb_BLE_Client_R_GAMEMODE:%d\r\n",game_manager.Signal);
}

static void app_game_notify_host(uint8_t mode){
	uint8_t clientMode = mode;
	BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
}

static void app_game_Process(void)
{
	static uint8_t errCnt = 0;
  static uint8_t state = 0;
	
	if(game_manager.clientCnt>0) game_manager.clientCnt--;
	switch(state){
		case 0:
			    if(game_manager.Signal){
						if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) == -1){
							hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
							if(++errCnt >= 20){errCnt =0;
								game_manager.Signal =0;
								Except_TxError(EXCEPT_GAME,"shoes into game mode fail");
							}
						}
						else state =1;
						if(0 != game_manager.HeartCnt)game_manager.HeartCnt =0;
					}
					if(game_manager.clientCnt >0 && mFlash.isHost) app_game_notify_host(0);
			    break;
		case 1:
			   if(mFlash.isHost){//管理右鞋状态
						 if(host_isconnect()){
							 if(0 == game_manager.clientCnt){
									app_game_notify_host(1);
									if(++errCnt >= 10){errCnt =0;
										SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
										Except_TxError(EXCEPT_GAME,"no get right shoes data");
									}
							 }
							 else if(errCnt>0) errCnt = 0;
							 if(0 != game_manager.right_discnt)game_manager.right_discnt =0;
						 }
						 else game_manager.right_discnt++;
					}
				  
			    if(0 == slave_isconnect())game_manager.client_discnt++;
					else if(0 != game_manager.client_discnt)game_manager.client_discnt =0;
						
					if(0 == game_manager.Signal  ||      //收到退出游戏的指令
						game_manager.client_discnt > 5 ||  //与主机断开5秒后，退出游戏模式
					  game_manager.right_discnt  > 10    //与右鞋断开10秒后，退出游戏模式
					){
						  if(game_manager.right_discnt > 10)Except_TxError(EXCEPT_GAME,"In game mode,right shoes disconnted long time");
						  if(game_manager.client_discnt > 5)Except_TxError(EXCEPT_GAME,"In game mode,client disconnted long time");
							errCnt =0;
							state =0;
						  game_manager.Signal = 0;
						  game_manager.client_discnt =0;
						  game_manager.right_discnt =0;
							hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
							if(mFlash.isHost)app_game_notify_host(0);
					}
			    break;
		default: state =0;game_manager.Signal = 0;game_manager.client_discnt =0;break;
	  }
}

static void app_AutoOutgame_Process(void){
	if(mFlash.isHost && (hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1)){
		SEGGER_RTT_printf(0,"game_manager.GameModeHeartCnt:%d\r\n",game_manager.HeartCnt);
		if(game_manager.HeartCnt++ >= 5){
			 game_manager.Signal = 0;
			 hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
			 BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.Signal,1); 
		}
	}
}

void app_game_Init(void)
{
	Process_Start(1000,"app_game",app_game_Process);
	BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
	Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
}
