#include "page1.h"

XLONG Call_lDuration;

static XCHAR _iItemNumber[3] = {0, 0, 0};
static XCHAR _iCurReadIndex[3] = {0, 0, 0};
static XCHAR _iCurWriteIndex[3] = {0, 0, 0};
static BOOLEAN _bCallRecStarted = FALSE;

#ifdef VER_WG2000
static XCHAR _MissedCallReadIndicate[CALL_RECORD_NUM] = {0};//judy
BOOLEAN bUserallread = TRUE;//judy
#endif

static CCHAR _iCallBeginLo[3] = {CALL_ANSWERED_LO, CALL_MISSED_LO, CALL_DIALED_LO};

#ifdef VER_IPH_E00
void CallRecDisplay(UCHAR iType)
{
	switch (iType)
	{
	case CALL_TYPE_ANSWERED:
		DisplayString(g_cAnsweredCalls, 0);
		break;
	case CALL_TYPE_MISSED:
		DisplayString(g_cMissedCalls, 0);
#ifdef GUI_TG_LED
		Led_Off(MISSED_CALL_LIGHT);
#endif
		break;
	case CALL_TYPE_DIALED:
		DisplayString(g_cDialedCalls, 0);
		break;
	}
}
#endif

UCHAR CallRecRead(UCHAR iReadType)
{
	DCHAR iType;
#ifdef VER_WG2000
        DCHAR i; //judy
        DCHAR tmp = 1; //judy
#endif

	iType = iReadType;
	Sys_iScrollType = iType;
	if (!_iItemNumber[iType])
	{
		return 0;
	}
	SdramRead(CALLREC_SDRAM_HI, (UCHAR)(_iCurReadIndex[iType]+_iCallBeginLo[iType]), CALL_FIELD_DWORD);
	p_strcpy_x2x(Key_pBuf, (PXCHAR)Sdram_pDataCache);
	if (p_strlen_x(Key_pBuf))
	{
		Key_bCopied = TRUE;
#ifdef VER_WG2000
		if (iType == CALL_TYPE_MISSED)//judy
                {

                   _MissedCallReadIndicate[_iCurReadIndex[iType]] = 1;
                   for (i=0; i<_iItemNumber[iType]; i++)
                   {
                      tmp &= _MissedCallReadIndicate[i];
                   }
                   if(tmp==0) bUserallread = FALSE;
                   else  bUserallread = TRUE;
                   UIMissedCallIndicate();

                }//judy
#endif
	}
	else
	{
		return 0;
	}
	return (UCHAR)(_iCurReadIndex[iType]+1);
}

UCHAR CallRecReadScroll(BOOLEAN bUp)
{
	DCHAR iType;

	iType = Sys_iScrollType;
	if (!_iItemNumber[iType])
	{
//		OutputString(IDS_CALL_REC_EMPTY);
		return 0;
	}
//	_iCurReadIndex[iType] = OnScroll(_iCurReadIndex[iType], _iCurWriteIndex[iType], bUp);
	_iCurReadIndex[iType] = OnScroll(_iCurReadIndex[iType], _iItemNumber[iType], bUp);
	return CallRecRead(iType);
}

ULONG CallRecGetStartTime(UCHAR iType, UCHAR iIndex)
{
	DCHAR iSdramLo;

	iSdramLo = iIndex; 
	if (!iSdramLo)	return 0;
	
	iSdramLo += _iCallBeginLo[iType];
	iSdramLo --;
	SdramRead(CALLREC_SDRAM_HI, iSdramLo, CALL_FIELD_DWORD);
	return PXCHAR2ULONG((PXCHAR)(Sdram_pDataCache+CALL_FIELD_STIME));
}

/*
ULONG CallRecGetDuration(UCHAR iType, UCHAR	iIndex)
{
	DCHAR iSdramLo;

	iSdramLo = iIndex; 
	if (!iSdramLo)	return 0;
	
	iSdramLo += _iCallBeginLo[iType];
	iSdramLo --;
	SdramRead(CALLREC_SDRAM_HI, iSdramLo, CALL_FIELD_DWORD);
	return PXCHAR2ULONG((PXCHAR)(Sdram_pDataCache+CALL_FIELD_DURATION));
}
*/

