// DlgIntelligentTime.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Smart.h"
#include "DlgIntelligentTime.h"
#include "DlgRuleSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgSmartTime dialog
extern CDlgSmartBasicSet*   g_dlgSmartBaseSet;
CDlgSmartTime::CDlgSmartTime(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSmartTime::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSmartTime)
	m_upload_capjpeg = FALSE;
	m_upload_record = FALSE;
	//}}AFX_DATA_INIT
	m_chs = 0;
	m_ch = 0;
	m_alarmins = 1;
	m_alarmouts = 4;
	m_sername = _T("");
	m_serurl = _T("");
	m_serport = 3000;
	m_username = _T("");
	m_password = _T("");
	m_bGetContactEx = FALSE;
	m_fourthway = NULL;
}


void CDlgSmartTime::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSmartTime)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSmartTime, CDialog)
	//{{AFX_MSG_MAP(CDlgSmartTime)
	ON_BN_CLICKED(IDC_BTN_INTELLIGENT_COPY, OnBtnCopy1)
	ON_BN_CLICKED(IDC_BTN_INTELLIGENT_GET, OnBtnGet)
	ON_BN_CLICKED(IDC_BTN_INTELLIGENT_SET, OnBtnSet)
	ON_BN_CLICKED(IDC_BTN_NEXT, OnBtnNext)
	ON_BN_CLICKED(IDC_BTN_PRO, OnBtnPro)
	ON_BN_CLICKED(IDC_BTN_CANCEL, OnBtnCancel)
	ON_WM_CLOSE()
	ON_WM_MOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSmartTime message handlers

BOOL CDlgSmartTime::OnInitDialog() 
{
	CDialog::OnInitDialog();
    InitialDlgData();
	g_dlgSmartBaseSet->m_ptrsetways.Add(this);
	m_second = (CDlgWizardSecond*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(1);
    m_second->ShowWindow(SW_HIDE);
	memset(&m_rulelinkinfo, 0,sizeof(VSNETSMALARMLINK));
	return TRUE; 
}
void CDlgSmartTime::OnOK()
{
}
void CDlgSmartTime::OnCancel()
{
}
void CDlgSmartTime::SetCameraLinkParam(LPCTSTR pDevname,LPCTSTR pUrl,UINT chs,
							UINT port,LPCTSTR pUsername,LPCTSTR pPsw)
{
	m_chs = chs;
	m_sername = pDevname;
	m_serport = port;
	m_serurl = pUrl;
	m_password = pPsw;
	m_username = pUsername;
}

void CDlgSmartTime::OnBtnCopy1() 
{
	if(!UpdateParam()) return;

	int i,tmpch;
	int sel = ((CComboBox*)GetDlgItem(IDC_COMBO_INTELLIGENT_CHCOPY))->GetCurSel();
	if(sel == -1) return;
	if(sel == (m_ch + 1)) return;
	tmpch = m_ch;
	if(sel == 0) //all channels
	{
		for(i= 0 ; i < m_chs ; i ++)
		{
			m_ch = i;
			OnBtnSet();
		}
	}
	else
	{
		m_ch = sel - 1;
		OnBtnSet();
	}
	m_ch = tmpch;
}

void CDlgSmartTime::OnBtnGet() 
{
	
	CString strmsg;
	int i;
	char lpnum[3];
	((CButton*)GetDlgItem(IDC_CHECK_EN))->SetCheck(m_rulelinkinfo.m_enable);
	for(i=0 ; i < 2 ; i ++)
	{
        ((CButton*)GetDlgItem(IDC_CHECK_RECV1 + i))->SetCheck(m_rulelinkinfo.m_brecord[i]);
	    ((CButton*)GetDlgItem(IDC_CHECK_CAPJPEG11 + i))->SetCheck(m_rulelinkinfo.m_capjpeg[i]);
		((CButton*)GetDlgItem(IDC_CHECK_UPLOAD_RECORD1 + i))->SetCheck(m_rulelinkinfo.m_ftp_alarmrec[i]);
		((CButton*)GetDlgItem(IDC_CHECK_UPLOAD_CAPJPEG1 + i))->SetCheck(m_rulelinkinfo.m_ftp_alarmcapjpeg[i]);
	}
    ((CStatic*)GetDlgItem(IDC_STATIC_FTPREC))->ShowWindow(TRUE);
    ((CStatic*)GetDlgItem(IDC_STATIC_FTPCAPJPEG))->ShowWindow(TRUE);
	for(i=0;i<7;i++)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_TIME1 + i))->SetCurSel(m_rulelinkinfo.m_timelist.pList[i].m_maskweek);

		sprintf(lpnum,"%02d",m_rulelinkinfo.m_timelist.pList[i].m_starthour);
		GetDlgItem(IDC_STARTHOUR1 + i)->SetWindowText(lpnum);
		sprintf(lpnum,"%02d",m_rulelinkinfo.m_timelist.pList[i].m_startmin);
		GetDlgItem(IDC_STARTMINUTE1 + i)->SetWindowText(lpnum);

		sprintf(lpnum,"%02d",m_rulelinkinfo.m_timelist.pList[i].m_stophour);
		GetDlgItem(IDC_STOPHOUR1 + i)->SetWindowText(lpnum);
		sprintf(lpnum,"%02d",m_rulelinkinfo.m_timelist.pList[i].m_stopmin);
		GetDlgItem(IDC_STOPMINUTE1 + i)->SetWindowText(lpnum);
	}
	for(i = 0 ; i < m_alarmouts ;  i ++)
	{
		((CButton*)GetDlgItem(IDC_CHECK_INTELLIGENT_ALARMOUT1 + i))->SetCheck(m_rulelinkinfo.m_outputmap[i]);
	}
	
	if(((CComboBox*)GetDlgItem(IDC_COMBO_INTELLIGENT_CHCOPY))->GetCount() <= 0)
	{	
		CString strtxt,strch;
		if(m_chs > 1)
		{
			strtxt = "ËùÓÐÍ¨µÀ";
			((CComboBox*)GetDlgItem(IDC_COMBO_INTELLIGENT_CHCOPY))->AddString(strtxt);
			for(i=0;i<m_chs;i++)
			{
				strch.LoadString(IDS_STRING123);
				strtxt.Format(strch,i+1);
				((CComboBox*)GetDlgItem(IDC_COMBO_INTELLIGENT_CHCOPY))->AddString(strtxt);
			}
			((CComboBox*)GetDlgItem(IDC_COMBO_INTELLIGENT_CHCOPY))->SetCurSel(0);
		}
	}
	UpdateData(FALSE);
}

