#include "Page1.h"

#include "p_dsp.h"
#include "gdiconst.h"

#include "v_task.h"

#ifdef OEM_SKYCOM
#include "skycom.h"
#endif

#ifdef GUI_STANDARD
static XCHAR _iSpeedDialIndex = 0;
static XCHAR _iSpeedDialScrool = 1;

BOOLEAN IsSetupMode()
{
	if (!IsFunctionMode())		return FALSE;
	return Task_bSetupMode;
}

void _UpdateVal(UCHAR iOffset, UCHAR iVal)
{
	SdramReadOptions();
	X_VAL(Sdram_pDataCache, iOffset) = iVal;
	SdramWriteOptions();
	OnUpdateOptions();
}

void _UpdateString(USHORT iOffset, PXCHAR pString, UCHAR iLen)
{
	SdramReadOptions();
	p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+iOffset), pString, iLen);
	SdramWriteOptions();
	OnUpdateOptions();
}

void _UpdateIP(UCHAR iOffset)
{
	XCHAR pIP[IP_ALEN];

	if (!p_strlen_x(Key_pBuf))	return;
	if (Sys_iIPType)	return;		// do not update when dhcp or pppoe enabled

	if (p_String2IP(Key_pBuf, pIP))
	{
		_UpdateString(iOffset, pIP, IP_ALEN);
	}
}

void _OnKeyFunctions()
{
	DCHAR iVal;

	if (!p_strlen_x(Key_pBuf))
	{
#ifdef _USE_FXS
		OnReboot();		// as * key in 1-port FXS gateway
#endif
		return;
	}

	iVal = (UCHAR)p_atoi(Key_pBuf, 10);
	switch (iVal)
	{
	case 0:	// disable DHCP
	case 1:	// enable DHCP
	case 2: // enable PPPOE
	case 3:
		_UpdateVal(OPTIONS_IP_TYPE, iVal);
		break;

	case 4:	// log off and PPPoE Disconnect
		OnLogoff();
		break;

	case 5:
	case 6:
	case 7:
	case 8:
		SysLoadOptions((UCHAR)(iVal-4));
		OnUpdateOptions();
		break;

#ifdef HW_MODEM
	case 9:
		ModemRedial();
		break;
#endif

	case 10:
		OnReboot();
		break;

	default:
		DtmfBusy();
		break;
	}
	Key_pBuf[0] = 0;
}

#ifdef HW_MODEM
void OnModemConnect()
{
	_UpdateVal(OPTIONS_IP_TYPE, IP_TYPE_MODEM);
}

void OnModemDisconnect()
{
	_UpdateVal(OPTIONS_IP_TYPE, IP_TYPE_DHCP);
}
#endif

void OnIPAddress()
{
#ifdef _USE_FAKE_FXO
	if (!Line_b1S1OStatus)
	{
		SwitchToFXO(FALSE);
	}
#endif

	DisplayString(g_cLocalIP, 0);

	// stop remote debug
	UdpDebugRemoteStop();
	if (IsCallMode())
	{
		if (IsSetupMode())
		{
			_UpdateIP(OPTIONS_IP_ADDR);
			Task_bSetupMode = FALSE;
			_OnKeyFunctions();
		}
	}
	else
	{
		SdramPlayIP(IVR_MSG_LOCAL, IVR_MSG_IP, Local_pIPAddress);
	}
}

void OnSubnetMask()
{
	DisplayString(g_cSubnetMask, 0);
	if (IsCallMode())
	{
		if (IsSetupMode())
		{
			_UpdateIP(OPTIONS_SUBNET_MASK);
			DtmfBusy();
		}
	}
	else
	{
		SdramPlayIP(IVR_MSG_SUBNET, IVR_MSG_MASK, Local_pSubnetMask);
	}
}

void OnGKAddress()
{
	if (!IsValidIP(Sys_pServiceIP))	return;

	DisplayString(g_cServiceIP, 0);
	if (!IsCallMode())
	{
		SdramPlayIP(IVR_MSG_SERVICE, IVR_MSG_IP, Sys_pServiceIP);
	}
}

void OnRouterAddress()
{
	DisplayString(g_cRouterIP, 0);
	if (IsCallMode())
	{
		if (IsSetupMode())
		{
			_UpdateIP(OPTIONS_ROUTER_ADDR);
			DtmfBusy();
		}
	}
	else
	{
		SdramPlayIP(IVR_MSG_GATEWAY, IVR_MSG_IP, Local_pRouteIP);
	}
}

