#include "system.h"
#include "app_client_step.h"
#include "app_flash.h"
#include "bsp_time.h"
#include "system.h"
#include "app_host.h"
#include "app_step.h"
#include "ble_comm.h"
#include "app_math.h"
#include "app_flash.h"

/************************ 函数定义 ***************************/
void cb_BLE_Client_T_UPDATE_STEPNUM(void* handle);

/************************ 获取步数 ***************************/

static uint8_t isScan = 0;

uint8_t app_client_step_GetIsScan(void)
{
	return isScan;
}

void app_client_step_SetIsScan(void)
{
	isScan =1;
}

//一小时保存一次步数
static void app_client_step_Process(void)
{
  static uint8_t state =0;
	static uint32_t tim =0;
	switch(state){
		case 0:
			    if(1 == isScan){
						state =1;
						tim = TIME_GetTicks();
					}
			    break;
	  case 1:
			    if(TIME_GetTicks() - tim >= 10000)state =2;
				  else app_host_GetClientData(1);
					break;
		case 2:
		      if(Flash_SaveStep() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_DATEStep,"save step fail");
		      isScan = 0;
		      state =0;
		      DEBUG_LOG("Flash_SaveInfomation:%d,mFlash.mStep.stepCur[1]:%d\r\n",TIME_GetTicks(),mFlash.mStep.stepCur[1]);
			    break;
		default:state =0;isScan = 0;break; 
	}
}

/************************ 删除步数 ***************************/
void app_client_DeleteStep(BLE_Client_Rx_t* target)
{
	uint8_t i =0;
	for(i=0;i<8;i++) mFlash.mStep.startTime[i] = target->pDat[i+1];
	app_math_calit_time(target->pDat[9]);
	if(0 != mFlash.mStep.step_num){mFlash.mStep.step_num = 0;
		 flash_SetClearStepFlag();
	}
}

/************************ 发送步数 ***************************/
#define PACK_STEP_LEN 40		//发送 PACK_STEP_LEN*4 个字节

static uint8_t isSendAll = 0;
static uint16_t packDex = 0;
static uint16_t packNum = 0;
static uint16_t lastNum = 0;

BLE_Client_Tx_t mSendStep = {
	.n = 1,
	.t = 1,
	.cb = cb_BLE_Client_T_UPDATE_STEPNUM,
};

void app_client_SendStepAll(uint16_t n)
{
	static uint8_t buf[250];
	uint8_t L=0;
	
	uint32_t addr = (flash_GetStep_StartAddress()+PACK_STEP_LEN*4*(n-1));
	buf[L++] = BLE_UPDATE_STEPNUM;
	for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
	buf[L++] = (uint8_t)(packNum>>8);
	buf[L++] = (uint8_t)(packNum>>0);
	buf[L++] = (uint8_t)(n>>8);
	buf[L++] = (uint8_t)(n>>0);
	if(n==0){
		uint32_t step = app_step_GetStep_L() + app_step_GetStep_R();
//		DEBUG_LOG("====>step=%d,app_step_GetStep_L()=%d,app_step_GetStep_R()=%d\n",step,app_step_GetStep_L(),app_step_GetStep_R());
		buf[L++] = (uint8_t)(step>>24);
		buf[L++] = (uint8_t)(step>>16);
		buf[L++] = (uint8_t)(step>>8);
		buf[L++] = (uint8_t)(step>>0);
	}else if(n==packNum){
		flash_Data_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
	}else{
		flash_Data_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
	}
//	DEBUG_LOG("====>lastNum=%d,PACK_STEP_LEN=%d\n",lastNum,PACK_STEP_LEN);
//	DEBUG_LOG("app_client_SendStepAll:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
	BLE_Client_Tx_Send(&mSendStep,BLE_UPDATE,buf,L);
}

void app_client_SendStepN(uint16_t n)
{
	uint8_t buf[250];
	uint8_t L=0;
	
	uint32_t addr = (flash_GetStep_StartAddress()+PACK_STEP_LEN*4*(n-1));
	buf[L++] = BLE_UPDATE_STEPNUM;
	for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
	
	buf[L++] = (uint8_t)(packNum>>8);
	buf[L++] = (uint8_t)(packNum>>0);
	buf[L++] = (uint8_t)(n>>8);
	buf[L++] = (uint8_t)(n>>0);
	if(n == packNum){
		flash_Data_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
	}else{
		flash_Data_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
	}
//	DEBUG_LOG("app_client_SendStepN:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
	BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
}

void app_client_SendStep(BLE_Client_Rx_t* target)
{
	uint16_t n = ((uint16_t)target->pDat[1]<<8)|((uint16_t)target->pDat[2]<<0);
	
	if(mFlash.mStep.step_num > 0){
		packNum = ((mFlash.mStep.step_num-1)/PACK_STEP_LEN)+1;
		lastNum = ((mFlash.mStep.step_num-1)%PACK_STEP_LEN)+1;
	}else{
		packNum = 0;
		lastNum = 0;
	}
	
//	DEBUG_LOG("====>mFlash.mStep.step_num=%d,packNum=%d,lastNum=%d,nnnn=%d\n",mFlash.mStep.step_num,packNum,lastNum,n);
	
	if(n==0){
		isSendAll = 1;
		packDex = 0;
		app_client_SendStepAll(packDex);
	}else{
		app_client_SendStepN(n);
	}
}

static void cb_BLE_Client_T_UPDATE_STEPNUM(void* handle)
{
	if(isSendAll==0) return;
	if(packDex<packNum){
		if(++packDex>=packNum) isSendAll = 0;
		app_client_SendStepAll(packDex);
	}
}

/************************ 初始化 ***************************/
void app_client_step_Initialize(void)
{
	Process_Start(1000,"app_client_step",app_client_step_Process);
	if(mFlash.mStep.stepCur[0]<mFlash.mStep.step[0])
	mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
	DEBUG_LOG("mFlash.mStep.step_num=%d\n",mFlash.mStep.step_num);
	DEBUG_LOG("mFlash.mStep.stepCur[0]=%d,mFlash.mStep.stepCur[1]=%d\n",mFlash.mStep.stepCur[0],mFlash.mStep.stepCur[1]);
}