void CDlgSmartTime::OnBtnSet() 
{
	if(!UpdateParam()) return;

	LONG   hParam;

	hParam = VSNET_ClientMessageOpen((char*)((const char*)m_sername),
		(char*)((const char*)m_serurl),(char*)((const char*)m_username),
		(char*)((const char*)m_password),m_serport);

	if(hParam)
	{
		VSNET_ClientMessageOpt(hParam,MESSAGE_CMD_SETVAPARAM,m_ch,&m_channvaex,NULL,NULL);
		if( m_bGetContactEx )
			VSNET_ClientMessageOpt(hParam,MESSAGE_CMD_SETVACONTACTEX,m_ch,&m_channva_contactex,NULL,NULL);
		
		VSNET_ClientMessageClose(hParam);
	}
}
BOOL CDlgSmartTime::UpdateParam()
{
	if(!UpdateData()) return FALSE;
    
	m_rulelinkinfo.m_enable = ((CButton*)GetDlgItem(IDC_CHECK_EN))->GetCheck();
	int i;
	char lpnum[3];
	for(i=0;i<7;i++)
	{   
		m_rulelinkinfo.m_timelist.pList[i].m_maskweek = ((CComboBox*)GetDlgItem(IDC_COMBO_TIME1 + i))->GetCurSel();
		GetDlgItem(IDC_STARTHOUR1 + i)->GetWindowText(lpnum,sizeof(lpnum));
		m_rulelinkinfo.m_timelist.pList[i].m_starthour = atoi(lpnum);
		
		GetDlgItem(IDC_STARTMINUTE1 + i)->GetWindowText(lpnum,sizeof(lpnum));
		m_rulelinkinfo.m_timelist.pList[i].m_startmin = atoi(lpnum);
		
		GetDlgItem(IDC_STOPHOUR1 + i)->GetWindowText(lpnum,sizeof(lpnum));
		m_rulelinkinfo.m_timelist.pList[i].m_stophour = atoi(lpnum);
		
		GetDlgItem(IDC_STOPMINUTE1 + i)->GetWindowText(lpnum,sizeof(lpnum));
		m_rulelinkinfo.m_timelist.pList[i].m_stopmin = atoi(lpnum);
	}
	BOOL btmListState = TimeListIsAllowed(m_rulelinkinfo.m_timelist);
	if (! btmListState)
	{
       memset(&m_rulelinkinfo, 0, sizeof(VSNETSMALARMLINK));
	   return FALSE;
	}
	for(i = 0 ;  i < 4 ; i ++)
	{
		m_rulelinkinfo.m_outputmap[i] = ((CButton*)GetDlgItem(IDC_CHECK_INTELLIGENT_ALARMOUT1 + i))->GetCheck();
	}
        for(i = 0 ; i < 2 ; i ++)
        {
		    m_rulelinkinfo.m_brecord[i] = ((CButton*)GetDlgItem(IDC_CHECK_RECV1 + i))->GetCheck();
		    m_rulelinkinfo.m_capjpeg[i] = ((CButton*)GetDlgItem(IDC_CHECK_CAPJPEG11 + i))->GetCheck();
            m_rulelinkinfo.m_ftp_alarmrec[i] = ((CButton*)GetDlgItem(IDC_CHECK_UPLOAD_RECORD1 + i))->GetCheck();
			m_rulelinkinfo.m_ftp_alarmcapjpeg[i] = ((CButton*)GetDlgItem(IDC_CHECK_UPLOAD_CAPJPEG1 + i))->GetCheck();
        }
	return TRUE;
}

