/*************************************************************************\
* "$RCSfile: CDlgPassword.cpp,v $"                                        *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2010-06-16 10:20:00 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.8 $                                 *
\*************************************************************************/
/*! \file CDlgPassword.cpp
*	\brief Implementation CDlgPassword Class
**************************************************************************/

#include <string.h>
#include "VIC_PS.h"
#include "CDlgPassword.hpp"
#include "CRegion.h"
#include "CPSApp.hpp"
#include "CDlg.hpp"

CDlgPassword *CDlgPassword::m_pThis = NULL;	//!< The one and only CDlgPassword instance

/*************************************************************************/
CDlgPassword::CDlgPassword()
/*************************************************************************/
/*! @brief Constructor
**************************************************************************/
{
	CPSApp *pApp = CPSApp::GetInstance();
	TEXTPARAMETER TP;
	char *Text;
	Config *pConf;

	pConf = Config::loadFromIni(VIB_Drive[1], "Password.ini", "PASSWORD_INI");
	Text = pConf->getValue("PASSWORD_A", "1234", 5); // Admin
	VIB_SStrCpy(m_PasswordA, Text, 16);
	Text = pConf->getValue("PASSWORD_U", "1111", 5); // User
	VIB_SStrCpy(m_PasswordU, Text, 16);
	delete pConf;

	VIB_ChDir(VIB_Drive[1], pApp->GetSkinPath());
	m_Icon[IB_B1] = CDlg::ReadIcon("B_1.png", 98, 98);
	m_Icon[IB_B2] = CDlg::ReadIcon("B_2.png", 98, 98);
	m_Icon[IB_B3] = CDlg::ReadIcon("B_3.png", 98, 98);
	m_Icon[IB_B4] = CDlg::ReadIcon("B_4.png", 98, 98);
	m_Icon[IB_B5] = CDlg::ReadIcon("B_5.png", 98, 98);
	m_Icon[IB_B6] = CDlg::ReadIcon("B_6.png", 98, 98);
	m_Icon[IB_B7] = CDlg::ReadIcon("B_7.png", 98, 98);
	m_Icon[IB_B8] = CDlg::ReadIcon("B_8.png", 98, 98);
	m_Icon[IB_B9] = CDlg::ReadIcon("B_9.png", 98, 98);
	m_Icon[IB_B0] = CDlg::ReadIcon("B_0.png", 98, 98);
	VIB_ChDir(VIB_Drive[1], "..");

	m_pDlg = new GUI_DLG;
	GUI_DlgCreate2(m_pDlg, 332, 532, "Automatic");
	GUI_DlgSetGradCol(m_pDlg, pApp->GetGradCol());
	m_pText = GUI_DlgAddText(m_pDlg, 112, 32, "Enter password");
	GUI_GetTxtParam(&TP);
	TP.Scale = 2;
	GUI_SetTxtParam(&TP);
	GUI_DlgAddSEditBox(m_pDlg, 116, 56, 98, m_SValue, 101);
	TP.Scale = 1;
	GUI_SetTxtParam(&TP);
	GUI_DlgAddIconButton(m_pDlg,   8, 100, 98, 98, NULL, &m_Icon[IB_B1], 11);
	GUI_DlgAddIconButton(m_pDlg, 117, 100, 98, 98, NULL, &m_Icon[IB_B2], 12);
	GUI_DlgAddIconButton(m_pDlg, 226, 100, 98, 98, NULL, &m_Icon[IB_B3], 13);
	GUI_DlgAddIconButton(m_pDlg,   8, 208, 98, 98, NULL, &m_Icon[IB_B4], 14);
	GUI_DlgAddIconButton(m_pDlg, 117, 208, 98, 98, NULL, &m_Icon[IB_B5], 15);
	GUI_DlgAddIconButton(m_pDlg, 226, 208, 98, 98, NULL, &m_Icon[IB_B6], 16);
	GUI_DlgAddIconButton(m_pDlg,   8, 316, 98, 98, NULL, &m_Icon[IB_B7], 17);
	GUI_DlgAddIconButton(m_pDlg, 117, 316, 98, 98, NULL, &m_Icon[IB_B8], 18);
	GUI_DlgAddIconButton(m_pDlg, 226, 316, 98, 98, NULL, &m_Icon[IB_B9], 19);
	GUI_DlgAddIconButton(m_pDlg,   8, 424, 98, 98, NULL, &m_Icon[IB_B0], 10);
	GUI_DlgAddAccel(m_pDlg, '1', 11);
	GUI_DlgAddAccel(m_pDlg, '2', 12);
	GUI_DlgAddAccel(m_pDlg, '3', 13);
	GUI_DlgAddAccel(m_pDlg, '4', 14);
	GUI_DlgAddAccel(m_pDlg, '5', 15);
	GUI_DlgAddAccel(m_pDlg, '6', 16);
	GUI_DlgAddAccel(m_pDlg, '7', 17);
	GUI_DlgAddAccel(m_pDlg, '8', 18);
	GUI_DlgAddAccel(m_pDlg, '9', 19);
	GUI_DlgAddAccel(m_pDlg, '0', 10);
	GUI_DlgAddIconButton(m_pDlg, 117, 424, 98, 98, "_OK",
		pApp->GetIcon(IconId::OK), 100);
	GUI_DlgAddIconButton(m_pDlg, 226, 424, 98, 98, pApp->GetStr(StringTable::CANCEL),
		pApp->GetIcon(IconId::CANCEL), DLG_ABORT);
}