void OnPhoneNumber()
{
	DisplayString(g_cPhoneNumber, 0);
	if (IsCallMode())
	{
		if (IsSetupMode())
		{
			if (p_strlen_x(Key_pBuf) < PHONE_NUMBER_LEN)
			{
#ifdef CALL_H323
				if (Sys_iServiceType == H323_SERVICE_TYPE_ETALK)
				{
					_UpdateString(OPTIONS_PIN, Key_pBuf, MAX_PIN_LEN);
				}
				else
#endif
				{
#ifdef CALL_N2P
					_UpdateString(OPTIONS_ACCOUNT, Key_pBuf, MAX_ACCOUNT_LEN);
#else
					_UpdateString(OPTIONS_PHONE_NUMBER, Key_pBuf, PHONE_NUMBER_LEN);
#endif
				}
			}
			DtmfBusy();
		}
	}
	else
	{
#ifdef CALL_H323
		if (Sys_iServiceType == H323_SERVICE_TYPE_ETALK)
		{
			DisplayLine(Sys_pPin, 1);
			SdramPlayString(Sys_pPin);
		}
		else if (Sys_iServiceType == H323_SERVICE_TYPE_IFONE)
		{
			DisplayString((PCCHAR)"Credit", 0);
			SdramReadData(OPTIONS_SDRAM_POS+BALANCE_SDRAM_LO);
			DisplayLine((PXCHAR)Sdram_pDataCache, 1);
		}
		else
#endif
		{
			DisplayLine(Sys_pLocalNumber, 1);
			SdramPhoneNumber();
		}
	}
}

void _OnCallRecord(UCHAR iType)
{
	DCHAR iIndex;
	XCHAR pBuf[DISPLAY_MAX_CHAR+1];

//	CallRecDisplay(iType);
	iIndex = CallRecRead(iType);
	if (iIndex)
	{
		GetDateTimeDisplay((PXCHAR)pBuf, TRUE, CallRecGetStartTime(iType, iIndex));
		DisplayLine(pBuf, 1);

		SdramSpeedDial(iIndex);
	}
	else
	{
		DisplayString(g_cEmpty, 1);
	}
}

void OnMissedCall()
{
	if (!IsCallMode())
	{
		// display the unanswered phone call numbers
		_OnCallRecord(CALL_TYPE_MISSED);
	}
}

void OnAnsweredCall()
{
	if (!IsCallMode())
	{	// display the answered phone call numbers
		_OnCallRecord(CALL_TYPE_ANSWERED);
	}
}

void OnDialedCall()
{
	if (!IsCallMode())
	{	// display the dialed phone call numbers
		_OnCallRecord(CALL_TYPE_DIALED);
	}
}

BOOLEAN _GetSpeedDial()
{
	PXCHAR_D pBuf;
	DCHAR iIndex;

	if (Sys_iScrollType != SCROLL_SPEED_DIAL)
	{
		if (p_strlen_x(Key_pBuf))
		{
			_iSpeedDialIndex = (UCHAR)p_atoi(Key_pBuf, 10) - 1;
			if (_iSpeedDialIndex >= SPEED_DIAL_NUM)
			{
				_iSpeedDialIndex = 0;
				return FALSE;	
			}
		}
	}

	iIndex = _iSpeedDialIndex;
	iIndex <<= 1;
	pBuf = ReadSpeedDial(iIndex);

	while (!CheckSpeedDial(pBuf))
	{
		if (_iSpeedDialScrool)
			_iSpeedDialIndex ++;
		else
			_iSpeedDialIndex --;
		if (_iSpeedDialIndex >= SPEED_DIAL_NUM)
		{
			return FALSE;
		}
		iIndex = _iSpeedDialIndex;
		iIndex <<= 1;
		pBuf = ReadSpeedDial(iIndex);
	}

	Key_bCopied = TRUE;
	p_strcpy_x2x(Key_pBuf, pBuf);
	iIndex ++;
	pBuf = ReadSpeedDial(iIndex);
	DisplayLine(pBuf, 1);	// display name here
	return TRUE;
}

