#include "app_connect_manage.h"
#include "bsp_time.h"
#include "system.h"
#include "ble_comm.h"
#include "app_flash.h"
#include "app_charge.h"
#include "app_ota.h"
#include "exception.h"
#include "hal_ble_client.h"
#include "hal_ble_host.h"
#include "hal_led.h"
#include "app_self_checking.h"
#include "hal_ser_imu_mode_manage.h"
#include "exception.h"
#include "hal_mt.h"
#include "hal_battery.h"

/********************** 变量区 *************************/
#define Ble_update_connted_Error 60//一次最多更新蓝牙连接间隔 次数

enum{
	shoes_
};

/********************** 函数声明区 *************************/
extern uint8_t app_client_step_GetIsScan(void);

BLE_Client_Tx_t mBLE_Client_T_CONNET_R = {
	.n = 10,
	.t = 500,
	.cb = 0,
};

static uint16_t LedPlaytime =0;
static uint32_t LedBlinktim =0;

static void cb_BLE_BLINK_LED(void* handle)
{	
	BLE_Client_Rx_t* target = handle;
	uint8_t buf[2]={0x0};

	buf[0] = target->pDat[0];
	buf[1] = target->pDat[1];
	
  LedPlaytime = ((target->pDat[0]<<8) + target->pDat[1]);
	SEGGER_RTT_printf(0,"=======>cb_BLE_BLINK_LED,displaytime:%d\n",LedPlaytime);
	BLE_Host_Tx_Send(0,BLE_BLINK_LED,buf,2); 
	LedBlinktim = TIME_GetTicks();
	if(GetBatteryPersent()>20){
			LED_Start(LED_OVERTURN,COLOR_GREEN);
	}else{
			LED_Start(LED_OVERTURN,COLOR_ORANGE);
	}
}

static uint8_t app_conneted_getRight(void){
	uint8_t state =BLE_RIGHT_DISCONNTED;
	if('C' != mFlash.mClient.isConfig)
		 state =BLE_RIGHT_NOCONFIG;
	else if(host_isconnect())
		 state =BLE_RIGHT_CONNTED;
	return state;
}

void app_connect_LED_Process(void)
{
	static uint8_t ResportCs_Flag = 0;
	static uint8_t state =0;
	static uint8_t host_state=0;
	switch(state){
		case 0:
			    if(slave_isconnect()){
						state =1;
						ResportCs_Flag = 0;
					}
			    break;
		case 1:
			    if(host_isconnect()){ 
							if(0 == ResportCs_Flag){			
								ResportCs_Flag = 1;
								host_state = app_conneted_getRight();
								BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_CONNET_R,&host_state,1);
								SEGGER_RTT_printf(0,"====================>BLE_Host_T_CONNET_LED:%d\n",TIME_GetTicks());
							}
					}
					else if(ResportCs_Flag > 0)	{
						ResportCs_Flag = 0;
						BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
					}
					
					if(LedPlaytime !=0){
						 if(TIME_GetTicks() - LedBlinktim >=LedPlaytime){
							 LedPlaytime =0;
							 LED_Stop(LED_OVERTURN);
						 }
					}
					if(!slave_isconnect()){
						state =0;
						BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
					}
			    break;
		default:
			    break;
	}
}

void cb_Slave_Connect(void)
{
	SEGGER_RTT_printf(0,"=======>cb_Slave_Connect\n");
	if(mFlash.isHost) slave_update_conn_interval_request(30,30);
}

void cb_Host_Connect(void)
{
	SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
}

void app_connect_Process(void)
{
	static uint8_t state = 0;
	static uint32_t cnt = 0;
	static uint16_t  update_temp = 0;
	static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
	
		switch(state){
			case 0:{
				if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
					SEGGER_RTT_printf(0,"=======>scan_start\n");
					Process_SetHoldOn(app_connect_Process,1);
					scan_start();
					cnt = 0;
					state = 1;
				}
				break;}
			case 1:{
				if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_CHARGE_PULLOUT)){
					Except_TxError(EXCEPT_DFU,"adv disconnted over time");
					SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
					nrf_ble_scan_stop();
					Process_SetHoldOn(app_connect_Process,0);
					state = 0;
				}else if(host_isconnect()){
					SEGGER_RTT_printf(0,"=======>host_isconnect\n");
					Process_SetHoldOn(app_connect_Process,0);
					if(mFlash.isHost){
						update_temp =0;
						ble_con_int = 0;
					}
					state =2;
				}
				if(++cnt>=100){ if(0 == app_client_step_GetIsScan() && 'C' == mFlash.mClient.isConfig)Except_TxError(EXCEPT_CONNET,"scan right shoes over time");
					SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
					nrf_ble_scan_stop();
					Process_SetHoldOn(app_connect_Process,0);
					state = 0;
				}
				break;}
			case 2:{
				if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1){//游戏模式下更新连接间隔
						if(ble_con_int !=2 && update_temp <= Ble_update_connted_Error){
							if(APP_SUCCESS ==  Ble_update_conn_interval(7.5,7.5)){
								ble_con_int =2;
								update_temp  =0;
							}
							else {
							  if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 7.5ms fail");
								++update_temp;
							}
						}
				}else{
						if(ble_con_int !=1 && update_temp <= Ble_update_connted_Error){
							if(APP_SUCCESS ==  Ble_update_conn_interval(100,100)){
								ble_con_int =1;
								update_temp =0;
							}
							else {
							  if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 100ms fail");
								update_temp++;
							}
						}
				}
			
				if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_CHARGE_PULLOUT)){
					if(host_isconnect())host_disconnect();
				}
				if(!host_isconnect()){
					SEGGER_RTT_printf(0,"=======>host_disconnect\n");
					Process_SetHoldOn(app_connect_Process,0);
					state = 0;
				}
				
				break;}
			default:state=0;break;
		}
}

static void cb_BLE_BLE_CONNET_R(void* handle)
{	
	uint8_t host_state=BLE_RIGHT_DISCONNTED;
	host_state =app_conneted_getRight();
	SEGGER_RTT_printf(0,"!!!!app_conneted_getRight:%d\n",host_state);
	BLE_Client_Tx_Send(0,BLE_CONNET_R,&host_state,1);
}

void app_connect_manage_Init(void)
{
	if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
	Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
	Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
	Process_Start(100,"app_connect_led",app_connect_LED_Process);
	BLE_Client_Rx_Regist(BLE_CONNET_R,cb_BLE_BLE_CONNET_R);
	BLE_Client_Rx_Regist(BLE_BLINK_LED,cb_BLE_BLINK_LED);
}