/*************************************************************************/
CDlgPassword::~CDlgPassword()
/*************************************************************************/
/*! @brief Destructor
**************************************************************************/
{
	for (int i = 0; i <IB_XXX; i++)
		GUI_FreeCImage(&m_Icon[i]);
	GUI_DlgFree(m_pDlg);
}

/*************************************************************************/
CDlgPassword *CDlgPassword::CreateInstance()
/*************************************************************************/
/*! @brief Creates CDlgPassword instance.
	@return Pointer to CDlgPassword instance
**************************************************************************/
{
	if (m_pThis == NULL)
		m_pThis = new CDlgPassword();
	return m_pThis;
}

/*************************************************************************/
void CDlgPassword::DisposeInstance()
/*************************************************************************/
/*! @brief Disposes CDlgPassword instance.
**************************************************************************/
{
	if (m_pThis)
		delete m_pThis;
	m_pThis = NULL;
}

/*************************************************************************/
int CDlgPassword::CheckPassword(FILE *fKB, int Level)
/*************************************************************************/
/*! @brief Check password.
	@param fKB Keyboard handle
	@param Level 0: User password\n
	1: Admin password
	@return 1: Password OK\n
	0: Error
**************************************************************************/
{
	const int MaxLen = 5;
	const int Secs = 10;	// Time out value
	int Id, nKey;
	int pos = 0;
	int ret = 0;
	int Counter = 50 * Secs;
	bool bActive = true;
	char Text[32];
	char Password[16];
	char *pPassword;
	char *pString;
	CAM_MSG Msg;
	CPSApp *pApp = CPSApp::GetInstance();
	CIMAGE *pImage = pApp->GetDispImage();

	if (Level == 0)
	{
		pPassword = m_PasswordU;
		pString = "Enter user password";
	}
	else
	{
		pPassword = m_PasswordA;
		pString = "Enter Admin password";
	}

	GUI_DlgModifyText(m_pText, (m_pDlg->rgbDlg.Width - GUI_GetTextWidth(pString)) / 2, 32, pString);

	Password[0] = m_SValue[0] = '\0';
	sprintf(Text, "Enter password: %d s", Secs);
	GUI_DlgSetTitle(m_pDlg, Text);
	GUI_DlgGotoItem(m_pDlg, DLG_ABORT);
	GUI_DlgUpdate(m_pDlg, pImage, -1, -1);

	while (bActive)
	{
		nKey = getc(fKB);
		if (nKey == EOF)
			_UNSET(fKB, _STATEOF);
		if (nKey == KB_ENTER)
			Id = 100;
		else
			Id = GUI_DlgWork(m_pDlg, nKey);
		switch (Id)
		{
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:
		case 17:
		case 18:
		case 19:
			if (pos < MaxLen)
			{
				m_SValue[pos] = '*';
				Password[pos] = '0' + Id - 10;
				pos++;
				m_SValue[pos] = Password[pos] = '\0';
			}
			GUI_DlgGotoItem(m_pDlg, DLG_ABORT);
			break;
		case 100:
			if (strcmp(Password, pPassword) == 0)
				ret = 1;
			bActive = false;
			break;
		case DLG_FOCUS_CHANGED:
			Id = GUI_DlgGetSelectedID(m_pDlg);
			if (pos<MaxLen && Id >= 10 && Id <= 19)
			{
				m_SValue[pos] = '*';
				Password[pos] = '0' + Id - 10;
				pos++;
				m_SValue[pos] = Password[pos] = '\0';
			}
			break;
		case DLG_VALUE_CHANGED:
			Id = GUI_DlgGetSelectedID(m_pDlg);
			if (Id == 101)
			{
				if (pos > 0 && m_SValue[pos] == '\0')	// Backspace
				{
					pos--;
					Password[pos] = '\0';
				}
				else if (pos < MaxLen)
				{
					Password[pos] = m_SValue[pos];
					m_SValue[pos] = '*';
					pos++;
					m_SValue[pos] = Password[pos] = '\0';
				}
			}
			break;
		case DLG_ABORT:
			bActive = false;
			break;
		}
		if (Id != DLG_EOF)
		{
			GUI_DlgUpdate(m_pDlg, pImage, -1, -1);
			Counter = 50 * Secs;
		}
		else
		{
			while (MBX_pend(pApp->GetMbxCam(), &Msg, 0)) ;	// Clear Camera MailBox

			VIB_WaitBios(20);
			Counter--;
			if ((Counter % 50) == 0)
			{
				sprintf(Text, "Enter password: %d s", Counter / 50);
				GUI_DlgSetTitle(m_pDlg, Text);
				GUI_DlgUpdate(m_pDlg, pImage, -1, -1);
			}
			if (Counter <= 0)
				bActive = false;
		}
	}
	pApp->RecBackground(pImage ? 0 : 1);
	return ret;
}

/*************************************************************************/
