// BarcodeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BarcodeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDLE_TIMER      11
#define IDLE_TIMEOUT    100

/////////////////////////////////////////////////////////////////////////////
// CBarcodeDlg dialog

BOOL GetIniFilePath(TCHAR* pName);

CBarcodeDlg::CBarcodeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBarcodeDlg::IDD, pParent)
{
	m_strBarcode = _T("");
    m_dwTick = 0;
    m_nPort = -1;
 	m_nBarcode_length = 14;
	m_bAutoStart = FALSE;
	m_bAutoInputSN = FALSE;

}

CBarcodeDlg::CBarcodeDlg(int nPort,CWnd* pParent /*= NULL*/)
    : CDialog(CBarcodeDlg::IDD, pParent)
{
    CBarcodeDlg::CBarcodeDlg(pParent);

    m_dwTick = 0;
    m_nPort = nPort;
	m_nBarcode_length = 14;
	m_bAutoStart = FALSE;
	m_bAutoInputSN = FALSE;
}


void CBarcodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBarcodeDlg)
	DDX_Control(pDX, IDC_BUTTON_SUSPEND, m_btnPause);
	DDX_Control(pDX, IDOK, m_btnOK);
	DDX_Control(pDX, IDC_EDIT_BARCODE, m_edtBarcode);
	DDX_Text(pDX, IDC_EDIT_BARCODE, m_strBarcode);
//	DDV_MaxChars(pDX, m_strBarcode, 14);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBarcodeDlg, CDialog)
	//{{AFX_MSG_MAP(CBarcodeDlg)
	ON_BN_CLICKED(IDC_BUTTON_SUSPEND, OnButtonSuspend)
	ON_WM_CTLCOLOR()
	ON_EN_CHANGE(IDC_EDIT_BARCODE, OnChangeEditBarcode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBarcodeDlg message handlers

BOOL CBarcodeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	//Set ICON
	m_btnOK.SetIcon(IDI_ICON_OK);
    m_btnOK.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 10);
	m_btnOK.EnableWindow(FALSE);	

	m_btnPause.SetIcon(IDI_ICON_PAUSE);
    m_btnPause.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 10);
	
	//Load Config
	TCHAR szFilePath[_MAX_PATH];
	if(!GetIniFilePath(szFilePath))
	{
		return FALSE;
	}
	   
	m_nBarcode_length = GetPrivateProfileInt(_T("SN"),  _T("SN_LENGTH"), 14, szFilePath);
    m_bAutoStart      = GetPrivateProfileInt(_T("SN"),  _T("AutoStartAfterInputSN"), 1, szFilePath);
	m_bAutoInputSN    = GetPrivateProfileInt(_T("SN"),  _T("AutoGenerateSN"), 0, szFilePath);

	//Set Title
	m_str_title.Format(_T("PORT:%d, Please input SN(Length: %d):"), m_nPort, m_nBarcode_length);
	SetWindowText(m_str_title);	   
	m_edtBarcode.SetLimitText(m_nBarcode_length);	
 
	UpdateData(FALSE);  	 
   
    m_edtBarcode.SetFocus();
	if (m_bAutoInputSN)
	{
		CTime Time = CTime::GetCurrentTime();
		CString strSN = Time.Format(_T("%Y%m%d%H%M%S"));
		if (strSN.GetLength() >= m_nBarcode_length)
		{
			strSN=strSN.Right(m_nBarcode_length);
		}
		else
		{
			int nLen = m_nBarcode_length - strSN.GetLength();
			CString strTemp;
			for(int i=0; i<nLen; i++)
			{
				strTemp.Format(_T("%d"), i);
				strSN += strTemp;
			}			
		}		   
		m_edtBarcode.SetWindowText(strSN);
	}
   
    return FALSE;  
}

void CBarcodeDlg::OnCancel() 
{  
	EndDialog(IDIGNORE);
    return;
}

void CBarcodeDlg::OnButtonSuspend() 
{
    EndDialog(IDIGNORE);
}