void OnSaveSpeedDial(BOOLEAN bSpeedDial)
{
	if (bSpeedDial)
	{
		_iSpeedDialIndex = 0;
		_iSpeedDialScrool = 1;
		KeypadClear();
	}

	if (!IsCallMode() && IsFunctionMode())
	{
//		DisplayString(g_cMemory, 0);
		if (!_GetSpeedDial())
		{
			KeypadClear();
			DisplayString((PCCHAR)"PhoneBook Empty", 1);
		}
		else
		{
			SdramSpeedDial((UCHAR)(_iSpeedDialIndex+1));
			Sys_iScrollType = SCROLL_SPEED_DIAL;
		}
	}
}

void OnLoadSpeedDial()
{
	DisplayString(g_cSpeedDial, 0);
	if (IsFunctionMode())
	{
		if (_GetSpeedDial())
		{
			OnCall();
		}
	}
}

void OnVolHigher()
{
	if (IsCallMode())
	{
		DisplayString(g_cVolPlus, 0);
		Ac97OutputVolHigher();
		DisplayOutputVol();
	}
	else
	{
		if (Sys_iScrollType == SCROLL_SPEED_DIAL)
		{
			_iSpeedDialIndex = OnScroll(_iSpeedDialIndex, SPEED_DIAL_NUM, TRUE);
			_iSpeedDialScrool = 1;
			OnSaveSpeedDial(FALSE);
		}
		else if (Sys_iScrollType < SCROLL_SPEED_DIAL)
		{
			if (CallRecReadScroll(TRUE))
			{
				_OnCallRecord(Sys_iScrollType);
			}
		}
	}
}

void OnVolLower()
{
	if (IsCallMode())
	{
		DisplayString(g_cVolMinus, 0);
		Ac97OutputVolLower();
		DisplayOutputVol();
	}
	else
	{
		if (Sys_iScrollType == SCROLL_SPEED_DIAL)
		{
			_iSpeedDialIndex = OnScroll(_iSpeedDialIndex, SPEED_DIAL_NUM, FALSE);
			_iSpeedDialScrool = 0;
			OnSaveSpeedDial(FALSE);
		}
		else if (Sys_iScrollType < SCROLL_SPEED_DIAL)
		{
			if (CallRecReadScroll(FALSE))
			{
				_OnCallRecord(Sys_iScrollType);
			}
		}
	}
}

BOOLEAN _PoundKeyAsCallKey()
{
	if (Sys_iServiceType == SERVICE_TYPE_HUAWEI || Sys_iServiceType == SERVICE_TYPE_UTSTARCOM)	return FALSE;
//	if (Sys_bUseDigitMap)	return FALSE;

	return TRUE;
}

void OnCall()
{
	DCHAR iLen;
#if defined CALL_H323 || defined CALL_MGCP || defined CALL_N2P || defined CALL_EPN || defined CALL_WP
	if (IsInCall())	return;
#else
	if (Task_bConnecting || (Task_bConnected && !Task_bCallHold))		return;
#endif
	
#ifdef OEM_SKYCOM
	Money_QueryType = 1;
	OnMoneyTcpRun();
#endif	

	DisplayString(g_cCall, 0);

	iLen = p_strlen_x(Key_pBuf);
	if (!iLen)
	{
#ifndef CALL_MGCP
		DtmfBusy();
#endif
		return;
	}
	if (_PoundKeyAsCallKey() && iLen > 1 && Key_pBuf[0] == '#')
	{
		CopySpeedDialKeypad();
		if (!_GetSpeedDial())
		{	
			UndoSpeedDialKeypad();	
		}
	}

	if (!IsCallMode())
	{
		OnPrepareCall();
		PIO_bHandFree = TRUE;
		Key_bCopied = TRUE;
#ifdef CALL_MGCP
		Task_iTimer = 0;
		return;
#endif
	}

#if defined CALL_H323 || defined CALL_MGCP || defined CALL_N2P || defined CALL_EPN || defined CALL_WP
	if (IsFunctionMode())
#endif
	{
		DialPlanBuildNumber();
		UpdateRedialBuf();
		CallRecWriteStart(Key_pBuf, CALL_TYPE_DIALED);
		p_memcpy_x2x_0(Sys_pPeerDisplay, Key_pBuf, PHONE_NUMBER_LEN);
		DisplayLine(Sys_pPeerDisplay, 1);
		DebugString(Sys_pRemoteNumber);
		if (Key_bCopied)
		{
			DtmfPlayKeys();
		}
		else
		{
			TaskCall();
		}
	}
}

