#include "Config/Include/APP_Config.h"
#include "Abstraction/Include/OS_Interface.h"
#include "Abstraction/Driver/RTC/rtc.h"
#include "Driver/Wkup/Wakeup.h"
#include "ShareTask.h"
#include <string.h>

static OS_TaskParam Param;

static void on_task_init()
{
	uint32_t wakeUpSource = 0;
	rtc_status_t rtcRet = RTC_SET_TIME_NG;
	rtc_timedate_t timeDate;

	memset(&timeDate, 0x0, sizeof(timeDate));
	// Read Alarm Kind and start time from eeprom

	wakeUpSource = GetWakeUpSrc();

	if(wakeUpSource == 0 || wakeUpSource == 2048)	//KL30
	{
		rtc_Init();	//int rtc driver
		rtc_SetTimeDate(&rtcTimer1_StartTime0);	//set default time
		rtc_StartCounter();	//start counter
	}
	else
	{
		//nothing to do from now on
	}
}

static void on_recv_msg(uint16_t event_code, void* data, uint32_t data_size)
{
	(void)data_size;

	switch(event_code)
	{
		case EV_RTC_SPI_MPU_GET_RTC_DATE_REQ:
			ShareTask_getRTCDate();
			break;
		case EV_RTC_SPI_MPU_SET_RTC_DATE_REQ:
			ShareTask_setRTCDate(data);
			break;
		default:
			break;
	}
}

static void on_recv_time_out()
{
	//TODO
}

static void on_loop()
{
	//TODO
}

void* ShareTask_getTaskParam()
{
    Param.queue_id = MSG_ID_SHARETASK;
    Param.wait_time = 500;  //ms
    Param.task_init = on_task_init;
    Param.recv_msg = on_recv_msg;
    Param.time_out = on_recv_time_out;
    Param.loop = on_loop;
    return &Param;
}

// *********************************************************************************************************************
//! Function handles Getting the RTC date by GNSS timer.
//! @param[in] timeDate   time data
//! @return : RTC_SET_TIME_OK: successful
//! 		  RTC_SET_TIME_NG: failed
// *********************************************************************************************************************
void ShareTask_getRTCDate(void)
{
	uint8_t aubMsgBuffer[RTCAPP_RTC_DATE_LEN] = {0};
	OS_Error sendRet = OS_ERROR_OK;
	rtc_timedate_t timeDate;

	memset(&timeDate, 0, sizeof(rtc_timedate_t));

	//get rtc time from driver
	rtc_GetTimeDate(&timeDate);

	//set RTC data to sending buffer
	aubMsgBuffer[0] = (uint8_t)((timeDate.year & 0xFF00) >> 8);	//year high
	aubMsgBuffer[1] = (uint8_t)(timeDate.year & 0x00FF);			//year low
	aubMsgBuffer[2] = (timeDate.month & 0x00FF);		//month
	aubMsgBuffer[3] = (timeDate.day & 0x00FF);			//day
	aubMsgBuffer[4] = (timeDate.hour & 0x00FF);			//hour
	aubMsgBuffer[5] = (timeDate.minutes & 0x00FF);		//minutes
	aubMsgBuffer[6] = (timeDate.seconds & 0x00FF);		//second
	aubMsgBuffer[7] = (uint8_t)((timeDate.milliseconds & 0xFF00) >> 8); //millisecond high
	aubMsgBuffer[8] = (uint8_t)(timeDate.milliseconds & 0x00FF);		//millisecond low
#if 0
	JPCC_DEBUG(JPCC_LOG_LEVEL_ERROR, "[ Rtc ][%s:%d] [%d]/[%d]/[%d] [%d]:[%d]:[%d].[%d]",
	                       			__FUNCTION__, __LINE__, timeDate.year, timeDate.month, timeDate.day, timeDate.hour,
	                       			timeDate.minutes, timeDate.seconds, timeDate.milliseconds);
#endif
	//Send response to MPU
	//nubCommMgr_SendMessage(EV_RTC_SPI_MCU_GET_RTC_DATE_RSP, (void *)aubMsgBuffer, RTCAPP_RTC_DATE_LEN);
	sendRet = OS_sndMsg(MSG_ID_SPIMANAGER, EV_RTC_SPI_MCU_GET_RTC_DATE_RSP, (void *)aubMsgBuffer, RTCAPP_RTC_DATE_LEN, OS_WAIT_INFINITE);
	if(sendRet != OS_ERROR_OK)
	{
		//retry and print error code
	}
}

// *********************************************************************************************************************
//! Function handles Setting the RTC date by GNSS timer.
//! @param[in] timeDate   time data
//! @return : RTC_SET_TIME_OK: successful
//! 		  RTC_SET_TIME_NG: failed
// *********************************************************************************************************************
void ShareTask_setRTCDate(uint8_t *RTCDate)
{
	rtc_status_t aubRet = 0;
	OS_Error sendRet = OS_ERROR_OK;
	rtc_timedate_t timeDate;
	uint8_t aubProcRet = 0;

	memset(&timeDate, 0, sizeof(rtc_timedate_t));

	/* Determine the validity of the input parameters */
	if (NULL == RTCDate)
	{
		//Input Error, Nothing to do, return
	}
	else
	{
		//get RTC date from input buffer
		timeDate.year = (uint32_t)(RTCDate[0] << 8) + RTCDate[1];	//year high shift right 1byte + year low
		timeDate.month = RTCDate[2];		//month
		timeDate.day = RTCDate[3];			//day
		timeDate.hour = RTCDate[4];			//hour
		timeDate.minutes = RTCDate[5];		//minutes
		timeDate.seconds = RTCDate[6];		//second
		timeDate.milliseconds = (uint32_t)(RTCDate[7] << 8) + (uint32_t)RTCDate[8];
		//timeTrustLevel = RTCDate[9];		//time trust level(0:soc default date ; 1:network date ; 2:GPS date)

		/* Setting data by the RTC driver */
		aubRet = rtc_SetTimeDate(&timeDate);
		if (RTC_SET_TIME_OK == aubRet)
		{
			aubProcRet = 0;	//OK
		}
		else
		{
			aubProcRet = 1;	//NG
		}
#if 0
		JPCC_DEBUG(JPCC_LOG_LEVEL_ERROR, "[ Rtc ][%s:%d]%d/%d/%d %d:%d:%d.%d, SetVal=[%d], trust level=[%d]",
	                       			 __FUNCTION__, __LINE__, timeDate.year, timeDate.month, timeDate.day,
	                       			 timeDate.hour, timeDate.minutes, timeDate.seconds, timeDate.milliseconds, aubProcRet, gTimeTrustLevel);
#endif
	}

	//Send process result to MPU
	//nubCommMgr_SendMessage(EV_RTC_SPI_MCU_SET_RTC_TIME_RSP, (void *)&aubProcRet, sizeof(Type_uByte));
	sendRet = OS_sndMsg(MSG_ID_SPIMANAGER, EV_RTC_SPI_MCU_SET_RTC_TIME_RSP, (void *)aubProcRet, sizeof(uint8_t), OS_WAIT_INFINITE);
	if(sendRet != OS_ERROR_OK)
	{
		//retry and print error code
	}
}

