// DlgDownloadFile.cpp : implementation file
//

#include "stdafx.h"
#include "netplayx.h"
#include "DlgDownloadFile.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgDownloadFile dialog


CDlgDownloadFile::CDlgDownloadFile(UINT nIDTemplate /*=CDlgDownloadFile::IDD*/,CWnd* pParent /*=NULL*/)
	: CDialog(nIDTemplate, pParent)
{
	//{{AFX_DATA_INIT(CDlgDownloadFile)
	m_startTime = COleDateTime::GetCurrentTime();
	m_endTime = COleDateTime::GetCurrentTime();
	m_szDownloadfile = _T("");
	m_endDate = COleDateTime::GetCurrentTime();
	m_startDate = COleDateTime::GetCurrentTime();
	//}}AFX_DATA_INIT
	m_language = 1;
}


void CDlgDownloadFile::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgDownloadFile)
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_STARTTIME, m_startTime);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_ENDTIME, m_endTime);
	DDX_Text(pDX, IDC_EDIT_DOWNLOADFILE, m_szDownloadfile);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_ENDDATE, m_endDate);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_STARTDATE, m_startDate);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgDownloadFile, CDialog)
	//{{AFX_MSG_MAP(CDlgDownloadFile)
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgDownloadFile message handlers

void CDlgDownloadFile::OnBtnBrowse() 
{
	CString	  szTmp,szFile;

	if(m_language)
	{
#ifdef FILEEXPAND_H264
		szTmp.LoadString(IDS_STRING265);
#else
		szTmp.LoadString(IDS_STRING233);
#endif
	}
	else
	{
#ifdef FILEEXPAND_H264
		szTmp.LoadString(IDS_STRING266);
#else
		szTmp.LoadString(IDS_STRING234);
#endif
	}

	GetDlgItem(IDC_EDIT_DOWNLOADFILE)->GetWindowText(szFile);

#ifdef FILEEXPAND_H264
	CFileDialog dlg(FALSE, 
			".264",
			szFile, 
			OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
			szTmp, this);
#else
	CFileDialog dlg(FALSE, 
			".mp4",
			szFile, 
			OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
			szTmp, this);
#endif
	if( dlg.DoModal() == IDOK)
	{
		GetDlgItem(IDC_EDIT_DOWNLOADFILE)->SetWindowText(dlg.GetPathName());
	}
}

void CDlgDownloadFile::OnOK() 
{
	if( UpdateData(TRUE) )
	{
		CTime	startTime(m_startDate.GetYear(),
						  m_startDate.GetMonth(),
						  m_startDate.GetDay(),
						  m_startTime.GetHour(),
						  m_startTime.GetMinute(),
						  m_startTime.GetSecond());

		CTime	endTime(m_endDate.GetYear(),
						m_endDate.GetMonth(),
						m_endDate.GetDay(),
						m_endTime.GetHour(),
						m_endTime.GetMinute(),
						m_endTime.GetSecond());

		CTime	allFileTime(m_allFileTime.GetYear(),
							m_allFileTime.GetMonth(),
							m_allFileTime.GetDay(),
							m_allFileTime.GetHour(),
							m_allFileTime.GetMinute(),
							m_allFileTime.GetSecond());

		if (GetStartTime() < m_filecreatetime )
		{
			if(m_language)
			{
				AfxMessageBox(IDS_STRING259);
			}
			else
			{
				AfxMessageBox(IDS_STRING260);
			}
			return;
		}

		if (startTime >= endTime)
		{
			if(m_language)
			{
				AfxMessageBox(IDS_STRING238);
			}
			else
			{
				AfxMessageBox(IDS_STRING239);
			}
			return;
		}

		if (startTime > allFileTime)
		{
			if(m_language)
			{
				AfxMessageBox(IDS_STRING240);
			}
			else
			{
				AfxMessageBox(IDS_STRING241);
			}
			return;
		}

		if (endTime > allFileTime)
		{
			if(m_language)
			{
				AfxMessageBox(IDS_STRING242);
			}
			else
			{
				AfxMessageBox(IDS_STRING243);
			}
			return;
		}

		if (m_szDownloadfile == "")
		{
			if(m_language)
			{
				AfxMessageBox(IDS_STRING244);
			}
			else
			{
				AfxMessageBox(IDS_STRING245);
			}
			return;
		}
		CDialog::OnOK();
	}
}

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

	m_endDate = m_allFileTime;
	m_endTime = m_allFileTime;
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

DWORD CDlgDownloadFile::GetStartTime()
{
	struct tm startTime;
	startTime.tm_year = m_startDate.GetYear() - 1900;
	startTime.tm_mon  = m_startDate.GetMonth() - 1;
	startTime.tm_mday = m_startDate.GetDay();
	startTime.tm_wday = m_startDate.GetDayOfWeek();
	startTime.tm_yday = m_startDate.GetDayOfYear();
	startTime.tm_hour = m_startTime.GetHour();
	startTime.tm_min  = m_startTime.GetMinute();
	startTime.tm_sec  = m_startTime.GetSecond();
	return mktime(&startTime);

	//return (m_startTime.GetHour() * 60 * 60 + m_startTime.GetMinute() * 60 + m_startTime.GetSecond()) * 1000;
}

DWORD CDlgDownloadFile::GetEndTime()
{
	struct tm endTime;
	endTime.tm_year= m_endDate.GetYear() - 1900;
	endTime.tm_mon  = m_endDate.GetMonth() - 1;
	endTime.tm_mday = m_endDate.GetDay();
	endTime.tm_wday = m_endDate.GetDayOfWeek();
	endTime.tm_yday = m_endDate.GetDayOfYear();
	endTime.tm_hour = m_endTime.GetHour();
	endTime.tm_min  = m_endTime.GetMinute();
	endTime.tm_sec  = m_endTime.GetSecond();
	return mktime(&endTime);

	//return (m_endTime.GetHour() * 60 * 60 + m_endTime.GetMinute() * 60 + m_endTime.GetSecond()) * 1000;
}