void OnHandFree()
{
	if (PIO_bHandFree)
	{
#ifdef BACKLIGHT_CTRL
//		L3ClockOff();
//		Cam_LedOff(0);
		Task_iTimer = 0;
#endif
		PIO_bHandFree = FALSE;
		if (!PIO_bHandsetUp)
		{
			HangupPhone();
		}
	}
	else
	{
#ifdef BACKLIGHT_CTRL
//		L3ClockOn();
#ifdef INVERT_BACKLIGHT
		Cam_LedOn(0);
#else
		Cam_LedOff(0);
#endif
#endif
		PIO_bHandFree = TRUE;
		OnPrepareCall();
		OnHotLine();
	}
	Ac97OnHandsetChange();
	KeypadClear();
}

void OnHandset()
{
	Task_iTimer = 0;
	if (PIO_bHandsetUp)
	{
		OutputString(IDS_HANDSET_UP);
#if defined _USE_FAKE_FXO && defined OEM_HUAWEI3COM
		Led_On(IP_LIGHT);
#endif		

#ifdef _USE_FXS
#ifdef VER_PA168Q
		Led_On(SECOND_LIGHT);
#endif
		Led_On(KEYPAD_LIGHT);
#endif
#ifdef BACKLIGHT_CTRL
//		L3ClockOn();
#ifdef INVERT_BACKLIGHT
		Cam_LedOn(0);
#else
		Cam_LedOff(0);
#endif
#endif
#ifdef _USE_FXS_AM79R70
		SlicActivePRMode();
#endif
#ifdef _USE_FXS_AG1170
//		SlicActivePolarityReversal();
#endif
//#ifdef _USE_FXS_AM79R70
//		SlicActiveMode();
//#endif
//#ifdef _USE_FXS
//		Slic_iCidStatus = 0;
//#endif
		if (PIO_bHandFree)
		{
			PIO_bHandFree = FALSE;
		}
		else
		{
			OnPrepareCall();
			OnHotLine();
		}
	}
	else
	{
		OutputString(IDS_HANDSET_DOWN);
#if defined _USE_FAKE_FXO && defined OEM_HUAWEI3COM
		Led_Off(IP_LIGHT);
#endif		

#ifdef BACKLIGHT_CTRL
//		L3ClockOff();
//		Cam_LedOff(0);
		Task_iTimer = 0;
#endif
		if (!PIO_bHandFree)
		{
			HangupPhone();
#ifdef _USE_FXS
#ifdef VER_PA168Q
			Led_Off(SECOND_LIGHT);
#endif
			Led_Off(KEYPAD_LIGHT);
#endif

#ifdef _USE_FXS_AM79R70
			SlicStandbyMode();
#endif
		}
	}
	Ac97OnHandsetChange();
	KeypadClear();
}

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

	if (Task_bConnected || Task_bSetupMode)
	{
		Led_Blink(SECOND_LIGHT);
	}
	else if (!Task_bLogoned || Task_bSetupMode)
	{
		Led_Blink(NORMAL_LIGHT);
	}
#if defined _USE_FAKE_FXO && defined OEM_HUAWEI3COM
	if (Task_bConnecting)
	{
		Led_Blink(IP_LIGHT);
	}
	else if (!Task_bLogoned)
	{
		Led_Blink(SERVICE_LIGHT);
	}
#endif

	if (Task_iTimer == FUNCTION_TIME_OUT)
	{
#ifdef BACKLIGHT_CTRL
#ifdef INVERT_BACKLIGHT
		Cam_LedOff(0);
#else
		Cam_LedOn(0);
#endif
#endif
		Task_bExtraDisplay = FALSE;
		if (Ac97_bIVR || IsUpgrading())
		{
			Task_iTimer = 0;
		}
		else
		{
			Sys_iScrollType = SCROLL_NULL;
			DisplayNormalState();
		}
	}

	if (Task_bConnected)
	{
		if (Task_bCallWaiting)
		{
			DisplayString(g_cCallWaiting, 0);
			DisplayLine(Sys_pPeerDisplay, 1);
		}
		else
		{
			if (Ac97_bMuteInput)
			{
				DisplayString(g_cCallMuted, 0);
			}
			GetTimeDisplay(Call_lDuration, (PUCHAR)pBuf);
			Display(pBuf, 1, 0, FALSE);
		}
	}
	else
	{
		CommonUIHandleTimer();
#ifdef VER_WG2000
		UIMissedCallIndicate();//judy 
#endif
	}
}