void CDlgSmartTime::OnBtnNext() 
{
	// TODO: Add your control notification handler code here
    if (! UpdateParam())
    {
		return;
    }
	if ( ! m_fourthway)
	{
        m_fourthway = new CDlgSmartOther();
	    m_fourthway->Create(IDD_DLG_SMOTHER,g_dlgSmartBaseSet);
	}
	else
	{
		ShowWindow(SW_HIDE);
	}
	m_fourthway->ShowWindow(SW_SHOW);
//	m_fourthway->MoveWindow(g_dlgSmartBaseSet->m_prodlgpos);
    
}

void CDlgSmartTime::OnBtnPro() 
{
	// TODO: Add your control notification handler code here
    UpdateData();
	ShowWindow(SW_HIDE);
	CDlgWizardFirst* temp = (CDlgWizardFirst*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(0); 
	//m_second->PreparePloyRule(temp->m_rule);
	m_second->ShowWindow(SW_SHOW);
//	m_second->MoveWindow(g_dlgSmartBaseSet->m_prodlgpos);
}

void CDlgSmartTime::OnBtnCancel() 
{
	// TODO: Add your control notification handler code here
	g_dlgSmartBaseSet->m_ioperatestate = -1;
	g_dlgSmartBaseSet->m_cureditrule.pos = -1;
	g_dlgSmartBaseSet->m_cureditrule.ruletype = -1;
	g_dlgSmartBaseSet->SetBtnState(FALSE);
 	g_dlgSmartBaseSet->PostMessage(WM_SMWND_DESTOY);
}

void CDlgSmartTime::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	g_dlgSmartBaseSet->m_ioperatestate = -1;
	g_dlgSmartBaseSet->SetBtnState(FALSE);
	OnBtnCancel();
	CDialog::OnClose();
}

