// AutoResumeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AutoResume.h"
#include "AutoResumeDlg.h"
#include <Notify.h>
#include <Pm.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAutoResumeDlg dialog

CAutoResumeDlg::CAutoResumeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAutoResumeDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAutoResumeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAutoResumeDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
    ON_EN_CHANGE(IDC_TIME, &CAutoResumeDlg::OnEnChangeEdit1)
    ON_BN_CLICKED(IDC_BUTTON1, &CAutoResumeDlg::OnBnClickedButton1)
    ON_BN_CLICKED(IDC_BUTTON2, &CAutoResumeDlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// CAutoResumeDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CAutoResumeDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_AUTORESUME_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_AUTORESUME_DIALOG));
	}
}
#endif


void CAutoResumeDlg::OnEnChangeEdit1()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here
}

void CAutoResumeDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here
    HMODULE hDLL = LoadLibrary(_T("\\Windows\\DTRControlSuspend.dll"));

    union {
        LONGLONG time64;
        FILETIME FT;
    }SleepTime;
    SYSTEMTIME st;

    GetLocalTime(&st);

    SystemTimeToFileTime(&st, &SleepTime.FT);

    // We are passed in seconds, but we need to set the
    // wake time in multiples of 100 nanoseconds   
    SleepTime.time64 += (15 * 10000000);

    FileTimeToSystemTime(&SleepTime.FT, &st);

    RETAILMSG(1, (
        L"+CeRunAppAtTime(%d/%d/%d %d:%d:%d.%03d)\r\n", 
        st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute,
        st.wSecond, st.wMilliseconds
        ));

    TCHAR lpFileName[MAX_PATH+1];

    GetModuleFileName(NULL, lpFileName, MAX_PATH);
    
   
    if (FALSE == CeRunAppAtTime(L" ", &st))
    {
        //MessageBox(L"Error", L"Failed", MB_OK);
        RETAILMSG(1,(TEXT("!!!!CeRunAppAtTime failed error code=%x!!!!!!\r\n"), GetLastError()));
    }

    //SetSystemPowerState(NULL, POWER_STATE_SUSPEND, POWER_FORCE);

    DTRControlSuspend pSuspend = 
        (DTRControlSuspend)GetProcAddress(hDLL, _T("DTRControlSuspend"));
    //Sleep(20000);

    if (pSuspend)
    {

        RETAILMSG(1,(TEXT("!!!!Call DTR suspend!!!!!!\r\n")));
        pSuspend(TRUE);
    }

}

void CAutoResumeDlg::OnBnClickedButton2()
{
    // TODO: Add your control notification handler code here
    CString csText;
    GetDlgItemText(IDC_TIME, csText);
    int nMax = _ttoi(csText);
    HANDLE hThread = CreateThread(NULL, 0, AutoRunTimeThread, &nMax, 0, NULL);
    if (hThread)
    {
        CloseHandle(hThread);
    }
}

DWORD WINAPI CAutoResumeDlg::AutoRunTimeThread(LPVOID lpParam)
{
    HMODULE hDLL = LoadLibrary(_T("\\Windows\\DTRControlSuspend.dll"));
    int nStep =0;
    int nMax = 10;
    if (lpParam)
    {
        nMax = *((int*)lpParam);
    }
    while (nStep< nMax)
    {
        Sleep(8000);
        RETAILMSG(1,(TEXT("!!!!for step %d!!!!!!\r\n"), nStep));
        nStep++;
        union {
            LONGLONG time64;
            FILETIME FT;
        }SleepTime;
        SYSTEMTIME st;

        GetLocalTime(&st);

        SystemTimeToFileTime(&st, &SleepTime.FT);

        // We are passed in seconds, but we need to set the
        // wake time in multiples of 100 nanoseconds   
        SleepTime.time64 += (15 * 10000000);

        FileTimeToSystemTime(&SleepTime.FT, &st);

        RETAILMSG(1, (
            L"+CeRunAppAtTime(%d/%d/%d %d:%d:%d.%03d)\r\n", 
            st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute,
            st.wSecond, st.wMilliseconds
            ));

        TCHAR lpFileName[MAX_PATH+1];

        GetModuleFileName(NULL, lpFileName, MAX_PATH);


        if (FALSE == CeRunAppAtTime(L" ", &st))
        {
            //MessageBox(L"Error", L"Failed", MB_OK);
            RETAILMSG(1,(TEXT("!!!!CeRunAppAtTime failed error code=%x!!!!!!\r\n"), GetLastError()));
        }
        //SetSystemPowerState(NULL, POWER_STATE_SUSPEND, POWER_FORCE);

        DTRControlSuspend pSuspend = 
            (DTRControlSuspend)GetProcAddress(hDLL, _T("DTRControlSuspend"));
        

        if (pSuspend)
        {

            RETAILMSG(1,(TEXT("!!!!Call DTR suspend!!!!!!\r\n")));
            pSuspend(TRUE);
        }
    }
    if (hDLL)
    {
        FreeLibrary(hDLL);
    }
    return 1;
}