void CallRecWriteStart(PXCHAR pNumber, UCHAR iType)
{
//	SdramReadData(OPTIONS_SDRAM_POS+CALLREC_CURWRITE_SDRAM_LO);
	p_memset_x((PXCHAR)Sdram_pDataCache, 0, CALL_RECORD_LEN);
	X_VAL(Sdram_pDataCache, CALL_FIELD_TYPE) = iType;
	p_strcpy_x2x((PXCHAR)Sdram_pDataCache, pNumber);
	ULONG2PUCHAR(Sys_lCurrentTime, (PUCHAR)((PXCHAR)(Sdram_pDataCache+CALL_FIELD_STIME)));
//	Call_lDuration = 0xffffffff;
	Call_lDuration = 0;
	SdramWriteData(OPTIONS_SDRAM_POS+CALLREC_CURWRITE_SDRAM_LO, CALL_RECORD_LEN);	
	_bCallRecStarted = TRUE;
}

UCHAR CallRecWriteStop()
{
	DCHAR iType;

	if (!_bCallRecStarted)	return 0;

	SdramReadData(OPTIONS_SDRAM_POS+CALLREC_CURWRITE_SDRAM_LO);
	iType = X_VAL(Sdram_pDataCache, CALL_FIELD_TYPE);

	if (_iItemNumber[iType] == CALL_RECORD_NUM)
	{
		CallRecDelete(iType, TRUE);
	}

	SdramReadData(OPTIONS_SDRAM_POS+CALLREC_CURWRITE_SDRAM_LO);

	// call rec write duration
	ULONG2PUCHAR(Call_lDuration, (PUCHAR)((PXCHAR)(Sdram_pDataCache+CALL_FIELD_DURATION))); 

	SdramWrite(CALLREC_SDRAM_HI, (UCHAR)(_iCurWriteIndex[iType]+_iCallBeginLo[iType]), CALL_FIELD_DWORD);

	_iCurReadIndex[iType] = _iCurWriteIndex[iType];
	_iCurWriteIndex[iType] = OnScroll(_iCurWriteIndex[iType], CALL_RECORD_NUM, TRUE);
	if (_iItemNumber[iType] < CALL_RECORD_NUM)
	{
		_iItemNumber[iType] ++;
	}

	_bCallRecStarted = FALSE;
	if (iType == CALL_TYPE_MISSED)
	{
#ifdef VER_WG2000
		bUserallread = FALSE;//judy
#endif
		UIMissedCallIndicate();
	}
	return _iItemNumber[iType];
}

UCHAR CallRecDelete(UCHAR iDelType, BOOLEAN bFromStart)
{
	DCHAR i, iType, iStartLo;

	iType = iDelType;
	if (!_iItemNumber[iType])
	{
		return 0;
	}

	iStartLo = (bFromStart ? 0 : _iCurReadIndex[iType]) + _iCallBeginLo[iType]; 
	for (i = iStartLo; i < _iItemNumber[iType] + _iCallBeginLo[iType]; i ++)
	{
		SdramRead(CALLREC_SDRAM_HI, (UCHAR)(i+1), CALL_FIELD_DWORD);
		SdramWrite(CALLREC_SDRAM_HI, i, CALL_FIELD_DWORD);
	}

	_iItemNumber[iType] --;
	_iCurWriteIndex[iType] = OnScroll(_iCurWriteIndex[iType], CALL_RECORD_NUM, FALSE);
	if (_iCurReadIndex[iType] == _iCurWriteIndex[iType])
	{
		_iCurReadIndex[iType] = OnScroll(_iCurReadIndex[iType], _iItemNumber[iType], FALSE);
	}
	return (UCHAR)(_iCurReadIndex[iType] + 1);
}


