#include "p_apps.h"
#include "p_dsp.h"

static CCHAR _cMonthDay[MONTHS_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void DisplayFileStatus()
{
	XCHAR pBuf[DISPLAY_MAX_CHAR+1];

	p_ltoa(SoloFileGetSize(), pBuf, 10);
	DisplayLine(pBuf, 1);
}

void GetDateTimeDisplay(PXCHAR pBuf, BOOLEAN bLcdDisplay, ULONG lTime)
{
	DSHORT iDay, iSecond, iYear;
	DCHAR iWeek, iMonth, iHour, iMinute, iMonthDay;
	BOOLEAN bLeapYear;
#ifdef _USE_FXS
	PXCHAR_D pCur;
#endif
//	DCHAR i, j;

	bLeapYear = FALSE;

//	Sys_lCurrentTime = 3313821780;	// 2005-01-04
//	Sys_lCurrentTime = 3313476180;	// 2004-12-31
//	Sys_lCurrentTime = 3345012180;	// 2005-12-31
//	Sys_lCurrentTime = 3345098580;	// 2006-01-01
//	Sys_lCurrentTime = 3281940180;	// 2004-01-01

	iWeek = (UCHAR)((lTime/SECONDS_PER_DAY + 1) % DAYS_PER_WEEK);
	lTime -= SECONDS_FIRST_4_YEAR;
	iDay = (USHORT)((lTime/SECONDS_PER_DAY + 1) % DAYS_PER_4_YEAR);
	iYear = (UCHAR)((lTime/SECONDS_PER_DAY + 1) / DAYS_PER_4_YEAR);

//	iWeek = (UCHAR)((Sys_lCurrentTime/SECONDS_PER_DAY + 1) % DAYS_PER_WEEK);
//	Sys_lCurrentTime -= SECONDS_FIRST_4_YEAR;
//	iDay = (USHORT)((Sys_lCurrentTime/SECONDS_PER_DAY + 1) % DAYS_PER_4_YEAR);
//	iYear = (UCHAR)((Sys_lCurrentTime/SECONDS_PER_DAY + 1) / DAYS_PER_4_YEAR);
	iYear *= 4;
	if (iDay > DAYS_IN_YEAR_0)
	{
		if (iDay <= DAYS_IN_YEAR_1)
		{
			iDay -= DAYS_IN_YEAR_0;
			iYear ++;
		}
		else if (iDay <= DAYS_IN_YEAR_2)
		{
			iDay -= DAYS_IN_YEAR_1;
			iYear += 2;
		}
		else
		{
			iDay -= DAYS_IN_YEAR_2;
			iYear += 3;
		}
	}
	else
	{
		bLeapYear = TRUE;
	}
	iYear += 1904;

//	OutputVal(iYear, IDS_CHECK_POINT_3);

	for (iMonth = 0; iMonth < MONTHS_PER_YEAR; iMonth ++)
	{
		iMonthDay = _cMonthDay[iMonth];
		if ((iMonth == 1) && bLeapYear)
		{
			iMonthDay ++;
		}

		if (iDay > iMonthDay)
		{
			iDay -= iMonthDay;
		}
		else
		{
			break;
		}
	}
	iMonth ++;
	iHour = (UCHAR)((lTime % SECONDS_PER_DAY) / SECONDS_PER_HOUR);
	iMinute = (UCHAR)((lTime % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
	iSecond = (UCHAR)(lTime % SECONDS_PER_MINUTE);	

	lTime += SECONDS_FIRST_4_YEAR;

//	iHour = (UCHAR)((Sys_lCurrentTime % SECONDS_PER_DAY) / SECONDS_PER_HOUR);
//	iMinute = (UCHAR)((Sys_lCurrentTime % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
//	iSecond = (UCHAR)(Sys_lCurrentTime % SECONDS_PER_MINUTE);	

//	Sys_lCurrentTime += SECONDS_FIRST_4_YEAR;

	if (bLcdDisplay)
	{
#if defined RES_CN
		pBuf[0] = iMonth/10 + '0';
		pBuf[1] = iMonth%10 + '0';
		pBuf[2] = '/';
		pBuf[3] = iDay/10 + '0';
		pBuf[4] = iDay%10 + '0';
		pBuf[5] = ' ';
		p_memcpy_c2x((PXCHAR)(pBuf+6), g_cWeekDay[iWeek], 3);
		pBuf[9] = ' ';
		pBuf[10] = iHour/10 + '0';
		pBuf[11] = iHour%10 + '0';
		pBuf[12] = ':';
		pBuf[13] = iMinute/10 + '0';
		pBuf[14] = iMinute%10 + '0';
		pBuf[15] = 0;
#else
		p_memcpy_c2x(pBuf, g_cWeekDay[iWeek], 3);
		pBuf[3] = ' ';
		pBuf[4] = iDay/10 + '0';
		pBuf[5] = iDay%10 + '0';
		pBuf[6] = ' ';
		p_memcpy_c2x((PXCHAR)(pBuf+7), g_cMonth[iMonth-1], 3);
		pBuf[10] = ' ';
		pBuf[11] = iHour/10 + '0';
		pBuf[12] = iHour%10 + '0';
		pBuf[13] = ':';
		pBuf[14] = iMinute/10 + '0';
		pBuf[15] = iMinute%10 + '0';
		pBuf[16] = 0;
#endif
	}
	else
	{
#ifdef _USE_FXS
		// Below is for CID's SDMF
/*		iWeek = p_strlen_x(Sys_pPeerDisplay);	// iWeek is used to save the caller number's length

		X_VAL(Sdram_pDataCache, 0) = 4;
		X_VAL(Sdram_pDataCache, 1) = 8 + iWeek;
		X_VAL(Sdram_pDataCache, 2) = iMonth/10 + '0';
		X_VAL(Sdram_pDataCache, 3) = iMonth%10 + '0';
		X_VAL(Sdram_pDataCache, 4) = iDay/10 + '0';
		X_VAL(Sdram_pDataCache, 5) = iDay%10 + '0';
		X_VAL(Sdram_pDataCache, 6) = iHour/10 + '0';
		X_VAL(Sdram_pDataCache, 7) = iHour%10 + '0';
		X_VAL(Sdram_pDataCache, 8) = iMinute/10 + '0';
		X_VAL(Sdram_pDataCache, 9) = iMinute%10 + '0';
		p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+10), Sys_pPeerDisplay, iWeek);
		iWeek += 10;
		iDay = 0;
		for (iMonth = 0; iMonth < iWeek; iMonth ++)	// iMonth is just a loop counter here
		{
			iDay += X_VAL(Sdram_pDataCache, iMonth);		// iDay is used to save the checksum
		}

		iDay %= 256;
		iDay = ~iDay;
		iDay ++;
		X_VAL(Sdram_pDataCache, iWeek) = (UCHAR)iDay;
*/
		// Below is for CID's MDMF
		pCur = (PXCHAR)Sdram_pDataCache;
		pCur += 2;

		pCur[0] = 1;
		pCur[1] = 8;
		pCur[2] = iMonth/10 + '0';
		pCur[3] = iMonth%10 + '0';
		pCur[4] = iDay/10 + '0';
		pCur[5] = iDay%10 + '0';
		pCur[6] = iHour/10 + '0';
		pCur[7] = iHour%10 + '0';
		pCur[8] = iMinute/10 + '0';
		pCur[9] = iMinute%10 + '0';
		pCur += 10;

		iWeek = p_strlen_x(Sys_pPeerDisplay);	// iWeek is used to save the caller number's length
		pCur[0] = 2;
		pCur[1] = iWeek;
		p_memcpy_x2x((PXCHAR)(pCur+2), Sys_pPeerDisplay, iWeek);
		iWeek += 2;
		pCur += iWeek;

		iMonth = p_strlen_x(Sys_pRemoteNumber);	// iWeek is used to save the caller number's length
		pCur[0] = 7;
		pCur[1] = iMonth;
		p_memcpy_x2x((PXCHAR)(pCur+2), Sys_pRemoteNumber, iMonth);
		iMonth += 2;
		pCur += iMonth;

		X_VAL(Sdram_pDataCache, 0) = 128;
		X_VAL(Sdram_pDataCache, 1) = 10 + iWeek + iMonth;

		iWeek += 12 + iMonth;
		iDay = 0;
		for (iMonth = 0; iMonth < iWeek; iMonth ++)	// iMonth is just a loop counter here
		{
			iDay += X_VAL(Sdram_pDataCache, iMonth);		// iDay is used to save the checksum
		}
		iDay %= 256;
		iDay = ~iDay;
		iDay ++;
		X_VAL(Sdram_pDataCache, iWeek) = (UCHAR)iDay;

		// Below is DTMF CID format
/*		pCur = (PXCHAR)Sdram_pDataCache;
		iWeek = p_strlen_x(Sys_pPeerDisplay);	// iWeek is used to save the caller number's length

		pCur[0] = DTMF_SIGNAL_A;
		pCur[1] = DTMF_SIGNAL_QUIET;
		
		for (i = 0, j = 2; i < iWeek; i ++)
		{
			pCur[j++] = Sys_pPeerDisplay[i] - '0';
			pCur[j++] = DTMF_SIGNAL_QUIET;
		}
		pCur[j] = DTMF_SIGNAL_WELL;
*/
#endif
	}
}

void GetTimeDisplay(ULONG lTime, PUCHAR pBuf)
{
	DCHAR i;
	XCHAR iTime[3];
	PXCHAR_D pCur;

	pCur = (PXCHAR)pBuf;
	p_strcpy_c2x(pCur, g_cTimeStart);

	if (lTime > SECONDS_PER_DAY)
	{
//		pCur[0] = '2';
//		pCur[1] = '4';
		lTime = lTime % SECONDS_PER_HOUR;
	}
	else
	{
		iTime[0] = (UCHAR)(lTime / SECONDS_PER_HOUR);
		iTime[1] = (UCHAR)((lTime % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
		iTime[2] = (UCHAR)(lTime % SECONDS_PER_MINUTE);

		for (i = 0; i < 3; i ++)
		{
			pCur[0] = iTime[i]/10 + '0';		// first value
			pCur[1] = iTime[i]%10 + '0';		// second value
			pCur += 3;
		}
	}
}

void DisplayOutputVol()
{
	DCHAR iVal;

	iVal = PIO_bHandsetUp ? Ac97_iHandsetOutputVol : Ac97_iHandfreeOutputVol;
	DisplayStatus((PCCHAR)"Vol", iVal, (UCHAR)((iVal+1)>>1));
}