void CDlgSmartTime::InitialDlgData()
{
	if (g_dlgSmartBaseSet->m_ioperatestate == 3)
	{
		switch (g_dlgSmartBaseSet->m_cureditrule.ruletype)
		{
			case LINERULE:
				memcpy(&m_rulelinkinfo, &g_dlgSmartBaseSet->m_singleRules.m_lines[g_dlgSmartBaseSet->m_cureditrule.pos].m_alarmlink, sizeof(VSNETSMALARMLINK));
				break;
			case REGIONINRULE:
				memcpy(&m_rulelinkinfo, &g_dlgSmartBaseSet->m_regionInRule.m_alarmlink, sizeof(VSNETSMALARMLINK));
				break;
			case REGIONOUTRULE:
				memcpy(&m_rulelinkinfo, &g_dlgSmartBaseSet->m_regionOutRule.m_alarmlink, sizeof(VSNETSMALARMLINK));
				break;
			case REGIONHOVERRULE:
				memcpy(&m_rulelinkinfo, &g_dlgSmartBaseSet->m_regionHoverRule.m_alarmlink, sizeof(VSNETSMALARMLINK));
				break;
			case THINGLEAVEDRULE:
			case THINGMOVERULE:
			case THINGSTEALEDRULE:
			case ILLEGALPARKRULE:
			case NUMCOUNTRULE:
				break;
		}
       OnBtnGet();
	}
	else
	{
       char lpnum[3];
	   memset(&lpnum, 0, sizeof(lpnum));
	   ((CButton*)GetDlgItem(IDC_CHECK_EN))->SetCheck(1);
       for(int i=0;i<7;i++)
	   {  
		   if (i == 0)
		   {
	           ((CComboBox*)GetDlgItem(IDC_COMBO_TIME1 + i))->SetCurSel(9);
			   sprintf(lpnum,"%s","00");
			   GetDlgItem(IDC_STARTHOUR1 + i)->SetWindowText(lpnum);
			   sprintf(lpnum,"%s","00");
			   GetDlgItem(IDC_STARTMINUTE1 + i)->SetWindowText(lpnum);
			   sprintf(lpnum,"%s","23");
			   GetDlgItem(IDC_STOPHOUR1 + i)->SetWindowText(lpnum);
			   sprintf(lpnum,"%s","59");
	           GetDlgItem(IDC_STOPMINUTE1 + i)->SetWindowText(lpnum);
		   }
		   else
		   {
	          ((CComboBox*)GetDlgItem(IDC_COMBO_TIME1 + i))->SetCurSel(10);
	          sprintf(lpnum,"%s","00");
	          GetDlgItem(IDC_STARTHOUR1 + i)->SetWindowText(lpnum);
	          sprintf(lpnum,"%s","00");
	          GetDlgItem(IDC_STARTMINUTE1 + i)->SetWindowText(lpnum);
	          sprintf(lpnum,"%s","00");
	          GetDlgItem(IDC_STOPHOUR1 + i)->SetWindowText(lpnum);
	          sprintf(lpnum,"%s","00");
	          GetDlgItem(IDC_STOPMINUTE1 + i)->SetWindowText(lpnum);
		   }
	   }
	   for (i = 0; i < 2; i ++)
	   {
            ((CButton*)GetDlgItem(IDC_CHECK_RECV1 + i))->SetCheck(1);
		    ((CButton*)GetDlgItem(IDC_CHECK_CAPJPEG11 + i))->SetCheck(1);
		    ((CButton*)GetDlgItem(IDC_CHECK_UPLOAD_RECORD1 + i))->SetCheck(1);
		    ((CButton*)GetDlgItem(IDC_CHECK_UPLOAD_CAPJPEG1 + i))->SetCheck(1);
	   }
	   for (i = 0; i < 4; i ++)
	   {
		    ((CButton*)GetDlgItem(IDC_CHECK_INTELLIGENT_ALARMOUT1 + i))->SetCheck(1);
	   }
	}
}

BOOL CDlgSmartTime::TimeListIsAllowed(TIMELIST timelist)
{
	TIMELIST tmlist;
	memcpy(&tmlist, &timelist, sizeof(TIMELIST));
	for (int i = 0; i < 7; i ++)
	{   
		CString temp = _T("");
			temp.LoadString(IDS_STRING544);
     	if (tmlist.pList[i].m_starthour > 24 || tmlist.pList[i].m_stophour > 24 ||
			tmlist.pList[i].m_startmin >= 60 || tmlist.pList[i].m_stopmin >= 60)
     	{
			AfxMessageBox(temp);
	        return FALSE;
     	}
		else if (tmlist.pList[i].m_starthour > tmlist.pList[i].m_stophour)
		{
			AfxMessageBox(temp);
	        return FALSE;
		}
		else if(tmlist.pList[i].m_starthour == tmlist.pList[i].m_stophour)
		{
            if (tmlist.pList[i].m_startmin > tmlist.pList[i].m_stopmin)
            {
               AfxMessageBox(temp);
			   return FALSE;
            }
			else if(tmlist.pList[i].m_startmin == tmlist.pList[i].m_stopmin)
			{
                if (tmlist.pList[i].m_startmin != 0)
                {
					AfxMessageBox(temp);
					return FALSE;
                }
				else if (tmlist.pList[i].m_startmin == 0 && tmlist.pList[i].m_starthour != 0)
				{
					AfxMessageBox(temp);
					return FALSE;
				}
			}
		}
		else if ((tmlist.pList[i].m_starthour == 24 && tmlist.pList[i].m_startmin >= 60) ||
			(tmlist.pList[i].m_stophour == 24 && tmlist.pList[i].m_stopmin >= 60))
		{
			AfxMessageBox(temp);
			return FALSE;
		}
	}
	return TRUE;
}

void CDlgSmartTime::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);
	
	// TODO: Add your message handler code here
//	CRect rect;
//	GetWindowRect(&rect);
//   g_dlgSmartBaseSet->m_prodlgpos = rect;
}
