// ClockDlg.cpp : Implementation file
//

#include "stdafx.h"
#include "RemoteTest.h"
#include "ClockDlg.h"
#include "RemoteTestDlg.h"
#include <sys\timeb.h>


CClockDlg::CClockDlg( CVAIBO *vaibo, CWnd* pParent/*=NULL*/ )
    : CDialog( CClockDlg::IDD, pParent )
{
    //{{AFX_DATA_INIT(CClockDlg)
    //}}AFX_DATA_INIT

    // Virtual AIBO
    m_vaibo = vaibo;
}


void CClockDlg::DoDataExchange( CDataExchange* pDX )
{
    CDialog::DoDataExchange( pDX );
    //{{AFX_DATA_MAP(CClockDlg)
    //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP( CClockDlg, CDialog )
    //{{AFX_MSG_MAP(CClockDlg)
    ON_WM_DESTROY()
    ON_BN_CLICKED( IDC_BTN_SET_CLOCK, OnSetClock )
	ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL    CClockDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    return TRUE;
}

void CClockDlg::OnDestroy()
{
    CDialog::OnDestroy();
}

void CClockDlg::OnSetClock()
{
    CString     str;
    int     nYear, nMonth, nDay, nHour, nMinute, nSecond, nUtc;

    editYear().GetWindowText( str );
    nYear = atoi( str );
    if ( ( nYear < 0 ) || ( nYear > 9999 ) ) {
        AfxMessageBox( "Year value is invalid\n" );
        return;
    }
    editMonth().GetWindowText( str );
    nMonth = atoi( str );
    if ( ( nMonth < 1 ) || ( nMonth > 12 ) ) {
        AfxMessageBox( "Month value is invalid\n" );
        return;
    }
    editDay().GetWindowText( str );
    nDay = atoi( str );
    if ( ( nDay < 1 ) || ( nDay > 31 ) ) {
        AfxMessageBox( "Day value is invalid\n" );
        return;
    }
    editHour().GetWindowText( str );
    nHour = atoi( str );
    if ( ( nHour < 0 ) || ( nHour > 23 ) ) {
        AfxMessageBox( "Hour value is invalid\n" );
        return;
    }
    editMinute().GetWindowText( str );
    nMinute = atoi( str );
    if ( ( nMinute < 0 ) || ( nMinute > 59 ) ) {
        AfxMessageBox( "Minute value is invalid\n" );
        return;
    }
    editSecond().GetWindowText( str );
    nSecond = atoi( str );
    if ( ( nSecond < 0 ) || ( nSecond > 59 ) ) {
        AfxMessageBox( "Second value is invalid\n" );
        return;
    }
    editUtc().GetWindowText( str );
    nUtc = atoi( str );
    if ( ( nUtc < -12 ) || ( nUtc > +12 ) ) {
        AfxMessageBox( "UTC value is invalid\n" );
        return;
    }

    int nRet = m_vaibo->SetClock(
		                  ( nYear << 16 ) | ( nMonth  << 8 ) | nDay,		// 0xYYYYMMDD
        ( nUtc  << 24 ) | ( nHour << 16 ) | ( nMinute << 8 ) | nSecond,		// 0x00HHMMSS
        true											// Wait for command completion
    );

    if ( nRet != VAIBO_NOERROR ) 
        AfxMessageBox( "SetClock error\n" );
}

void CClockDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog ::OnShowWindow(bShow, nStatus);
	
	GetNowTime();
}

void CClockDlg::GetNowTime()
{
	struct tm *now;
	struct _timeb tbuffer;

	_ftime( &tbuffer );
	now = localtime( &tbuffer.time );

	int nTimeZone =  tbuffer.timezone / 60;

    CTime   time = CTime::GetCurrentTime();

    CString     str;
    str.Format( "%d", now->tm_year + 1900 );
    editYear().SetWindowText( ( LPCTSTR )str );
    str.Format( "%d", now->tm_mon + 1 );
    editMonth().SetWindowText( ( LPCTSTR )str );
    str.Format( "%d", now->tm_mday ); 
    editDay().SetWindowText( ( LPCTSTR )str );
    str.Format( "%d", now->tm_hour );
    editHour().SetWindowText( ( LPCTSTR )str );
    str.Format( "%d", now->tm_min );
    editMinute().SetWindowText( ( LPCTSTR )str );
	str.Format( "%d", now->tm_sec );
    editSecond().SetWindowText( ( LPCTSTR )str );
	str.Format( "%d", nTimeZone );
    editUtc().SetWindowText( ( LPCTSTR )str );
}
