/******************************************************************************
 * FunctionName : esptouch_FailCb
 * Description  : esp-touch fail callback
*******************************************************************************/
void ICACHE_FLASH_ATTR
    esptouch_FailCb(void* data)
{   
    wifi_station_disconnect();
    smartconfig_stop();
    //wifi_set_opmode(STATIONAP_MODE);
    
    SC_INFO("ESP-TOUCH FAIL \r\n");
    os_timer_disarm(&esptouch_tout_t);
    #if LIGHT_DEVICE
    light_shadeStart(HINT_RED,2000,0,1,NULL);
    #endif
    
    SC_INFO("ENABLE LIGHT ACTION(ESP-NOW)");
    os_printf("debug: channel:%d\r\n",wifi_get_channel());
#if ESP_NOW_SUPPORT
    light_EspnowInit();
#endif

#if ESP_MESH_SUPPORT
    SC_INFO("RESTART MESH NOW...\r\n");
    #if LIGHT_DEVICE
    light_hint_stop(HINT_RED);
    #endif
    //user_MeshStart();
    wifi_RestartMeshScan(5000);
#else
	SC_INFO("ESP-TOUCH FAIL, OPEN WEBSERVER NOW");
	mesh_SetSoftap();//check
#endif
}
/******************************************************************************
 * FunctionName : esptouch_SuccessCb
 * Description  : esp-touch success callback
*******************************************************************************/
void ICACHE_FLASH_ATTR
    esptouch_SuccessCb(void* data)
{
    wifi_set_opmode(STATIONAP_MODE);
    
    os_timer_disarm(&esptouch_tout_t);//disable check timeout 
    #if LIGHT_DEVICE
    light_hint_stop(HINT_WHITE);
    #endif
    SC_INFO("ESP-TOUCH SUCCESS \r\n");
    
    SC_INFO("ENABLE LIGHT ACTION(ESP-NOW)");
    SC_INFO("debug: channel:%d\r\n",wifi_get_channel());
#if ESP_MESH_SUPPORT
    if(MESH_DISABLE == espconn_mesh_get_status()){
        //user_MeshStart();
        //mesh_enable_task();
    }
    user_esp_platform_connect_ap_cb();
    //user_MeshStart();
#else
    user_esp_platform_connect_ap_cb();
#endif
#if ESP_NOW_SUPPORT
    light_EspnowInit();
#endif
    SC_INFO("CONNECTED TO AP...ENABLE MESH AND RUN PLATFORM CODE ...WAIT...\r\n");

}
void ICACHE_FLASH_ATTR
sp_LightPairState(void)
{

#if 1
    switch(light_state){
        case (SP_LIGHT_IDLE):
            os_printf("status:SP_LIGHT_IDLE\n");
			//pair start command, correct
            if(USER_PBULIC_BUTTON_INFO==buttonPairingInfo.simple_pair_state){
                os_printf("statue:Get button Info,next wait button request\n");
				sp_LightPairTimerStart();
                sp_LightPairAccept();
                light_state=SP_LIGHT_WAIT_BUTTON_REQUEST;
				light_shadeStart(HINT_WHITE,500,3,1,NULL);
            }
			//exception
            else{
                light_state=SP_LIGHT_ERROR_HANDLE;
				light_hint_stop(HINT_RED);
                os_printf("buttonPairingInfo.simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break;
        case (SP_LIGHT_WAIT_BUTTON_REQUEST):
            os_printf("status:SP_LIGHT_WAIT_BUTTON_REQUEST\n");
			light_shadeStart(HINT_GREEN,500,3,0,NULL);
			
			//receive button pair request, send req to phone
            if(LIGHT_RECV_BUTTON_REQUSET==buttonPairingInfo.simple_pair_state){
                os_printf("statue:Get button request,next wait user permit or refuse\n"); 
                sp_LightPairRequestPermission();
                light_state=SP_LIGHT_WAIT_USER_INDICATE_PERMIT;
            }
			//receive pair start command again before timeout , right now, restart state machine
			else if(USER_PBULIC_BUTTON_INFO==buttonPairingInfo.simple_pair_state){
                os_printf("statue:Get button Info,restart state machine,wait button request\n");
				sp_LightPairTimerStart();
                sp_LightPairAccept();
                light_state=SP_LIGHT_WAIT_BUTTON_REQUEST;
            }
			//error if other states
			else{
                //simple_pair_deinit();
                light_state=SP_LIGHT_ERROR_HANDLE;
				light_hint_stop(HINT_RED);
                os_printf("err in SP_LIGHT_WAIT_BUTTON_REQUEST ;simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break;
        case (SP_LIGHT_WAIT_USER_INDICATE_PERMIT):
			light_shadeStart(HINT_BLUE,500,3,0,NULL);
            os_printf("status:SP_LIGHT_WAIT_USER_INDICATE_PERMIT\n");
			//phone user permit pairing
            if(USER_PERMIT_SIMPLE_PAIR==buttonPairingInfo.simple_pair_state){
                os_printf("statue:User permit simple pair,next wait simple result\n"); 
				sp_PairedDevRmAll(&PairedDev);//
                sp_LightPairStart();
                light_state=SP_LIGHT_WAIT_SIMPLE_PAIR_RESULT;
            }
			//phone user refuse pairing, END
			else if(USER_REFUSE_SIMPLE_PAIR==buttonPairingInfo.simple_pair_state){
                os_printf("statue:User refuse simple pair,state clear\n");
                //check state
                sp_LightPairRefuse();
                //light_state=SP_LIGHT_IDLE;
                //buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
                light_state = SP_LIGHT_END;
				light_shadeStart(HINT_RED,500,2,0,NULL);
				sp_LightPairState();
				
            }
			//exceptions
			else{
                //simple_pair_deinit();
                light_state=SP_LIGHT_ERROR_HANDLE;
				light_hint_stop(HINT_RED);
                os_printf("buttonPairingInfo.simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break;
        case (SP_LIGHT_WAIT_SIMPLE_PAIR_RESULT):
            os_printf("status:SP_LIGHT_WAIT_SIMPLE_PAIR_RESULT\n");
			//pairing finished , END
            if(LIGHT_SIMPLE_PAIR_SUCCED==buttonPairingInfo.simple_pair_state){
                os_printf("status:sp_LightPairSucced\n");  
				light_shadeStart(HINT_WHITE,500,2,0,NULL);

				//1.get key
				//buttonPairingInfo.espnowKey
				//2.add peer and save
            	int res = esp_now_add_peer((uint8*)(buttonPairingInfo.button_mac), (uint8)ESP_NOW_ROLE_CONTROLLER,(uint8)wifi_get_channel(), (uint8*)(buttonPairingInfo.espnowKey), (uint8)ESPNOW_KEY_LEN);
            	os_printf("INIT RES: %d ; MAC:"MACSTR"\r\n",res,MAC2STR(((uint8*)(buttonPairingInfo.button_mac))));
				//3.save
                sp_AddPairedDev(&PairedDev,buttonPairingInfo.button_mac,buttonPairingInfo.espnowKey,wifi_get_channel());
                sp_DispPairedDev(&PairedDev);

                //simple_pair_deinit();//check
                //light_state=SP_LIGHT_IDLE;
                //buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
                sp_LightPairReportResult(true);
				light_state = SP_LIGHT_END;
				sp_LightPairState();
            }
			//pairing failed , END
			else if(LIGHT_SIMPLE_PAIR_FAIL==buttonPairingInfo.simple_pair_state){
                os_printf("status:sp_LightPairFail\n");
                //simple_pair_deinit();//check
                //light_state=SP_LIGHT_IDLE;
                //buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
                sp_LightPairReportResult(false);
				light_state = SP_LIGHT_END;
				light_hint_stop(HINT_RED);
				sp_LightPairState();
            }
			//exception
			else{
                simple_pair_deinit();
				light_hint_stop(HINT_RED);
                light_state=SP_LIGHT_ERROR_HANDLE;
                os_printf("buttonPairingInfo.simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break; 
			
		case (SP_LIGHT_ERROR_HANDLE):
			os_printf("status:simpaire in Err!!!\r\nBACK TO IDLE\r\n"); 
			light_hint_stop(HINT_RED);
        case (SP_LIGHT_END):
			//reset state, deinit simplepair, stop tout timer
			sp_LightPairTimerStop();
			simple_pair_deinit();
			light_state=SP_LIGHT_IDLE;
			buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
			sp_PairedDevLoadAll(&PairedDev);
			break;
			  
        default:
            os_printf("status:unsafe param: %d\n",light_state);
            break;
    }

#endif
}