#include "app_game.h"
#include "nrf_gpio.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"
#include "ble_comm.h"

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

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

static game_manager_t game_manager ={0};

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

void cb_BLE_Client_R_GAMEMODE(void* handle)
{
	BLE_Client_Rx_t* target = handle;
	game_manager.isGameMode = target->pDat[0];
	if(game_manager.isGameMode){
		hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
		game_manager.GameModeHeartCnt =0;
	}else{
		hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
	}
	
	switch(game_manager.isGameMode)
	{
		case 1:
			IMU_Dtalige_Rowdata_OFF();
			break;
		case 2:
			IMU_Dtalige_Rowdata_ON();
			break;
	}
	
	BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.isGameMode,1);
	SEGGER_RTT_printf(0,">>>>>>>>>>cb_BLE_Client_R_GAMEMODE:%d\r\n",game_manager.isGameMode);
}

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.GameModeHeartCnt);
		if(game_manager.GameModeHeartCnt++ >= 5){
			 game_manager.isGameMode = 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.isGameMode,1); 
		}
	}
}

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_imu_gamemode = 0;
	
	if(game_manager.clientCnt>0) game_manager.clientCnt--;
	switch(state_imu_gamemode){
		case 0:
			    if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) == -1){
						 if(1 == game_manager.isGameMode){
								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;
									Except_TxError(EXCEPT_GAME,"shoes into game mode fail");
								}
						 }
						 if(game_manager.clientCnt >0 && mFlash.isHost) app_game_notify_host(0);
					}
					else state_imu_gamemode =1;
			    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;
							 game_manager.right_discnt =0;
						 }
						 else{
							 if(game_manager.right_discnt++ >10){//与右鞋断开10秒后，退出游戏模式
									game_manager.isGameMode = 0;
									errCnt =0;
								  state_imu_gamemode =0;
									hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
								  
									Except_TxError(EXCEPT_GAME,"In game mode,right shoes disconnted long time");
							 }
						 }
			    }
			
					if(0 == game_manager.isGameMode){
							errCnt =0;
							state_imu_gamemode =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);
					}
					
					 //与主机断开5秒后，退出游戏模式
					 if(!slave_isconnect()){
							if(game_manager.client_discnt++ >5){
								game_manager.isGameMode = 0;
								errCnt =0;
								state_imu_gamemode =0;
								hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
								Except_TxError(EXCEPT_GAME,"In game mode,client disconnted long time");
							}
					 }
					 else game_manager.client_discnt =0;
			     break;
	  }
}

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