// DlgEndSet.cpp : implementation file
//

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

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


/////////////////////////////////////////////////////////////////////////////
// CDlgEndSet dialog

extern CDlgSmartBasicSet*   g_dlgSmartBaseSet;
CDlgEndSet::CDlgEndSet(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEndSet::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgEndSet)
	m_bStartRule = FALSE;
	m_sruleType = _T("");
	m_sruleName = _T("");
	//}}AFX_DATA_INIT
}


void CDlgEndSet::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgEndSet)
	DDX_Check(pDX, IDC_CHECK_STARTRULE, m_bStartRule);
	DDX_Text(pDX, IDC_EDIT_RULETYPE, m_sruleType);
	DDX_Text(pDX, IDC_EDIT_RULENAME, m_sruleName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgEndSet, CDialog)
	//{{AFX_MSG_MAP(CDlgEndSet)
	ON_BN_CLICKED(IDC_BTN_END, OnBtnEnd)
	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()

/////////////////////////////////////////////////////////////////////////////
// CDlgEndSet message handlers

BOOL CDlgEndSet::OnInitDialog() 
{
	CDialog::OnInitDialog();
	m_fourth = (CDlgSmartOther*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(3);
    m_fourth->ShowWindow(SW_HIDE);
	memset(&m_allparam, 0, sizeof(WHOLEDATA));
// TODO: Add extra initialization here
	g_dlgSmartBaseSet->m_ptrsetways.Add(this);
	InitialDlgData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEndSet::OnBtnEnd() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	sprintf(m_allparam.m_rulename,"%s", m_sruleName);
	DelAllWizard(TRUE);
	SaveRuleData();
	g_dlgSmartBaseSet->SetBtnState(FALSE);
	g_dlgSmartBaseSet->m_cureditrule.pos = -1;
	g_dlgSmartBaseSet->m_cureditrule.ruletype = -1;
	g_dlgSmartBaseSet->SaveAllParam();
	g_dlgSmartBaseSet->UpdataModify();
	g_dlgSmartBaseSet->PostMessage(WM_SMWND_DESTOY);
}

void CDlgEndSet::OnBtnPro() 
{
	// TODO: Add your control notification handler code here
    UpdateData();
	ShowWindow(SW_HIDE);
	m_fourth->ShowWindow(SW_SHOW);
//	m_fourth->MoveWindow(g_dlgSmartBaseSet->m_prodlgpos);
}

void CDlgEndSet::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);
}
//保存规则信息在m_allparam结构体中
void CDlgEndSet::DelAllWizard(BOOL bSavedparam)
{
	m_allparam.m_enable = m_bStartRule;
	int count = g_dlgSmartBaseSet->m_ptrsetways.GetSize();
	int i = 0;
    while(i < count)
	{  
		switch (i)
		{
		case 0:
			{
				CDlgWizardFirst* temp = (CDlgWizardFirst*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(i);
				if(bSavedparam)
				{
				  m_allparam.m_ruletype = temp->m_rule;
				}
				else
				{
					g_dlgSmartBaseSet->m_ptrsetways.RemoveAt(i);
					temp->DestroyWindow();
					delete temp;
					TRACE("CDlgWizardFirst --- destory \n");
				}	
				break;
			}
		case 1:
			{
				CDlgWizardSecond* temp = (CDlgWizardSecond*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(i);
				if(bSavedparam)
				{
				  if (! m_allparam.m_ruletype)
				  {
					  m_allparam.m_linealarm.m_sgline_beg_x = ((CSingleLineRule*)temp->m_myrulestatic.m_pSelectRule)->m_Rule.m_sgline_beg_x;
					  m_allparam.m_linealarm.m_sgline_beg_y = ((CSingleLineRule*)temp->m_myrulestatic.m_pSelectRule)->m_Rule.m_sgline_beg_y;
					  m_allparam.m_linealarm.m_sgline_drc = ((CSingleLineRule*)temp->m_myrulestatic.m_pSelectRule)->m_Rule.m_sgline_drc;
					  m_allparam.m_linealarm.m_sgline_end_x = ((CSingleLineRule*)temp->m_myrulestatic.m_pSelectRule)->m_Rule.m_sgline_end_x;
					  m_allparam.m_linealarm.m_sgline_end_y = ((CSingleLineRule*)temp->m_myrulestatic.m_pSelectRule)->m_Rule.m_sgline_end_y;
				  }
				  else
				  {
					  memset(&m_allparam.m_region, 0, sizeof(VSNETVAREGIONERULE));
					  m_allparam.m_region.m_region_valid = 1;
					  ((CRegionRule*)temp->m_myrulestatic.m_pSelectRule)->GetRuleFromRgn((UINT*)&m_allparam.m_region.m_packed_msk);
				  }
				  m_allparam.m_hovertime = temp->m_hovertime;
				}
				else
				{   
					temp->m_myrulestatic.Stop();
					g_dlgSmartBaseSet->m_ptrsetways.RemoveAt(i);
					temp->DestroyWindow();
					TRACE("CDlgWizardSecond --- destory \n");
					delete temp;
				}
				break;
			}
		case 2:
			{
				CDlgSmartTime* temp = (CDlgSmartTime*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(i);
				if(bSavedparam)
				{
					memcpy(&m_allparam.m_alarmlink, &temp->m_rulelinkinfo, sizeof(VSNETSMALARMLINK));
				}
				else
				{	
					g_dlgSmartBaseSet->m_ptrsetways.RemoveAt(i);
					temp->DestroyWindow();
					delete temp;
					TRACE("CDlgSmartTime --- destory \n");
				}
				break;
			}
		case 3:
			{
				CDlgSmartOther* temp = (CDlgSmartOther*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(i);
				if(bSavedparam)
				{
                    VSNETSMOSD Osdinfo[2];
					memset(&Osdinfo[0], 0, sizeof(VSNETSMOSD));
					memset(&Osdinfo[1], 0, sizeof(VSNETSMOSD));
					m_allparam.m_osdenable = temp->m_bEnableSerOsd;
                    for (int i = 0; i < 2; i ++) 
					{
						memcpy(&m_allparam.m_osd[i], &temp->m_osdinfo[i], sizeof(OSDINFO));
                    }                 
				}
				else
				{   
                    g_dlgSmartBaseSet->m_ptrsetways.RemoveAt(i);
					temp->DestroyWindow();
					delete temp;
					TRACE("CDlgSmartOther --- destory \n");
				}
				break;
			}
		}
     i ++;
	}			
}

void CDlgEndSet::OnClose() 
{
    if (IDYES == MessageBox("是否保存本规则的所有设置？","温馨提示",MB_YESNO))
    {
	    OnBtnEnd();
    }
	else
	{
	    OnBtnCancel();
	}
    g_dlgSmartBaseSet->m_ioperatestate = -1;
	g_dlgSmartBaseSet->SetBtnState(FALSE);
	CDialog::OnClose();
}

void CDlgEndSet::InitialDlgData()
{  
	if (! g_dlgSmartBaseSet->m_ioperatestate)//添加规则
	{
	    m_bStartRule = TRUE;
	}
	else//编辑规则
    {
		int ruletype = 0, index = 0;
		ruletype = g_dlgSmartBaseSet->m_cureditrule.ruletype;
		index = g_dlgSmartBaseSet->m_cureditrule.pos;
		switch (ruletype)
		{
		case LINERULE:
			m_bStartRule = g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_enable;
			break;
		case REGIONINRULE:
			m_bStartRule = g_dlgSmartBaseSet->m_regionInRule.m_enable;
			break;
		case REGIONOUTRULE:
			m_bStartRule = g_dlgSmartBaseSet->m_regionOutRule.m_enable;
			break;
		case REGIONHOVERRULE:
			m_bStartRule = g_dlgSmartBaseSet->m_regionHoverRule.m_enable;
			break;
		case THINGLEAVEDRULE:
		case THINGMOVERULE:
		case THINGSTEALEDRULE:
		case ILLEGALPARKRULE:
		case NUMCOUNTRULE:
			break;
		}
	}
   CDlgWizardFirst* temp = NULL;
   CString strtype = _T("");
   temp = (CDlgWizardFirst*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(0);
   m_sruleName = temp->m_rulename;
   strtype.LoadString(IDS_STRING534 + temp->m_rule);
   m_sruleType = strtype;
   UpdateData(FALSE);
}

BOOL CDlgEndSet::SaveRuleData()
{
   int operatetype = g_dlgSmartBaseSet->m_ioperatestate;
   int ruletype = g_dlgSmartBaseSet->m_cureditrule.ruletype;
   int index = g_dlgSmartBaseSet->m_cureditrule.pos;
   int count = g_dlgSmartBaseSet->m_singleRules.m_count;
   if (strlen(g_dlgSmartBaseSet->m_singleRules.m_lines[0].m_rulename) == 0)
   {
	   count = 0;
   }
   int curtype = m_allparam.m_ruletype;
   BOOL rgntoline = (index == -1 && curtype == 0 && ruletype != -1 && ruletype);
   if (!operatetype || rgntoline)
   {   
	   index = count;
   }
	switch(curtype)
	{   
		case LINERULE:
            { 
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_enable = m_allparam.m_enable;
				sprintf(g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_rulename, "%s", m_allparam.m_rulename);
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_sgline_beg_x = m_allparam.m_linealarm.m_sgline_beg_x;
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_sgline_beg_y = m_allparam.m_linealarm.m_sgline_beg_y;
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_sgline_drc = m_allparam.m_linealarm.m_sgline_drc;
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_sgline_end_x = m_allparam.m_linealarm.m_sgline_end_x;
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_sgline_end_y = m_allparam.m_linealarm.m_sgline_end_y;
				memcpy(&g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_alarmlink, &m_allparam.m_alarmlink, sizeof(ALARMLINK));
				g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_enableosd = m_allparam.m_osdenable;
				for (int i = 0; i < 2; i ++)
				{
                    memcpy(&g_dlgSmartBaseSet->m_singleRules.m_lines[index].m_osd[i], &m_allparam.m_osd[i], sizeof(OSDINFO));
				}
				if (index == count)
				{
					g_dlgSmartBaseSet->m_singleRules.m_count = count + 1;
				}
				if(rgntoline && operatetype)
				{
					DelOriginalRule(ruletype);
				}
				break;
			}
		case REGIONINRULE:
			{
				g_dlgSmartBaseSet->m_regionInRule.m_enable = m_allparam.m_enable;
				g_dlgSmartBaseSet->m_regionInRule.m_enabledefregion = m_allparam.m_region.m_region_valid;
				memcpy(g_dlgSmartBaseSet->m_regionInRule.m_packed_msk, m_allparam.m_region.m_packed_msk,sizeof(g_dlgSmartBaseSet->m_regionInRule.m_packed_msk));
				sprintf(g_dlgSmartBaseSet->m_regionInRule.m_rulename, "%s", m_allparam.m_rulename);
				memcpy(&g_dlgSmartBaseSet->m_regionInRule.m_alarmlink, &m_allparam.m_alarmlink, sizeof(ALARMLINK));
			    g_dlgSmartBaseSet->m_regionInRule.m_enableosd = m_allparam.m_osdenable;
				for (int i = 0; i < 2; i ++)
				{
				    memcpy(&g_dlgSmartBaseSet->m_regionInRule.m_osd[i], &m_allparam.m_osd[i], sizeof(OSDINFO));	
				}
			    if (ruletype != REGIONINRULE && operatetype)
				{
				    ruletype == 0 ? DelOriginalRule(ruletype, index):DelOriginalRule(ruletype);
				}
				break;
			}
		case REGIONOUTRULE:
			{
				g_dlgSmartBaseSet->m_regionOutRule.m_enable = m_allparam.m_enable;
                g_dlgSmartBaseSet->m_regionOutRule.m_enabledefregion = m_allparam.m_region.m_region_valid;
				memcpy(g_dlgSmartBaseSet->m_regionOutRule.m_packed_msk, m_allparam.m_region.m_packed_msk,sizeof(g_dlgSmartBaseSet->m_regionOutRule.m_packed_msk));
				sprintf(g_dlgSmartBaseSet->m_regionOutRule.m_rulename, "%s", m_allparam.m_rulename);
				memcpy(&g_dlgSmartBaseSet->m_regionOutRule.m_alarmlink, &m_allparam.m_alarmlink, sizeof(ALARMLINK));
				g_dlgSmartBaseSet->m_regionOutRule.m_enableosd = m_allparam.m_osdenable;
				for (int i = 0; i < 2; i ++)
				{
					memcpy(&g_dlgSmartBaseSet->m_regionOutRule.m_osd[i], &m_allparam.m_osd[i], sizeof(OSDINFO));	
				}
				if (ruletype != REGIONOUTRULE && operatetype)
				{   
					ruletype == 0 ? DelOriginalRule(ruletype, index):DelOriginalRule(ruletype);
				}
				break;
			}
			
		case REGIONHOVERRULE:
			{
				g_dlgSmartBaseSet->m_regionHoverRule.m_enable = m_allparam.m_enable;
				g_dlgSmartBaseSet->m_regionHoverRule.m_enabledefregion = m_allparam.m_region.m_region_valid;
				memcpy(g_dlgSmartBaseSet->m_regionHoverRule.m_packed_msk, m_allparam.m_region.m_packed_msk,sizeof(g_dlgSmartBaseSet->m_regionHoverRule.m_packed_msk));
				sprintf(g_dlgSmartBaseSet->m_regionHoverRule.m_rulename, "%s", m_allparam.m_rulename);
				g_dlgSmartBaseSet->m_regionHoverRule.m_hovertime = m_allparam.m_hovertime;
				memcpy(&g_dlgSmartBaseSet->m_regionHoverRule.m_alarmlink, &m_allparam.m_alarmlink, sizeof(ALARMLINK));
				g_dlgSmartBaseSet->m_regionHoverRule.m_enableosd = m_allparam.m_osdenable;
				for (int i = 0;i < 2; i ++)
				{
					memcpy(&g_dlgSmartBaseSet->m_regionHoverRule.m_osd[i], &m_allparam.m_osd[i], sizeof(OSDINFO));	
				}
				if (ruletype != REGIONHOVERRULE && operatetype)
				{
					ruletype == 0 ? DelOriginalRule(ruletype, index):DelOriginalRule(ruletype);
				}
				break;
			}
		case THINGLEAVEDRULE:
		case THINGMOVERULE:
		case THINGSTEALEDRULE:
		case ILLEGALPARKRULE:
		case NUMCOUNTRULE:
			    break;			
	}
   return TRUE;
}

void CDlgEndSet::DelOriginalRule(int originalrule, int index)
{
	switch (originalrule)
	{
	case LINERULE:
		{
            memset(&g_dlgSmartBaseSet->m_singleRules.m_lines[index], 0, sizeof(VSNETSMALARMLINE));
		    g_dlgSmartBaseSet->m_singleRules.m_count --;
		    break;
		}
	case REGIONINRULE:
		{
			memset(&g_dlgSmartBaseSet->m_regionInRule, 0, sizeof(VSNETSMREGIONINOUT));
		    break;
		}
	case REGIONOUTRULE:
		{
			memset(&g_dlgSmartBaseSet->m_regionOutRule, 0, sizeof(VSNETSMREGIONINOUT));
			break;
		}
	case REGIONHOVERRULE:
		{
			memset(&g_dlgSmartBaseSet->m_regionHoverRule, 0, sizeof(VSNETSMREGIONHOVER));
			break;
		}
		
	case THINGLEAVEDRULE:
    case THINGMOVERULE:
    case THINGSTEALEDRULE:
    case ILLEGALPARKRULE:
	case NUMCOUNTRULE:
		break;
   }
}

void CDlgEndSet::OnOK() 
{
	// TODO: Add extra validation here
	
//	CDialog::OnOK();
}

void CDlgEndSet::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;
}