void UIStartup()
{
#ifdef GUI_TG_LED
	if (Task_iRingType == RING_TYPE_DND)
	{
		Led_On(RING_DND_LIGHT);
	}
	else
	{
		Led_Off(RING_DND_LIGHT);
	}
#endif

#ifdef BACKLIGHT_CTRL
#ifdef INVERT_BACKLIGHT
	Cam_LedOff(0);
#else
	Cam_LedOn(0);
#endif
#endif

	p_memset_ip_0(Sys_pServiceIP);
}

void UIInitialize()
{
}

void UIRingIndicate(BOOLEAN bRinging)
{
	if (bRinging)
	{
		Led_Blink(SECOND_LIGHT);
	}
	else
	{
#ifdef VER_PA168P
		Led_On(SECOND_LIGHT);
#else
		Led_Off(SECOND_LIGHT);
#ifdef VER_WG2000
		UIMissedCallIndicate();//judy		
#endif
#endif
	}
}

void UIMissedCallIndicate()
{
#ifdef GUI_TG_LED
	Led_On(MISSED_CALL_LIGHT);
#endif
#ifdef VER_WG2000
     if (Task_bConnected)//judy
     {
           Led_On(SECOND_LIGHT);
     }
     else if (bUserallread)
     {
           Led_Off(SECOND_LIGHT);
     }
     else
	   Led_Blink(SECOND_LIGHT);//judy
#endif
}

BOOLEAN UIHandleSpecialKeys()
{
	DCHAR iLen;

	iLen = p_strlen_x(Key_pBuf);
/*	if (!p_strcmp_c2x(Key_pBuf, (PCCHAR)"000999"))		// used for PA168V only, 
	{													// some customer want the box play IP without press the IP key
		Ac97Stop();
		SdramPlayIP(IVR_MSG_LOCAL, IVR_MSG_IP, Local_pIPAddress);
	}
*/
	if ((Key_iCurrentID == '*' || Key_iCurrentID == '#'))
	{
		if (!iLen || IsPassword(Key_pBuf))
		{
//			if (IsPassword(Key_pBuf))
			if (Task_bSetupMode)
			{	// into test mode
				if (Key_iCurrentID == '#')
				{
					OnLogoff();
					SdramUpdateFlagByte(FLAGS_UPGRADE_MODE, '#');	// Use Upgrade mode Flag to indicate # has been pressed
#ifdef SETTINGS_PAGE
					SwitchPage(SETTINGS_PAGE);
#else
					OnReboot();
#endif
				}
				else
				{
					OnReboot();
				}
			}
		}
	}

	if (Key_iCurrentID == '#')
	{
		if (_PoundKeyAsCallKey())
		{
			if (iLen && (!Sys_bUseDigitMap || Key_bCopied))
			{
				if (Sys_iScrollType <= SCROLL_VOICE_MSG)
				{
					Key_bCopied = TRUE;
				}
				OnCall();
				return TRUE;
			}
		}
		else
		{
			if (iLen && Key_bCopied)
			{
//				if (Sys_iScrollType <= SCROLL_VOICE_MSG)
//				{
//					Key_bCopied = TRUE;
//				}
				OnCall();
				return TRUE;
			}
		}
	}

	return FALSE;
}

void DisplayKeys()
{
	if (p_strlen_x(Key_pBuf) > DISPLAY_MAX_CHAR)
	{
		Display(Key_pBuf, 0, 0, FALSE);
		Display((PXCHAR)(Key_pBuf+DISPLAY_MAX_CHAR), 1, 0, FALSE);
	}
	else
	{
		if (Task_bConnected)
		{
			Display(Key_pBuf, 0, 0, FALSE);
		}
		else
		{
			Display(Key_pBuf, 1, 0, FALSE);	
		}
	}
}

void OnBackSpace()
{
	KeypadBackspace();
	if (p_strlen_x(Key_pBuf))
	{
		DisplayKeys();
	}
	else
	{
		DisplayString(g_cBlank, 1);
		DisplayNormalState();
	}
}

#endif