void CBarcodeDlg::OnOK() 
{
    UpdateData(TRUE);
	
	BOOL bIsSNValid = TRUE;
	for (int i=0; i<m_strBarcode.GetLength(); i++)
	{
		if (!( _istdigit( m_strBarcode.GetAt(i)) ||
			((m_strBarcode.GetAt(i) >= 0X61 && m_strBarcode.GetAt(i) <= 0X7A) //  >'a'  <'z'
			||(m_strBarcode.GetAt(i) >= 0x41 && m_strBarcode.GetAt(i) <= 0x5A ) )
			))
		{
			bIsSNValid = FALSE;
			break;
		}
	}
	if (!bIsSNValid)
	{
		AfxMessageBox(_T("There are invalid chars in the SN inputted, please input again"));
		return;
	}

    if(m_strBarcode.IsEmpty())
    {
		GetDlgItem(IDC_EDIT_BARCODE)->SetFocus();
		return;
    }
	else if (m_strBarcode.GetLength() !=  m_nBarcode_length)
	{
		GetDlgItem(IDC_EDIT_BARCODE)->SetFocus();    
		return;
	}

	for (i=0; i<m_strBarcode.GetLength(); i++)
	{
		TCHAR ch = m_strBarcode.GetAt(i);
		if (!_istalnum(ch))
		{
			AfxMessageBox(_T("There are invalid chars in the SN inputted, please input again"));
			return;
		}
	}

    EndDialog(IDOK);
}

BOOL CBarcodeDlg::PreTranslateMessage(MSG* pMsg) 
{
    /*if(pMsg->message == WM_KEYUP || pMsg->message == WM_KEYDOWN)
    {
        DWORD dwTick = m_dwTick;
        m_dwTick = ::GetTickCount();
        
        char c = (char)pMsg->wParam;
        if(isdigit(c))
        {
            return FALSE;
        }
        
        if(isalpha(c))
        {
            return FALSE;
        }
        
        if(m_dwTick - dwTick < 100)
        {
            return TRUE;
        }
    }*/

	return CDialog::PreTranslateMessage(pMsg);
}

void CBarcodeDlg::OnChangeEditBarcode() 
{		
    CString strTitle;
	GetWindowText(strTitle);
    CString strText;
    m_edtBarcode.GetWindowText(strText);
  /*
	for (int i=0; i<strText.GetLength(); i++)
	{
		if (!( _istdigit( strText.GetAt(i)) ||
			((strText.GetAt(i) >= 0X61 && strText.GetAt(i) <= 0X7A) //  >'a'  <'z'
			||(strText.GetAt(i) >= 0x41 && strText.GetAt(i) <= 0x5A ) )
			))
		{
			strText.Delete(i);
		}
	}*/

 	CString str;
	str.Format(_T("%d"), strText.GetLength());
	str += _T("  :  ");
	str += m_str_title;
	SetWindowText(str); 

    if(strText.GetLength() == m_nBarcode_length)
	{
      m_btnOK.EnableWindow(TRUE);
	  if (m_bAutoStart)
	  {
		  OnOK();
	  }	  
	}
	else
	{
       m_btnOK.EnableWindow(FALSE);	
	   m_edtBarcode.SetFocus();
	   Invalidate();
	}
}

BOOL GetIniFilePath(TCHAR* pName)
{
    if(pName == NULL)
    {
        return FALSE;
    }
    
    DWORD dwRet = ::GetModuleFileName(AfxGetApp()->m_hInstance, pName, _MAX_PATH);
    if(dwRet == 0)
    {
        pName[0] = 0;
        return FALSE;
    }

    TCHAR* pResult = _tcsrchr(pName, _T('.'));
    if(pResult == NULL)
    {
        pName[0] = 0;
        return FALSE;
    }
    
    *pResult = 0;
    _tcscat(pName, _T(".ini"));
    
    return TRUE;
}
