// DlgRuleSet.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Smart.h"
#include "DlgRuleSet.h"
#include "Rule.h"
#include "DlgIntelligentLog.h"
#include "DlgIntelligentHighSet.h"
#include "DlgEndSet.h"
#include "DlgSmartOther.h"
#include "DlgIntelligentTime.h"
#include "DlgWizardSecond.h"

#include <set>


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

#define  MAX_RULE_COUNT 9
#define SETITEMSTATE(nID,state)\
    ((CButton*)GetDlgItem(nID))->SetCheck(state);\
    GetDlgItem(nID)->EnableWindow(!state)\

#define OPERATORERROR(temp,nIDS)\
	CString temp = _T("");\
	temp.LoadString(nIDS);\
	AfxMessageBox(temp);\
	return\

//#define DELETECURWIZARDDLG() \
//	temp = (CObject*)m_ptrsetways.GetAt(0);\
//     m_ptrsetways.RemoveAt(0);\
//     delete temp\
/////////////////////////////////////////////////////////////////////////////
// CDlgSmartBasicSet dialog
CDlgSmartBasicSet*   g_dlgSmartBaseSet = NULL;

CDlgSmartBasicSet::CDlgSmartBasicSet(CWnd* pParent /*=NULL*/)
: CDialog(CDlgSmartBasicSet::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSmartBasicSet)
	m_object_max_area = 0;
	m_object_min_area = 0;
	m_tracktype = -1;
	//}}AFX_DATA_INIT
	g_dlgSmartBaseSet = this;
	m_hIcon_single_left   = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_SINGLE_LEFT),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_SINGLE_LEFT);
    m_hIcon_single_right  = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_SINGLE_RIGHT),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_SINGLE_RIGHT);
    m_hIcon_single_double = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_SINGLE_DOUBLE),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_SINGLE_DOUBLE);
	
    m_hIcon_multi_left    = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_MULTI_LEFT),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_MULTI_LEFT);
    m_hIcon_multi_right   = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_MULTI_RIGHT),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_MULTI_RIGHT);
    m_hIcon_multi_double  = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_MULTI_DOUBLE),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_MULTI_DOUBLE);
	
	//    m_hIcon_region_fill      = (HICON)::LoadImage(AfxGetInstanceHandle(),
	//		MAKEINTRESOURCE(IDI_ICON_REGION_FILL),
	//		IMAGE_ICON,
	//		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_REGION_FILL);
    m_hIcon_region_pencil    = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_REGION_BRUSH),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_REGION_BRUSH);
    m_hIcon_region_line      = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_REGION_POLYGON),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_REGION_POLYGON);
    m_hIcon_region_erase     = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_REGION_ERASE),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_REGION_ERASE);
    m_hIcon_region_rect      = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_REGION_RECT),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_REGION_RECT);
    m_hIcon_region_cleanall  = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_REGION_CLEANALL),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_REGION_CLEANALL);
	
    m_hIcon_select          = (HICON)::LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(IDI_ICON_SELECT),
		IMAGE_ICON,
		16,16,0);//AfxGetApp()->LoadIcon(IDI_ICON_SELECT);
	
	
    m_prevItemID = 0;
    m_bShowAlarmMsg = TRUE;
    m_szAlarmMsg    = _T("");
	m_pEditTree     = NULL;
	m_bIsEditStatus = FALSE;
	m_dlgWizardFirst = NULL;
    m_ioperatestate = -1;
	m_proselectline = -1;
	m_pRule = NULL;
	m_ptrsetways.RemoveAll();
	memset(m_preRuleStartFlags, 0, sizeof(m_preRuleStartFlags));
	memset(m_curRuleStartFlags, 0, sizeof(m_curRuleStartFlags));
	m_prodlgpos.left = 0;
	m_prodlgpos.top = 0;
	m_sername = _T("video server");
	m_url = _T("192.168.1.81");
	m_port = 3000;
	m_password = _T("123");
	m_username = _T("admin");
}


void CDlgSmartBasicSet::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSmartBasicSet)
	DDX_Control(pDX, IDC_SLIDER_SENSI, m_tracksensi);
	DDX_Control(pDX, IDC_LIST_RULES, m_myrulelist);
	DDX_Control(pDX, IDC_SLIDER_LINE_WIDTH, m_hSliderLineWidth);
	DDX_Control(pDX, IDC_STATIC_IMAGE, m_rulesStatic);
	DDX_Control(pDX,IDC_TREE_RULE,m_ruletree);
	DDX_Text(pDX, IDC_EDIT_OBJECT_MAXAREA, m_object_max_area);
	DDX_Text(pDX, IDC_EDIT_OBJECT_MINAREA, m_object_min_area);
	DDX_Radio(pDX, IDC_RADIO_ALL, m_tracktype);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSmartBasicSet, CDialog)
//{{AFX_MSG_MAP(CDlgSmartBasicSet)
ON_BN_CLICKED(IDC_BTN_NEWRULE, OnBtnNewRule)
ON_BN_CLICKED(IDC_BTN_DELRULE, OnBtnDelRule)
ON_BN_CLICKED(IDC_BTN_COPYRULE, OnBtnCopyRule)
ON_BN_CLICKED(IDC_BTN_APPLAY, OnBtnApplay)
ON_BN_CLICKED(IDC_BTN_GETRULE, OnBtnGetrule)
ON_BN_CLICKED(IDC_BTN_SET_SHIELD_MSK, OnBtnSetShieldMsk)
ON_BN_CLICKED(IDC_BTN_GETVAPARAM, OnBtnGetvaparam)
ON_BN_CLICKED(IDC_BTN_SETVAPARAM, OnBtnSetvaparam)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_LINE_WIDTH, OnCustomdrawSliderLineWidth)
ON_NOTIFY(TVN_SELCHANGED,IDC_TREE_RULE,OnSelchangedRuleTree)
ON_NOTIFY(NM_CLICK,IDC_TREE_RULE,OnClickRuleTree)
ON_NOTIFY(NM_RCLICK,IDC_TREE_RULE,OnRClickRuleTree)
ON_NOTIFY(TVN_BEGINLABELEDIT,IDC_TREE_RULE,OnBeginEditRuleTree)
ON_NOTIFY(TVN_ENDLABELEDIT,IDC_TREE_RULE,OnEndEditRuleTree)
ON_COMMAND(IDC_MENU_SM_MODIFY_RULENAME,OnEditTreeLabel)
ON_BN_CLICKED(IDC_BTN_HIGHPARA, OnBtnHighpara)
ON_BN_CLICKED(IDC_BTN_TURE, OnBtnTure)
ON_BN_CLICKED(IDC_BTN_CANCEL, OnBtnCancel)
ON_WM_PAINT()
ON_NOTIFY(NM_CLICK, IDC_LIST_RULES, OnClickListRules)
ON_BN_CLICKED(IDC_BTN_ENDSET, OnBtnEndset)
ON_BN_CLICKED(IDC_BTN_GET, OnBtnGet)
ON_MESSAGE(WM_SMWND_DESTOY,OnDelAllWizardDlg)
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_SENSI, OnCustomdrawSliderSensi)
	ON_BN_CLICKED(IDC_RADIO_ALL, OnRadioAll)
	ON_BN_CLICKED(IDC_RADIO_CAR, OnRadioCar)
	ON_BN_CLICKED(IDC_RADIO_PERSON, OnRadioPerson)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSmartBasicSet message handlers

BOOL CDlgSmartBasicSet::OnInitDialog() 
{
	CRect rc(0,0,0,0);
	((CTabCtrl*)GetParent())->GetClientRect(&rc);
	((CTabCtrl*)GetParent())->AdjustRect(FALSE,&rc);
	MoveWindow(&rc,TRUE);
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_hImageList.Create(IDB_ICON_RULETREE,16,0,RGB(255,255,255));
	m_ruletree.SetImageList(&m_hImageList,TVSIL_STATE);
	
	m_rulesStatic.SetWindowPos(NULL,0,0,352,288,SWP_NOMOVE);
    PostMessage(WM_COMMAND,MAKEWPARAM(IDC_BTN_PLAY,BN_CLICKED),0);
    //PostMessage(WM_COMMAND,MAKEWPARAM(IDC_BTN_SHOWALARM,BN_CLICKED),(LPARAM)GetDlgItem(IDC_BTN_SHOWALARM)->GetSafeHwnd());
	
    ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT))->SetIcon(m_hIcon_select);
	//    ((CButton*)GetDlgItem(IDC_BTN_REGION_FILL))->SetIcon(m_hIcon_region_fill);
    ((CButton*)GetDlgItem(IDC_BTN_REGION_PENCIL))->SetIcon(m_hIcon_region_pencil);
    ((CButton*)GetDlgItem(IDC_BTN_REGION_LINE))->SetIcon(m_hIcon_region_line);
    ((CButton*)GetDlgItem(IDC_BTN_REGION_ERASE))->SetIcon(m_hIcon_region_erase);
    ((CButton*)GetDlgItem(IDC_BTN_REGION_RECT))->SetIcon(m_hIcon_region_rect);
    ((CButton*)GetDlgItem(IDC_BTN_REGION_CLEANALL))->SetIcon(m_hIcon_region_cleanall);
	
    CString   szToolTip;
	
    m_toolTip.Create(this);
	m_toolTip.Activate(TRUE);
	
    szToolTip.LoadString(IDS_STRING489);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_SINGLE_DRC_LEFT),szToolTip);
    szToolTip.LoadString(IDS_STRING490);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_SINGLE_DRC_RIGHT),szToolTip);
    szToolTip.LoadString(IDS_STRING491);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_SINGLE_DRC_DOUBLE),szToolTip);
	//    szToolTip.LoadString(IDS_STRING492);
	//    m_toolTip.AddTool(GetDlgItem(IDC_BTN_REGION_FILL),szToolTip);
    szToolTip.LoadString(IDS_STRING493);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_REGION_ERASE),szToolTip);
    szToolTip.LoadString(IDS_STRING494);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_REGION_CLEANALL),szToolTip);
    szToolTip.LoadString(IDS_STRING495);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_REGION_LINE),szToolTip);
    szToolTip.LoadString(IDS_STRING496);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_REGION_PENCIL),szToolTip);
    szToolTip.LoadString(IDS_STRING497);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_REGION_RECT),szToolTip);
    szToolTip.LoadString(IDS_STRING498);
    m_toolTip.AddTool(GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT),szToolTip);
	szToolTip.LoadString(IDS_STRING499);
	m_toolTip.AddTool(&m_hSliderLineWidth,szToolTip);
	szToolTip.LoadString(IDS_STRING511);
	m_toolTip.AddTool(&m_ruletree,szToolTip);
	
	szToolTip.LoadString(IDS_STRING505);
	m_ruletree.InsertItem(szToolTip);
	//szToolTip.LoadString(IDS_STRING506);
	//m_ruletree.InsertItem(szToolTip);
	szToolTip.LoadString(IDS_STRING507);
	m_ruletree.InsertItem(szToolTip);
	szToolTip.LoadString(IDS_STRING508);
	m_ruletree.InsertItem(szToolTip);
	szToolTip.LoadString(IDS_STRING509);
	m_ruletree.InsertItem(szToolTip);
	szToolTip.LoadString(IDS_STRING510);
	m_ruletree.InsertItem(szToolTip);
	
	m_hSliderLineWidth.SetRange(1,50);
	m_hSliderLineWidth.SetPos(1);
	
	m_tracksensi.SetRange(1,100);
	m_tracksensi.SetPos(1);

    CreateList();
	OnBtnGetvaparam();
    GetRuleInfo();
    InitialRuleList();
	LoadTrackType();
	LoadTrackSensitivity();
	return TRUE; 
}

void CDlgSmartBasicSet::OnOK()
{
	
}

void CDlgSmartBasicSet::OnCancel()
{
	
}

void CDlgSmartBasicSet::OnDestroy()
{
	g_dlgSmartBaseSet = NULL;
	CDialog::OnDestroy();
}

BOOL CDlgSmartBasicSet::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if( HIWORD(wParam) == BN_CLICKED )
        OnEditBtnClicked(LOWORD(wParam));
	
	return CDialog::OnCommand(wParam, lParam);
}

BOOL CDlgSmartBasicSet::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message == WM_MOUSEMOVE)
	{
		memset(m_preRuleStartFlags, 0, sizeof(m_preRuleStartFlags));
		GetRulesStartState();
		m_toolTip.Activate(TRUE);
		int index = 0;
		index = m_myrulelist.GetCurSel();
		if (index == -1 && !((CButton*)GetDlgItem(IDC_BTN_MINAREA))->GetCheck() &&
			!((CButton*)GetDlgItem(IDC_BTN_MAXAREA))->GetCheck() && 
			!((CButton*)GetDlgItem(IDC_BTN_SET_SHIELD_MSK))->GetCheck())
		{
			CRegionRule* region = new CRegionRule(0);
			VSNETVAREGIONERULE reginfo;
			memset(&reginfo, 0, sizeof(VSNETVAREGIONERULE));
			reginfo.m_region_valid = 1;
			memset((char*)reginfo.m_packed_msk, 0X00, sizeof(reginfo.m_packed_msk));
			region->SetRule(&reginfo);
			m_rulesStatic.SetCurRule(region);
		}
	}
    else if (pMsg->message == WM_KEYUP)
	{   
		if (pMsg->wParam == VK_UP || pMsg->wParam == VK_DOWN)
		{
			SetState();
		}
		else
		{
            m_rulesStatic.OnKeyControl(pMsg->wParam, pMsg->message == WM_KEYDOWN);
		}
    }
	m_toolTip.RelayEvent(pMsg);
	return CDialog::PreTranslateMessage(pMsg);
}
void CDlgSmartBasicSet::OnEditBtnClicked(int nBtnID)
{
    BOOL    bProcess = FALSE;
	
    switch( nBtnID )
    {
    case IDC_BTN_PLAY:
        m_rulesStatic.Play();
		//OnBtnGetrule();
		//GetRuleInfo();
		SETITEMSTATE(IDC_BTN_PLAY,TRUE);
		SETITEMSTATE(IDC_BTN_PAUSE,FALSE);
        return;
        break;
		
    case IDC_BTN_PAUSE:
        m_rulesStatic.Pause();   
        SETITEMSTATE(IDC_BTN_PLAY,FALSE);
        SETITEMSTATE(IDC_BTN_PAUSE,TRUE);
        return;
        break;
    case IDC_BTN_SINGLE_DRC_LEFT:
        m_rulesStatic.SetSingleLineRuleDrc(LINE_DRC_LEFT);
        bProcess = TRUE;
        break;
		
    case IDC_BTN_SINGLE_DRC_RIGHT:
        m_rulesStatic.SetSingleLineRuleDrc(LINE_DRC_RIGHT);
        bProcess = TRUE;
        break;
		
    case IDC_BTN_SINGLE_DRC_DOUBLE:
		m_rulesStatic.SetSingleLineRuleDrc(LINE_DRC_DOUBLE);
		bProcess = TRUE;
        break;
    case IDC_BTN_SINGLE_DRC_SELECT:
        {
            BOOL bMove = (((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT))->GetCheck());
            if( !m_rulesStatic.EnableRuleMove( bMove ) )
            {
                ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT))->SetCheck(!bMove);
            }
        }
        break;
    case IDC_BTN_REGION_PENCIL:
		m_rulesStatic.SetEditActionType(EDITRULE_ACTION_PENCIL);
		bProcess = TRUE;
        break;
    case IDC_BTN_REGION_ERASE:
		m_rulesStatic.SetEditActionType(EDITRULE_ACTION_ERASE);
		bProcess = TRUE;
        break;
    case IDC_BTN_REGION_CLEANALL:
        m_rulesStatic.RegionCleanAll();
        break;
    case IDC_BTN_REGION_RECT:
        m_rulesStatic.SetEditActionType(EDITRULE_ACTION_REGION_RECT);
        bProcess = TRUE;
        break;
    case IDC_BTN_REGION_FILL:
		if(m_rulesStatic.m_polylinefinish)
		{
			m_rulesStatic.SetEditActionType(EDITRULE_ACTION_REGION_FILL);
			m_rulesStatic.m_polylinefinish = FALSE;
			bProcess = TRUE;
		}
		else
		{
			AfxMessageBox("所绘制的封闭线没有完成，请完成后再填充！");
			SETITEMSTATE(IDC_BTN_REGION_FILL,FALSE);
			GetDlgItem(IDC_BTN_REGION_FILL)->UpdateWindow();
			bProcess = FALSE;
		}
        break;
    case IDC_BTN_REGION_LINE:
        m_rulesStatic.SetEditActionType(EDITRULE_ACTION_REGION_POLYGON);
        bProcess = TRUE;
        break;
    case IDC_BTN_MINAREA:
		m_rulesStatic.SetEditActionType(EDITRULE_ACTION_OBJECT_MIN_AREA);
        break;
    case IDC_BTN_MAXAREA:
		m_rulesStatic.SetEditActionType(EDITRULE_ACTION_OBJECT_MAX_AREA);
    }
	if(nBtnID == IDC_BTN_MINAREA || nBtnID == IDC_BTN_MAXAREA)
	{
		CRect rc(0,0,0,0);
		m_rulesStatic.GetWindowRect(&rc);
		m_rulesStatic.DrawArea(rc.CenterPoint());
	}
    if( !bProcess ) return;
    
    if( m_prevItemID != 0 )
    {
        SETITEMSTATE(m_prevItemID,FALSE);
    }
	
    SETITEMSTATE(nBtnID,TRUE);
    m_prevItemID = nBtnID;
	
	if(nBtnID == IDC_BTN_REGION_PENCIL || nBtnID == IDC_BTN_REGION_ERASE)
		GetDlgItem(IDC_SLIDER_LINE_WIDTH)->ShowWindow(SW_SHOW);
	else
		GetDlgItem(IDC_SLIDER_LINE_WIDTH)->ShowWindow(SW_HIDE);
}

BOOL CDlgSmartBasicSet::LoadRulesSettings()
{
    BOOL iRet       = m_rulesStatic.LoadRulesSettings();
	
	if(!iRet)return FALSE;
	/*
	int         rulesCount,index;
    CMyRule*      pRule;
	
	  rulesCount = m_rulesStatic.GetRulesCount();
	  index      = 0;
	  HTREEITEM root = m_ruletree.GetRootItem();
	  while (root != NULL)
	  {
	  DeleteChildItem(m_ruletree,root);
	  root = m_ruletree.GetNextSiblingItem(root);
	  }
	  
		if (rulesCount > 0)
		{
        CString  szInfo;
		
		  index      = 0;
		  while (index < rulesCount)
		  {
		  pRule = m_rulesStatic.GetRuleByIndex(index);
		  ASSERT (pRule);
		  
            AddRule(index, pRule);
            index++;
			}
			}
	*/
    return iRet;
}

void CDlgSmartBasicSet::OnBtnNewRule() 
{   
	memset(m_allowedrules, 0, sizeof(m_allowedrules));
    GetAllowedAddRules(m_allowedrules);//是否允许增加规则，不能增加在该函数中直接return
    RULETYPEPOS temp;
	memset(&temp, 0 ,sizeof(RULETYPEPOS));
	temp.pos = -1;
	temp.ruletype = -1;
	
    BOOL bAllowedAdd = FALSE;
	for (int i = 0; i < MAX_RULE_COUNT; i ++)
	{
		if (m_allowedrules[i] > 0)
		{
			bAllowedAdd = TRUE;
		}
	}
	if (! bAllowedAdd)
	{
		OPERATORERROR(temp, IDS_STRING543);
	}
	else
	{
		SetBtnState();
	}
	OperateRules(0,temp);//增加规则
    if (! g_dlgSmartBaseSet->m_ptrsetways.GetSize())
    {
		m_dlgWizardFirst = NULL;
    }
	if (! m_dlgWizardFirst)
	{
		m_dlgWizardFirst = new CDlgWizardFirst;
		m_dlgWizardFirst->Create(IDD_DLG_SMWIZARD1,g_dlgSmartBaseSet);
	}
	m_dlgWizardFirst->ShowWindow(SW_SHOW);
}

void CDlgSmartBasicSet::BtnNewRule(int index) 
{   
	SetBtnState();
	memset(m_allowedrules, 0, sizeof(m_allowedrules));
    GetAllowedAddRules(m_allowedrules);
    RULETYPEPOS temp;
	memset(&temp, 0 ,sizeof(RULETYPEPOS));
	temp.pos = -1;
	temp.ruletype = -1;
	if (- 1 == m_cureditrule.ruletype)
	{
        BOOL bAllowedAdd = FALSE;
		for (int i = 0; i < MAX_RULE_COUNT; i ++)
		{
			if (m_allowedrules[i] > 0)
			{
				bAllowedAdd = TRUE;
			}
		}
		if (! bAllowedAdd)
		{
			
			OPERATORERROR(temp, IDS_STRING543);
		}
		OperateRules(0,temp);
	}
	else
	{
		GetAllowedAddRules(m_allowedrules, index);
		OperateRules(3,temp);
	}
    if (! g_dlgSmartBaseSet->m_ptrsetways.GetSize())
    {
		m_dlgWizardFirst = NULL;
    }
	if (! m_dlgWizardFirst)
	{
		m_dlgWizardFirst = new CDlgWizardFirst;
		m_dlgWizardFirst->Create(IDD_DLG_SMWIZARD1,g_dlgSmartBaseSet);
	}
	m_dlgWizardFirst->ShowWindow(SW_SHOW);
}

void CDlgSmartBasicSet::OnSelchangedRuleTree(NMHDR* pNMHDR, LRESULT* pResult)
{
	*pResult = 0;
}
void CDlgSmartBasicSet::OnClickRuleTree(NMHDR* pNMHDR, LRESULT* pResult)
{
	CPoint pt;
	GetCursorPos(&pt);
	m_ruletree.ScreenToClient(&pt);
	int index = -1;
	HTREEITEM hItem = m_ruletree.HitTest(pt);
	if(hItem == NULL)
	{
		hItem = m_ruletree.GetSelectedItem();
		if(hItem != NULL)
		{
			if(IsRootItem(m_ruletree,hItem))
			{
				HTREEITEM hChild = m_ruletree.GetChildItem(hItem);
				if(hChild != NULL)
					index = m_ruletree.GetItemData(hChild);
				else
					index = -1;
			}
			else
				index = m_ruletree.GetItemData(hItem);
			ChangeSelRule(index);
		}
		*pResult = 1;
		return;
	}
	CRect rc;
	m_ruletree.GetItemRect(hItem,&rc,TRUE);
	CMyRule *pRule = NULL;
	if(rc.PtInRect(pt))
	{
		m_ruletree.SelectItem(hItem);
		((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT))->SetCheck(FALSE);
	}
	else
	{
		rc.left -= 15;
		if(rc.PtInRect(pt))
		{
			m_ruletree.SetCheck(hItem,!m_ruletree.GetCheck(hItem));
			if(IsRootItem(m_ruletree,hItem))
			{
				HTREEITEM hChild = m_ruletree.GetChildItem(hItem);
				while (hChild != NULL)
				{
					m_ruletree.SetCheck(hChild,m_ruletree.GetCheck(hItem));
					index = m_ruletree.GetItemData(hChild);
					pRule = m_rulesStatic.GetRuleByIndex(index);
					if(pRule != NULL)
					{
						pRule->m_bEnable = m_ruletree.GetCheck(hChild);
					}
					hChild = m_ruletree.GetNextSiblingItem(hChild);
				}
				if(GetTypeByName(m_ruletree.GetItemText(hItem)) == 1 && pRule != NULL)
				{
					((CSingleLineRule*)pRule)->CheckMultiLineRule(pRule->m_bEnable);
				}
			}
			else
			{
				index = m_ruletree.GetItemData(hItem);
				pRule = m_rulesStatic.GetRuleByIndex(index);
				if(pRule != NULL)
				{
					pRule->m_bEnable = m_ruletree.GetCheck(hItem);
				}
				hItem = m_ruletree.GetParentItem(hItem);
				if(hItem != NULL)
				{
					if(GetTypeByName(m_ruletree.GetItemText(hItem)) == 1 && pRule != NULL)
					{
						((CSingleLineRule*)pRule)->CheckMultiLineRule(pRule->m_bEnable);
					}
				}
			}
		}
		rc.left -= 15;
		rc.right = rc.left + 10;
		if(rc.PtInRect(pt))
		{
			m_ruletree.Expand(hItem,TVE_TOGGLE);
		}
	}
	*pResult = 1;
}
void CDlgSmartBasicSet::OnRClickRuleTree(NMHDR* pNMHDR, LRESULT* pResult)
{
	CPoint pt;
	GetCursorPos(&pt);
	m_ruletree.ScreenToClient(&pt);
	HTREEITEM hItem = m_ruletree.HitTest(pt);
	if(hItem == NULL)return;
	m_ruletree.SelectItem(hItem);
	if(IsRootItem(m_ruletree,hItem))return;
	m_ruletree.ClientToScreen(&pt);
	CMenu basemenu;
	basemenu.LoadMenu(IDR_MENU_SMRULETREE);
	CMenu *popup = basemenu.GetSubMenu(0);
	popup->TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this);
	
	*pResult = 1;
}
void CDlgSmartBasicSet::OnEditTreeLabel()
{
	HTREEITEM hItem = m_ruletree.GetSelectedItem();
	if(hItem == NULL)return;
	m_pEditTree = m_ruletree.EditLabel(hItem);
}
void CDlgSmartBasicSet::OnBeginEditRuleTree(NMHDR* pNMHDR, LRESULT* pResult)
{
	TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
	if(pTVDispInfo->item.hItem == NULL || m_ruletree.ItemHasChildren(pTVDispInfo->item.hItem))
	{
		*pResult = 1;
		return;
	}
	m_bIsEditStatus = TRUE;
	
	*pResult = 0;
}
void CDlgSmartBasicSet::OnEndEditRuleTree(NMHDR* pNMHDR, LRESULT* pResult)
{
	TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
	HTREEITEM hItem = pTVDispInfo->item.hItem;
	if(hItem == NULL)
	{
		*pResult = 0;
		return;
	}
	CString   szNewText;
	m_pEditTree->GetWindowText(szNewText);
	m_ruletree.SetItemText(hItem,szNewText);
	m_ruletree.UpdateWindow();
	int index = (int)m_ruletree.GetItemData(hItem);
	CMyRule *pRule = m_rulesStatic.GetRuleByIndex(index);
	if(pRule != NULL)
	{
		pRule->m_ruleName = szNewText;
		pRule->m_bEnable = m_ruletree.GetCheck(hItem);
	}
	m_bIsEditStatus = FALSE;
	
	*pResult = 0;
}
void CDlgSmartBasicSet::ChangeSelRule(int index,BOOL bRegionMsk)
{
    if (m_prevItemID != 0)
    {
		SETITEMSTATE(m_prevItemID,FALSE);
	}
	
    if (index == -1 && !bRegionMsk)
    {
        GetDlgItem(IDC_BTN_SINGLE_DRC_LEFT)->ShowWindow(FALSE);
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_LEFT))->SetCheck(FALSE);
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_RIGHT)->ShowWindow(FALSE);
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_RIGHT))->SetCheck(FALSE);
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_DOUBLE)->ShowWindow(FALSE);
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_DOUBLE))->SetCheck(FALSE);
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT)->ShowWindow(FALSE);
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT))->SetCheck(FALSE);
		
        GetDlgItem(IDC_BTN_REGION_FILL)->ShowWindow(FALSE);
        GetDlgItem(IDC_BTN_REGION_PENCIL)->ShowWindow(FALSE);
        GetDlgItem(IDC_BTN_REGION_LINE)->ShowWindow(FALSE);
        GetDlgItem(IDC_BTN_REGION_ERASE)->ShowWindow(FALSE);
        GetDlgItem(IDC_BTN_REGION_RECT)->ShowWindow(FALSE);
        GetDlgItem(IDC_BTN_REGION_CLEANALL)->ShowWindow(FALSE);
    }
    else
    {    
        CMyRule*  pSelectRule;   
        BOOL    bVisible,bCheck,bSingleLine;
        m_rulesStatic.SetCurSelRuleByIndex(index);
		
        pSelectRule = m_rulesStatic.GetRuleByIndex(index);
        bVisible    = pSelectRule && pSelectRule->m_ruleType == RULE_TYPE_SINGLELINE && !bRegionMsk;
        bSingleLine = bVisible && !((CSingleLineRule*)pSelectRule)->m_bMultiSegment;
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_LEFT)->ShowWindow(bVisible);
        if( bVisible )
        {
            bCheck  = ((CSingleLineRule*)pSelectRule)->m_Rule.m_sgline_drc == LINE_DRC_LEFT;
            if( bCheck )
                m_prevItemID = IDC_BTN_SINGLE_DRC_LEFT;
            
            ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_LEFT))->SetIcon(bSingleLine?m_hIcon_single_left:m_hIcon_multi_left);
        }
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_LEFT))->SetCheck( bVisible && bCheck );
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_RIGHT)->ShowWindow(bVisible);
        if( bVisible )
        {
            bCheck = ((CSingleLineRule*)pSelectRule)->m_Rule.m_sgline_drc == LINE_DRC_RIGHT;
            if( bCheck )
                m_prevItemID = IDC_BTN_SINGLE_DRC_RIGHT;
			
            ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_RIGHT))->SetIcon(bSingleLine?m_hIcon_single_right:m_hIcon_multi_right);
        }
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_RIGHT))->SetCheck( bVisible && bCheck );
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_DOUBLE)->ShowWindow(bVisible);
        if( bVisible )
        {
            bCheck = ((CSingleLineRule*)pSelectRule)->m_Rule.m_sgline_drc == LINE_DRC_DOUBLE;
            if( bCheck )
                m_prevItemID = IDC_BTN_SINGLE_DRC_DOUBLE;
			
            ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_DOUBLE))->SetIcon(bSingleLine?m_hIcon_single_double:m_hIcon_multi_double);
        }
        ((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_DOUBLE))->SetCheck( bVisible && bCheck );
		
        bVisible  = bRegionMsk || (pSelectRule && pSelectRule->m_ruleType == RULE_TYPE_REGION);
		//        GetDlgItem(IDC_BTN_REGION_FILL)->ShowWindow(bVisible);
        GetDlgItem(IDC_BTN_REGION_PENCIL)->ShowWindow(bVisible);
        GetDlgItem(IDC_BTN_REGION_LINE)->ShowWindow(bVisible);
        GetDlgItem(IDC_BTN_REGION_ERASE)->ShowWindow(bVisible);
        GetDlgItem(IDC_BTN_REGION_RECT)->ShowWindow(bVisible);
        GetDlgItem(IDC_BTN_REGION_CLEANALL)->ShowWindow(bVisible);
		
        GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT)->ShowWindow(bRegionMsk || pSelectRule != NULL);
        GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT)->EnableWindow(pSelectRule != NULL && pSelectRule->m_ruleType == RULE_TYPE_SINGLELINE);
		
        if (!bRegionMsk)
        {
            if (((CButton*)GetDlgItem(IDC_BTN_SET_SHIELD_MSK))->GetCheck())
            {
                ((CButton*)GetDlgItem(IDC_BTN_SET_SHIELD_MSK))->SetCheck(FALSE);
                PostMessage(WM_COMMAND,MAKEWPARAM(IDC_BTN_SET_SHIELD_MSK,BN_CLICKED),(LPARAM)GetDlgItem(IDC_BTN_SET_SHIELD_MSK)->GetSafeHwnd());
            }
        }
    }
    m_rulesStatic.SetEditActionType(EDITRULE_ACTION_NONE);
}

void CDlgSmartBasicSet::OnBtnDelRule() 
{   
	memset(m_allowedrules, 0, sizeof(m_allowedrules));
	GetAllowedAddRules(m_allowedrules);
    int index = -1;
	index = m_myrulelist.GetCurSel();
	if (-1 == index)
	{
		OPERATORERROR(temp,IDS_STRING530);
	}
    RULETYPEPOS delrule;
	memset(&delrule, 0, sizeof(RULETYPEPOS));
	delrule.pos = -1;
	delrule.ruletype = -1;
    GetRuleInfobyIndex(index,delrule);
    OperateRules(1,delrule);
	//m_myrulelist.DeleteItem(index);
    SaveAllParam();
	m_myrulelist.DeleteAllItems();
	GetRuleInfo();
    InitialRuleList();
	Invalidate();
}

void CDlgSmartBasicSet::OnBtnCopyRule() 
{   
	memset(m_allowedrules, 0, sizeof(m_allowedrules));
	GetAllowedAddRules(m_allowedrules);
	int rulescount = m_myrulelist.GetItemCount();
	int index = -1; 
	index = m_myrulelist.GetCurSel();
	if (-1 == index)
	{
		OPERATORERROR(temp,IDS_STRING528);
	}
	CString copedrulename = _T("");
	BOOL bRet = FALSE;
	bRet = SetCopyedRuleName(index, copedrulename);
	if (bRet)
	{
		RULETYPEPOS ruletypos;
		memset(&ruletypos, 0, sizeof(RULETYPEPOS));
		ruletypos.pos = -1;
		ruletypos.ruletype = -1;
		GetRuleInfobyIndex(index, ruletypos);
		OperateRules(2, ruletypos, copedrulename);
	}
	SaveAllParam();
	m_myrulelist.DeleteAllItems();
	GetRuleInfo();
    InitialRuleList();
	Invalidate();
}

void CDlgSmartBasicSet::OnBtnApplay() 
{
    m_rulesStatic.SaveRuelsSettings();
}

void CDlgSmartBasicSet::AddRule(int index,CMyRule *pRule)
{
    CString    szInfo;
	
    if (index >= 0)
    {
        switch (pRule->m_ruleType)
        {
        case RULE_TYPE_SINGLELINE:
            szInfo = ((CSingleLineRule*)pRule)->m_bMultiSegment?"多段绊线规则":"绊线规则";
            break;
        case RULE_TYPE_DOUBLELINE:
            szInfo = "双线规则";
            break;
        case RULE_TYPE_REGION:
            if (((CRegionRule*)pRule)->m_regionDrc == 0 )
            {
                szInfo = "区域进入规则";
            }
            else if (((CRegionRule*)pRule)->m_regionDrc == 1)
            {
                szInfo = "区域离开规则";
            }
            else if (((CRegionRule*)pRule)->m_regionDrc == 2)
            {
                szInfo = "区域徘徊规则";
            }
            break;
        }
		HTREEITEM root = FindRootItem(m_ruletree,szInfo);
		if(root == NULL)return;
		int nCount = GetChildItemCount(m_ruletree,root);
        HTREEITEM hItem = m_ruletree.InsertItem(pRule->m_ruleName,root);
		if(hItem == NULL)return;
		m_ruletree.SetItemData(hItem,index);
		m_ruletree.SelectItem(hItem);
		m_ruletree.SetCheck(hItem,pRule->m_bEnable);
		if(IsAllChildCheck(m_ruletree,root))
			m_ruletree.SetCheck(root);
		else
			m_ruletree.SetCheck(root,FALSE);
    }
}
HTREEITEM	CDlgSmartBasicSet::FindRootItem(CTreeCtrl& tree,CString ItemText)
{
	HTREEITEM hItem = tree.GetRootItem();
	CString szItemText;
	while(hItem != NULL)
	{
		szItemText = tree.GetItemText(hItem);
		if(szItemText == ItemText)return hItem;
		hItem = tree.GetNextSiblingItem(hItem);
	}
	return NULL;
}
int CDlgSmartBasicSet::GetChildItemCount(CTreeCtrl& tree,HTREEITEM root)
{
	if(root == NULL)return 0;
	int nCount = 0;
	HTREEITEM child = tree.GetChildItem(root);
	while(child != NULL)
	{
		nCount ++;
		child = tree.GetNextSiblingItem(child);
	}
	return nCount;
}
void CDlgSmartBasicSet::OnBtnGetrule() 
{
    BOOL iRet = LoadRulesSettings();
	
	HTREEITEM root = m_ruletree.GetRootItem();
	while(root != NULL)
	{
		m_ruletree.Expand(root,TVE_EXPAND);
		root = m_ruletree.GetNextSiblingItem(root);
	}
	root = m_ruletree.GetSelectedItem();
	if(root != NULL)
	{
		int index = (int)m_ruletree.GetItemData(root);
		ChangeSelRule(index);
	}
    GetDlgItem(IDC_BTN_APPLAY)->EnableWindow(iRet);
	((CButton*)GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT))->SetCheck(FALSE);
}

void CDlgSmartBasicSet::OnObjectMinAreaChange(int areaVal)
{
    CString     szValue;
	
    szValue.Format("%d", areaVal < 0 ? -areaVal : areaVal);
    GetDlgItem(IDC_EDIT_OBJECT_MINAREA)->SetWindowText(szValue);
}

void CDlgSmartBasicSet::OnObjectMaxAreaChange(int areaVal)
{
    CString     szValue;
    
    szValue.Format("%d", areaVal < 0 ? -areaVal : areaVal);
    GetDlgItem(IDC_EDIT_OBJECT_MAXAREA)->SetWindowText(szValue);
}

void CDlgSmartBasicSet::OnBtnSetShieldMsk() 
{   
	if(((CButton*)GetDlgItem(IDC_BTN_SET_SHIELD_MSK))->GetCheck())
	{
		ChangeSelRule(-1, TRUE);
		m_rulesStatic.EditShieldMskRegion(TRUE);
	}
    
}

BOOL CDlgSmartBasicSet::LoadVAParam()
{
    BOOL    iRet1,iRet2,iRet3;
	
    m_rulesStatic.LoadVAParam(iRet1, iRet2, iRet3);
    if (iRet2)
    {
        CString   szText;
        
        szText.Format("%d", m_rulesStatic.m_vaobjectarea.m_obj_min_area);
        GetDlgItem(IDC_EDIT_OBJECT_MINAREA)->SetWindowText(szText);
		//m_object_min_area = szText;
		
        szText.Format("%d", m_rulesStatic.m_vaobjectarea.m_obj_max_area);
        GetDlgItem(IDC_EDIT_OBJECT_MAXAREA)->SetWindowText(szText);
		//m_object_max_area = szText;
		UpdateData();
    }
	
    if (iRet3)
    {
        ((CButton*)GetDlgItem(IDC_CHECK_ALARM_WND_ENABLE))->SetCheck(m_rulesStatic.m_vaalarmsettings.m_alm_window_enable);
        ((CButton*)GetDlgItem(IDC_CHECK_ALARM_DOTLINE_ENABLE))->SetCheck(m_rulesStatic.m_vaalarmsettings.m_alm_dotline_enable);
        ((CButton*)GetDlgItem(IDC_CHECK_ALARM_STABILIZATION_ENABLE))->SetCheck(m_rulesStatic.m_vaalarmsettings.m_stabilization_enable);
        ((CButton*)GetDlgItem(IDC_CHECK_REGION_SHIELD_ENABLE))->SetCheck(m_rulesStatic.m_vaalarmsettings.m_region_shield_enable);
    }
    return TRUE;
}

void CDlgSmartBasicSet::OnBtnGetvaparam() 
{
    BOOL iRet = LoadVAParam();
    GetDlgItem(IDC_BTN_SETVAPARAM)->EnableWindow(iRet);
}

void CDlgSmartBasicSet::OnBtnSetvaparam() 
{
	memset(m_allowedrules, 0, sizeof(m_allowedrules));
	GetAllowedAddRules(m_allowedrules);
	int index = -1;
	index = m_myrulelist.GetCurSel();
	if (-1 == index)
	{
		OPERATORERROR(temp,IDS_STRING529);
	}
	SetBtnState();
	m_cureditrule.ruletype = -1;
	m_cureditrule.pos = -1;
	GetRuleInfobyIndex(index,m_cureditrule);
    BtnNewRule(index);
}
void  CDlgSmartBasicSet::Stop()
{
	m_rulesStatic.Stop();
}
void CDlgSmartBasicSet::OnCustomdrawSliderLineWidth(NMHDR* pNMHDR, LRESULT* pResult) 
{
	m_rulesStatic.ChangeRegionLineWidth(m_hSliderLineWidth.GetPos());	
	*pResult = 0;
}
void CDlgSmartBasicSet::DeleteChildItem(CTreeCtrl& tree,HTREEITEM root)
{
	if(tree.GetCount() <= 0)return;
	if(!tree.ItemHasChildren(root))return;
	HTREEITEM slibing,child = tree.GetChildItem(root);
	while (child != NULL)
	{
		slibing = tree.GetNextSiblingItem(child);
		tree.DeleteItem(child);
		child = slibing;
	}
}
BOOL CDlgSmartBasicSet::IsRootItem(CTreeCtrl& tree,HTREEITEM hItem)
{
	HTREEITEM root = tree.GetRootItem();
	while (root != NULL)
	{
		if(root == hItem)
			return TRUE;
		root = tree.GetNextSiblingItem(root);
	}
	return FALSE;
}
BOOL CDlgSmartBasicSet::IsAllChildCheck(CTreeCtrl& tree,HTREEITEM root)
{
	if(!tree.ItemHasChildren(root))return FALSE;
	HTREEITEM hChild = tree.GetChildItem(root);
	while(hChild != NULL)
	{
		if(!tree.GetCheck(hChild))return FALSE;
		hChild = tree.GetNextSiblingItem(hChild);
	}
	return TRUE;
}
void CDlgSmartBasicSet::UpdateRuleIndex(CTreeCtrl& tree,HTREEITEM hItem,int index /* = 0 */)
{
	if(hItem == NULL)return;
	int oldindex = tree.GetItemData(hItem);
	if(oldindex > index)
	{
		tree.SetItemData(hItem,oldindex - 1);
	}
	HTREEITEM hChild = tree.GetChildItem(hItem);
	UpdateRuleIndex(tree,hChild,index);
	HTREEITEM hSlibing = tree.GetNextSiblingItem(hChild);
	while(hSlibing != NULL)
	{
		UpdateRuleIndex(tree,hSlibing,index);
		hSlibing = tree.GetNextSiblingItem(hSlibing);
	}
}
void CDlgSmartBasicSet::UpdateRuleIndex(CTreeCtrl& tree,int index /* = 0 */)
{
	HTREEITEM hRoot = tree.GetRootItem();
	while(hRoot != NULL)
	{
		UpdateRuleIndex(tree,hRoot,index);
		hRoot = tree.GetNextSiblingItem(hRoot);
	}
}
CString CDlgSmartBasicSet::GetNameByType(int ruleType)
{
	CString  lpReturn = _T("");
	switch (ruleType)
	{
	case 0:
		lpReturn = _T("绊线规则");
		break;
	case 1:
		lpReturn = _T("多段绊线规则");
		break;
	case 2:
		lpReturn = _T("区域进入规则");
		break;
	case 3:
		lpReturn = _T("区域离开规则");
		break;
	case 4:
		lpReturn = _T("区域徘徊规则");
		break;
	}
	return lpReturn;
}
int CDlgSmartBasicSet::GetTypeByName(CString ruleName)
{
	int  type = -1;
	if(ruleName == _T("绊线规则"))
		type = 0;
	else if(ruleName == _T("多段绊线规则"))
		type = 1;
	else if(ruleName == _T("区域进入规则"))
		type = 2;
	else if(ruleName == _T("区域离开规则"))
		type = 3;
	else if(ruleName == _T("区域徘徊规则"))
		type = 4;
	return type;
}
void CDlgSmartBasicSet::ChangeBtnStatus(HTREEITEM hItem)
{
	if(hItem == NULL)
		hItem = m_ruletree.GetSelectedItem();
	if(hItem == NULL)
		return;
	CString ItemText;
	int     RuleType = -1;
	BOOL    bChild = FALSE;
	if(IsRootItem(m_ruletree,hItem))
	{
		ItemText = m_ruletree.GetItemText(hItem);
		bChild = FALSE;
	}
	else
	{
		HTREEITEM hParent = m_ruletree.GetParentItem(hItem);
		if(hParent != NULL)
			ItemText = m_ruletree.GetItemText(hParent);
		bChild = TRUE;
	}
	{
		RuleType = GetTypeByName(ItemText);
		BOOL   bMax = FALSE;
		if(RuleType == 0)
		{
			if(m_rulesStatic.GetSingleLineRuleCount() >= SINGLELINE_RULE_MAXCOUNT)
				bMax = TRUE;
			else
				bMax = FALSE;
		}
		else if(RuleType == 1)
		{
			if(m_rulesStatic.GetMultiLineRuleCount() >= MULTILINE_RULE_MAXCOUNT ||
				m_rulesStatic.GetSingleLineRuleCount() >= SINGLELINE_RULE_MAXCOUNT)
				bMax = TRUE;
			else
				bMax = FALSE;
		}
		else if(RuleType == 2)
		{
			if(m_rulesStatic.GetRegionRuleCount(REGION_DRC_ENTER) >= REGION_RULE_MAXCOUNT)
				bMax = TRUE;
			else 
				bMax = FALSE;
		}
		else if(RuleType == 3)
		{
			if(m_rulesStatic.GetRegionRuleCount(REGION_DRC_EXIT) >= REGION_RULE_MAXCOUNT)
				bMax = TRUE;
			else 
				bMax = FALSE;
		}
		else if(RuleType == 4)
		{	
			if(m_rulesStatic.GetRegionRuleCount(REGION_DRC_HORVER) >= REGION_RULE_MAXCOUNT)
				bMax = TRUE;
			else 
				bMax = FALSE;
		}
		if(bMax && bChild)
		{
			GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(FALSE);
			GetDlgItem(IDC_BTN_DELRULE)->EnableWindow();
		}
		else if(bMax && !bChild)
		{
			GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(FALSE);
			GetDlgItem(IDC_BTN_DELRULE)->EnableWindow(FALSE);
		}
		else if(!bMax && bChild)
		{
			GetDlgItem(IDC_BTN_NEWRULE)->EnableWindow();
			GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow();
			GetDlgItem(IDC_BTN_DELRULE)->EnableWindow();
		}
		else
		{
			GetDlgItem(IDC_BTN_NEWRULE)->EnableWindow();
			GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(FALSE);
			GetDlgItem(IDC_BTN_DELRULE)->EnableWindow(FALSE);
		}
	}
}

void CDlgSmartBasicSet::OnBtnHighpara() 
{
	CDlgSmartHighSet *higherSet;
	higherSet = NULL;
	higherSet = new CDlgSmartHighSet;
	if (higherSet == NULL)
	{
		return;
	}
	higherSet->DoModal();
	delete higherSet;
	higherSet = NULL;
}
BOOL CDlgSmartBasicSet::GetRuleInfo()
{
	memset(&m_cureditrule, 0, sizeof(RULETYPEPOS));
	m_cureditrule.pos = -1;
	m_cureditrule.ruletype = -1;
//	for (int i = 0; i < 10 ; i ++)
//	{
	//	memset(&m_singleRules.m_lines[i], 0, sizeof(VSNETSMALARMLINE));
	//	memset(m_singleRules.m_lines[i].m_rulename, 0, sizeof(m_singleRules.m_lines[i].m_rulename));
	//}
	memset(&m_singleRules, 0, sizeof(VSNETSMALARMLINES));
	memset(&m_regionInRule, 0, sizeof(VSNETSMREGIONINOUT));
	memset(m_regionInRule.m_rulename, 0, sizeof(m_regionInRule.m_rulename));
	memset(&m_regionOutRule, 0, sizeof(VSNETSMREGIONINOUT));
	memset(m_regionInRule.m_rulename, 0, sizeof(m_regionOutRule.m_rulename));
	memset(&m_regionHoverRule, 0, sizeof(VSNETSMREGIONHOVER));
	memset(m_regionInRule.m_rulename, 0, sizeof(m_regionHoverRule.m_rulename));
	BOOL bRet = LoadRulesSettings();
	return bRet;
}
void CDlgSmartBasicSet::InitialRuleList()
{   
	int  current = 0;
    BOOL bpreventstate = FALSE;
	int  alarmtype = 0;
	CString temp = _T("");
	temp.LoadString(IDS_STRING534 + alarmtype ++);
	
	for (current = 0; current < m_singleRules.m_count; current ++)
	{   
#if 0
        if (strlen(m_singleRules.m_lines[current].m_rulename) <= 0)
		{   
			continue ;
		}
#endif
        m_myrulelist.InsertItem(current, m_singleRules.m_lines[current].m_rulename);
		m_myrulelist.SetItemText(current, 1, temp);
		m_myrulelist.SetCheckbox(current, 2, m_singleRules.m_lines[current].m_enable);
		bpreventstate =	m_singleRules.m_lines[current].m_alarmlink.m_enable;
		if (bpreventstate)
		{
			m_myrulelist.SetItemText(current, 3, "布防");
		}
		else
		{
			m_myrulelist.SetItemText(current, 3, "未布防");
		}
	}
	temp.LoadString(IDS_STRING534 + alarmtype ++);
    if (strlen(m_regionInRule.m_rulename) > 0)
    {
        m_myrulelist.InsertItem(current, m_regionInRule.m_rulename);
        m_myrulelist.SetItemText(current, 1, temp);
        m_myrulelist.SetCheckbox(current, 2, m_regionInRule.m_enable);
        bpreventstate = m_regionInRule.m_alarmlink.m_enable;
        if (bpreventstate)
        {
            m_myrulelist.SetItemText(current, 3, "布防");
        }
        else
        {
            m_myrulelist.SetItemText(current, 3, "未布防");  
        }
	}
    else
	{
		current --;
	}
	temp.LoadString(IDS_STRING534 + alarmtype ++);
	if (strlen(m_regionOutRule.m_rulename) > 0)
	{
		current ++;
		m_myrulelist.InsertItem(current, m_regionOutRule.m_rulename);
		m_myrulelist.SetItemText(current, 1, temp);
		m_myrulelist.SetCheckbox(current, 2, m_regionOutRule.m_enable);
		bpreventstate = m_regionOutRule.m_alarmlink.m_enable;
		if (bpreventstate)
		{
			m_myrulelist.SetItemText(current, 3, "布防");
		}
		else
		{
			m_myrulelist.SetItemText(current, 3, "未布防");  
		}
	} 
	temp.LoadString(IDS_STRING534 + alarmtype ++);
	if (strlen(m_regionHoverRule.m_rulename) > 0)
	{
		current ++;
		m_myrulelist.InsertItem(current, m_regionHoverRule.m_rulename);
		m_myrulelist.SetItemText(current, 1, temp);
		m_myrulelist.SetCheckbox(current, 2, m_regionHoverRule.m_enable);
		bpreventstate = m_regionHoverRule.m_alarmlink.m_enable;
		if (bpreventstate)
		{
			m_myrulelist.SetItemText(current, 3, "布防");
		}
		else
		{
			m_myrulelist.SetItemText(current, 3, "未布防");  
		}
	} 
	if (m_proselectline != -1)
	{
		m_myrulelist.SetCurSel(m_proselectline);
	}
}

void CDlgSmartBasicSet::OnBtnTure() 
{	
	// TODO: Add your control notification handler code here
	SaveAllParam();
	SaveTrackType();
	SaveTrackSensitivity();
	::PostMessage(GetParent()->m_hWnd, WM_CLOSE, 0, 0);
}

void CDlgSmartBasicSet::OnBtnCancel() 
{
	// TODO: Add your control notification handler code here
	::PostMessage(GetParent()->m_hWnd, WM_CLOSE, 0, 0);
	CDialog::OnCancel();
}

BOOL CDlgSmartBasicSet::SaveParamToServer()
{
	CString   szText;
    GetDlgItem(IDC_EDIT_OBJECT_MINAREA)->GetWindowText(szText);
    m_rulesStatic.m_vaobjectarea.m_obj_min_area = atoi(szText);
    GetDlgItem(IDC_EDIT_OBJECT_MAXAREA)->GetWindowText(szText);
    m_rulesStatic.m_vaobjectarea.m_obj_max_area = atoi(szText);
	
    if (m_rulesStatic.m_vaobjectarea.m_obj_min_area >= m_rulesStatic.m_vaobjectarea.m_obj_max_area)
	{
		CString temp = _T("");
		temp.LoadString(IDS_STRING531);
		AfxMessageBox(temp);
		return FALSE;
	}
    
    m_rulesStatic.m_vaalarmsettings.m_alm_window_enable    = ((CButton*)GetDlgItem(IDC_CHECK_ALARM_WND_ENABLE))->GetCheck();
    m_rulesStatic.m_vaalarmsettings.m_alm_dotline_enable   = ((CButton*)GetDlgItem(IDC_CHECK_ALARM_DOTLINE_ENABLE))->GetCheck();
    m_rulesStatic.m_vaalarmsettings.m_stabilization_enable = ((CButton*)GetDlgItem(IDC_CHECK_ALARM_STABILIZATION_ENABLE))->GetCheck();
    m_rulesStatic.m_vaalarmsettings.m_region_shield_enable = ((CButton*)GetDlgItem(IDC_CHECK_REGION_SHIELD_ENABLE))->GetCheck();
	
    m_rulesStatic.SaveVAParam();
	return TRUE;
}

void CDlgSmartBasicSet::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
//	CRect rect;
	//m_myrulelist.GetWindowRect(&rect);
	//::InvalidateRect(m_myrulelist.m_hWnd, &rect, FALSE);
	// Do not call CDialog::OnPaint() for painting messages
}

void CDlgSmartBasicSet::OnClickListRules(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	memset(m_curRuleStartFlags, 0, sizeof(m_curRuleStartFlags));
	GetRulesStartState(TRUE);
	if (CompareModify())
	{
        SetRulesEnableModify();
		SaveAllParam();
		m_proselectline = m_myrulelist.GetCurSel();
		m_myrulelist.DeleteAllItems();
		GetRuleInfo();
		InitialRuleList();
	}
	SetState();
	*pResult = 0;
}

void CDlgSmartBasicSet::OnBtnEndset() 
{
	((CButton*)GetDlgItem(IDC_BTN_SET_SHIELD_MSK))->SetCheck(0);
    SaveParamToServer();
	int index = -1;
	index = m_myrulelist.GetCurSel();
	ChangeSelRule(index);
	m_rulesStatic.EditShieldMskRegion(FALSE);
}   

int CDlgSmartBasicSet::GetRuleCounts(CString ruleType)
{
	int rulecounts = 0;
	int count = m_myrulelist.GetItemCount();
	for (int i = 0; i < count; i ++)
	{
		char ruletype[20];
		CString temp = _T("");
		memset(ruletype, 0, sizeof(ruletype));
		m_myrulelist.GetItemText(i, 1, ruletype, sizeof(ruletype));
        temp.Format("%s", ruletype);
		if (temp == ruleType)
		{
			rulecounts ++;
		}
	}
	return rulecounts;		
}

int CDlgSmartBasicSet::GetCurCopyNum(CString copyedrulename, int index)
{
	int copycurnum = 0;
	CString temp= _T("");
	temp = copyedrulename.Right(6);
	temp = temp.Left(5);
	if (temp == "_copy")
	{
		temp = copyedrulename.Left(copyedrulename.GetLength() - 6);
	}
	else
		temp = copyedrulename;
	for (int i = 0; i < m_myrulelist.GetItemCount(); i ++)
	{   
		if (index == i)
		{
			continue;
		}
		CString temp1 =_T("");
		char rulename[60];
		memset(rulename, 0, sizeof(rulename));
		m_myrulelist.GetItemText(i, 0, rulename,sizeof(rulename));
		CString temp2 = _T("");
		temp1.Format("%s", rulename);
		temp2 = temp1.Right(6);
		temp2 = temp2.Left(5);
		if (temp2 == "_copy")
		{
			temp1 = temp1.Left(temp1.GetLength() - 6);
		}
		if (temp1 == temp)
		{
			copycurnum ++;
		}
	}
	return  copycurnum;
}

void CDlgSmartBasicSet::GetAllowedAddRules(BYTE* allowedrules, int index)
{   
	UpdateData();
	int j = 0;
	CString ruletype = _T("");
	ruletype.LoadString(IDS_STRING534);
	int rulecounts = GetRuleCounts(ruletype);
	if (rulecounts >= 10)
		allowedrules[j ++] = (BYTE)0;
	else
        allowedrules[j ++] = (BYTE)1;
	int typeindex = 1;
	for (int i = 0; i < 3; i ++)
	{
		ruletype.LoadString(IDS_STRING534 + typeindex++);
		rulecounts = GetRuleCounts(ruletype);
		if (rulecounts >= 1)
			allowedrules[j ++] = (BYTE)0;
		else
			allowedrules[j ++] = (BYTE)1;
	}
    for (int x = 0; x < 5; x ++)
    {
		allowedrules[j ++] = (BYTE)0;
    }
	if (index != -1)
	{
		allowedrules[m_cureditrule.ruletype] = (BYTE)1;
	}
}
BOOL CDlgSmartBasicSet::GetPreventState(TIMELIST timelist)
{
	TIMELIST templist;
	memcpy(&templist, &timelist, sizeof(TIMELIST));
	for (int i = 0; i < 7; i ++)
	{
		if (templist.pList[i].m_maskweek != 10)
		{
			if (templist.pList[i].m_starthour < templist.pList[i].m_stophour || templist.pList[i].m_startmin < templist.pList[i].m_stopmin)
			{
				return TRUE;
			}
		}
	}
    return FALSE;
}
void CDlgSmartBasicSet::GetRuleInfobyIndex(int index, RULETYPEPOS& ruletypepos)
{
	char  rulename[60];
	char  ruletype[20];
	memset(rulename, 0, sizeof(rulename));
	memset(ruletype, 0, sizeof(ruletype));
	m_myrulelist.GetItemText(index, 0, rulename, sizeof(rulename));
	m_myrulelist.GetItemText(index, 1, ruletype, sizeof(ruletype));
	CString temptype = _T("");
	CString tempname = _T("");
	tempname.Format("%s", rulename);
	temptype.Format("%s", ruletype);
	CString temprules = _T("");
	temprules.LoadString(IDS_STRING534);
	if (temptype == temprules)
	{
		ruletypepos.ruletype = 0;
		for (int j = 0; j < m_singleRules.m_count; j ++)
		{
			if (m_singleRules.m_lines[j].m_rulename == tempname)
			{
				ruletypepos.pos = j;
				return;
			}
		}
	}
	int typeindex = 1;
	for (int ii = 0; ii < 8; ii ++)
	{
        temprules.LoadString(IDS_STRING534 + typeindex ++);
		if (temptype == temprules)
		{
			ruletypepos.ruletype = typeindex - 1;
			return;
		}
	}
}

void CDlgSmartBasicSet::OperateRules(int operatortype, RULETYPEPOS& ruletypepos, CString typerulename)
{
	switch (operatortype)
	{
	case ADDRULE:
	case EDITRULE:
		{
			m_ioperatestate = operatortype;
			break;
		}
	case DELTULE:
	case COPYRULE:
		{
			switch (ruletypepos.ruletype)
			{
			case LINERULE:
				{
					if (operatortype == COPYRULE)
					{
						memcpy(&m_singleRules.m_lines[m_singleRules.m_count], &m_singleRules.m_lines[ruletypepos.pos], sizeof(VSNETSMALARMLINE));
						memcpy(m_singleRules.m_lines[m_singleRules.m_count].m_rulename, typerulename.GetBuffer(0),sizeof(m_singleRules.m_lines[m_singleRules.m_count].m_rulename));
						m_singleRules.m_count = m_singleRules.m_count + 1;
						break;
					}
					for (int i = 0; i < m_singleRules.m_count; i ++)
					{
						if (ruletypepos.pos == m_singleRules.m_count -1)
						{
							memset(&m_singleRules.m_lines[ruletypepos.pos], 0, sizeof(VSNETSMALARMLINE));
							m_singleRules.m_count = m_singleRules.m_count - 1;
							break;
						}
						if (i == ruletypepos.pos)
						{
							for (int j = i; j < m_singleRules.m_count - 1; j ++)
							{
								memcpy(&m_singleRules.m_lines[j], &m_singleRules.m_lines[j + 1], sizeof(VSNETSMALARMLINE));
							}
							m_singleRules.m_count = m_singleRules.m_count - 1;
							break;
						}
					}
					break;
				}
				
			case REGIONINRULE:
				{
					memset(&m_regionInRule, 0 ,sizeof(VSNETSMREGIONINOUT));
					m_regionInRule.m_enabledefregion = 0;
					break;
				}
			case REGIONOUTRULE:
				{
					memset(&m_regionOutRule, 0, sizeof(VSNETSMREGIONINOUT));
					m_regionOutRule.m_enabledefregion = 0;
					break;
				}
				
			case REGIONHOVERRULE:
				{
					memset(&m_regionHoverRule, 0, sizeof(VSNETSMREGIONHOVER));
					m_regionHoverRule.m_enabledefregion = 0;
					break;
				}
			case THINGLEAVEDRULE:
			case THINGMOVERULE:
			case THINGSTEALEDRULE:
			case ILLEGALPARKRULE:
			case NUMCOUNTRULE: 
				break;
			}
			break;
		}
	}
}
//设置复制后的规则名字
BOOL CDlgSmartBasicSet::SetCopyedRuleName(int index, CString& curcopyedname)
{
	CString temp = _T("");
	temp.LoadString(IDS_STRING534);
	if (10 <= GetRuleCounts(temp))
	{
		CString temp = _T("");
		temp.LoadString(IDS_STRING533);
		AfxMessageBox(temp);
		return FALSE;
	}
	char strrulename[60];
	char strruletype[20];
	int isStart = -1;
	char stralarmstate[8];
	memset(strrulename, 0, sizeof(strrulename));
	memset(strrulename, 0, sizeof(strruletype));
    memset(stralarmstate, 0, sizeof(stralarmstate));
	m_myrulelist.GetItemText(index, 0, strrulename, sizeof(strrulename));
	m_myrulelist.GetItemText(index, 1, strruletype, sizeof(strruletype));
	isStart = m_myrulelist.GetCheckbox(index, 2);
    m_myrulelist.GetItemText(index, 3, stralarmstate, sizeof(stralarmstate));
	CString strTemp = _T("");
	strTemp.Format(strrulename);
	int rulecopyednum = GetCurCopyNum(strTemp, index);
	temp.LoadString(IDS_STRING534);
	int singlerules = GetRuleCounts(temp);
	if (0 == rulecopyednum)
	{
		CString copyname = _T("");
		copyname = strTemp.Right(6);
		copyname = copyname.Left(5);
		if (copyname == "_copy")
		{
			TCHAR ch = strTemp.GetAt(strTemp.GetLength() - 1);
            for (int i = 1; i < 10; i ++)
            {
				if ((ch - '0') != i)
				{
					strTemp.SetAt(strTemp.GetLength() - 1,i + '0');
					break;
				}
            }
		}
		else
		{
            strTemp += "_copy1";
		}
	}
	else
	{
		int copy[10];
		int j = 0;
		memset(copy, 0 ,sizeof(copy));
		for (int i = 0; i < singlerules; i ++)
		{
			memset(strrulename, 0, sizeof(strrulename));
			m_myrulelist.GetItemText(i, 0, strrulename,sizeof(strrulename));
			CString temp1 = _T("");
			temp1.Format("%s", strrulename);
			CString temp2 = _T("");
			CString temp3 = _T("");
			temp2 = temp1.Right(6);
			temp2 = temp2.Left(5);
			if ("_copy" == temp2)
			{
				temp3 = temp1.Left(temp1.GetLength() - 6);
                CString temp4 = _T("");
				if (strTemp.GetLength() > 6)
				{
					temp4 = strTemp.Left(strTemp.GetLength() - 6);
					CString temp5 = _T("");
					temp5 = strTemp.Right(6);
					temp5 = temp5.Left(5);
					if (temp5 != "_copy")
					{
						temp4 = strTemp;
					}
				}
				else
				{
					temp4 = strTemp;
				}
				if (temp3 == temp4)
				{
					TCHAR ctemp = temp1.GetAt(temp1.GetLength() - 1);
					copy[j ++] =ctemp - '0';
				}
			}
		}
		std::set<int> iSet;
		iSet.clear();
		for (int ii= 0; ii< j; ii++)
		{
			iSet.insert(copy[ii]);
		}
		int size = iSet.size();
		int iname = -1;
		for (int iii = 1; iii < 10; iii ++)
		{  
			iSet.insert(iii);
			if (iSet.size() > size)
			{
				iname = iii;
				break;
			}
		}
		TCHAR ch = '0' + iname;
		if (iname == -1)
		{
			ch = '0';
		}
		CString temp5 = _T("");
		temp5 = strTemp.Right(6);
		temp5 = temp5.Left(5);
		if (temp5 == _T("_copy"))
		{
			strTemp.SetAt(strTemp.GetLength() - 1, ch);
		}
		else
		{
			strTemp = strTemp + "_copy" + ch;
		}		
	}
    curcopyedname = strTemp;
	m_myrulelist.InsertItem(singlerules, strTemp.GetBuffer(0));
	m_myrulelist.SetItemText(singlerules, 1, strruletype);
    m_myrulelist.SetCheckbox(singlerules, 2, isStart);
	m_myrulelist.SetItemText(singlerules, 3, stralarmstate);
	return TRUE;
}
void CDlgSmartBasicSet::SetBtnState(BOOL bAllowedOperate)
{
	GetDlgItem(IDC_BTN_SINGLE_DRC_SELECT)->EnableWindow(! bAllowedOperate);
	//    GetDlgItem(IDC_BTN_REGION_FILL)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_REGION_PENCIL)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_REGION_LINE)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_REGION_ERASE)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_REGION_RECT)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_REGION_CLEANALL)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_SLIDER_LINE_WIDTH)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_PLAY)->EnableWindow(! bAllowedOperate);
    GetDlgItem(IDC_BTN_PAUSE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_NEWRULE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_HIGHPARA)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_CHECK_ALARM_STABILIZATION_ENABLE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_ENDSET)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_SET_SHIELD_MSK)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_CHECK_ALARM_WND_ENABLE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_CHECK_ALARM_DOTLINE_ENABLE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_MINAREA)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_MAXAREA)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_CHECK_REGION_SHIELD_ENABLE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_SETVAPARAM)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_DELRULE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_TURE)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_CANCEL)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_GET)->EnableWindow(! bAllowedOperate);
	GetDlgItem(IDC_BTN_SAVE)->EnableWindow(!bAllowedOperate);
}
void CDlgSmartBasicSet::OnDelAllWizardDlg(WPARAM wParam,LPARAM lParam)
{
	while(m_ptrsetways.GetSize())
	{
		int i = m_ptrsetways.GetSize() - 1;
		switch (i)
		{
		case 4:
			{
				CDlgEndSet* temp = (CDlgEndSet*)m_ptrsetways.GetAt(i);
				temp->DestroyWindow();
				delete temp;
				m_ptrsetways.RemoveAt(i);
				TRACE("CDlgEndSet i:%d Size:%d \n",i,m_ptrsetways.GetSize());
				break;
			}
		case 3:
			{
				CDlgSmartOther* temp = (CDlgSmartOther*)m_ptrsetways.GetAt(i);
				temp->DestroyWindow();
				delete temp;
				m_ptrsetways.RemoveAt(i);
				TRACE("CDlgSmartOther i:%d Size:%d \n",i,m_ptrsetways.GetSize());
				break;
			}
		case 2:
			{
				CDlgSmartTime* temp = (CDlgSmartTime*)m_ptrsetways.GetAt(i);
				temp->DestroyWindow();
				delete temp;
				m_ptrsetways.RemoveAt(i);
				TRACE("CDlgSmartTime i:%d Size:%d \n",i,m_ptrsetways.GetSize());
				break;
			}
		case 1:
			{
				CDlgWizardSecond* temp = (CDlgWizardSecond*)m_ptrsetways.GetAt(i);
				temp->m_myrulestatic.Stop();
				temp->DestroyWindow();
				delete temp;
				m_ptrsetways.RemoveAt(i);
				TRACE("CDlgWizardSecond i:%d Size:%d \n",i,m_ptrsetways.GetSize());
				break;
			}
		case 0:
			{
				CDlgWizardFirst* temp = (CDlgWizardFirst*)m_ptrsetways.GetAt(i);
				temp->DestroyWindow();
				delete temp;
				m_ptrsetways.RemoveAt(i);
				TRACE("CDlgWizardFirst i:%d Size:%d \n",i,m_ptrsetways.GetSize());
				break;
			}
		}
	}
	SetBtnState(FALSE);
}

void CDlgSmartBasicSet::OnBtnGet() 
{
    LoadVAParam();
	GetRuleInfo();
	m_myrulelist.DeleteAllItems();
    InitialRuleList(); 
	UpdateData(FALSE);
	Invalidate();
}

BOOL CDlgSmartBasicSet::SaveAllParam()
{   
	BOOL bsaveruleparam = FALSE;
	BOOL bsavedparam = FALSE;
	bsavedparam = SaveParamToServer();//保存规则信息到服务器
    bsaveruleparam = m_rulesStatic.SaveRuelsSettings();
    if (bsavedparam && bsaveruleparam)
    {
		return TRUE;
    }
	return FALSE;
}

void CDlgSmartBasicSet::UpdataModify()
{
    OnBtnGet();
}

void CDlgSmartBasicSet::CreateList()
{
	CString strtemp;
    strtemp = _T("");
	LV_COLUMN lvcolumn;
	lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH ;
	lvcolumn.fmt = LVCFMT_CENTER;
	m_myrulelist.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	
    strtemp.LoadString(IDS_STRING513);
	lvcolumn.pszText = strtemp.GetBuffer(0);
	strtemp.ReleaseBuffer();
	lvcolumn.iSubItem = 0;
	lvcolumn.cx = 190;
	m_myrulelist.InsertColumn(0, &lvcolumn);
	
    strtemp.LoadString(IDS_STRING514);
	lvcolumn.pszText = strtemp.GetBuffer(0);
	strtemp.ReleaseBuffer();
	lvcolumn.iSubItem = 1;
	lvcolumn.cx = 190;
	m_myrulelist.InsertColumn(1, &lvcolumn);
	
    strtemp.LoadString(IDS_STRING515);
	lvcolumn.pszText = strtemp.GetBuffer(0);
	strtemp.ReleaseBuffer();
	lvcolumn.iSubItem = 2;
	lvcolumn.cx = 60;
	m_myrulelist.InsertColumn(2, &lvcolumn);
	
    strtemp.LoadString(IDS_STRING516);
	lvcolumn.pszText = strtemp.GetBuffer(0);
	strtemp.ReleaseBuffer();
	lvcolumn.iSubItem = 3;
	lvcolumn.cx = 160;
	m_myrulelist.InsertColumn(3, &lvcolumn);
	
}
//判断是否有规则同名
BOOL CDlgSmartBasicSet::IsRuleNameSame(CString temp)
{
	for (int i = 0; i < m_singleRules.m_count; i ++)
	{
		if (m_ioperatestate == 3)
		{
			if (m_cureditrule.pos == i)
			{
				continue;
			}
		}
		CString name = _T("");
		name.Format("%s", m_singleRules.m_lines[i].m_rulename);
		if (temp == name )
		{
			return TRUE;
		}
	}
	return FALSE;
}
//得到当前所有的使能状态
void CDlgSmartBasicSet::GetRulesStartState(BOOL icurstate /* = FALSE */)
{
	for (int i = 0; i < m_myrulelist.GetItemCount(); i ++)
	{   
		if (icurstate)
            m_curRuleStartFlags[i] = BYTE(m_myrulelist.GetCheckbox(i, 2));
		else
			m_preRuleStartFlags[i] = BYTE(m_myrulelist.GetCheckbox(i, 2));
	}
}
//对比所有的规则使能是否改变
BOOL CDlgSmartBasicSet::CompareModify()
{  
	for (int i = 0; i < sizeof(m_curRuleStartFlags)/sizeof(BYTE); i ++)
	{
		if (m_curRuleStartFlags[i] != m_preRuleStartFlags[i])
		{
			return TRUE;
		}
	}
	return FALSE;
}
//改变后的规则使能保存到对应规则中
void CDlgSmartBasicSet::SetRulesEnableModify()
{
	int i = 0;
	for (i = 0; i < m_singleRules.m_count; i ++)
	{
		m_singleRules.m_lines[i].m_enable = m_curRuleStartFlags[i];
	}
	if (strlen(m_regionInRule.m_rulename) > 0)
	{
		m_regionInRule.m_enable = m_curRuleStartFlags[i ++];
	}
	if (strlen(m_regionOutRule.m_rulename) > 0)
	{
		m_regionOutRule.m_enable = m_curRuleStartFlags[i ++];
	}
	if (strlen(m_regionHoverRule.m_rulename) > 0)
	{
		m_regionHoverRule.m_enable = m_curRuleStartFlags[i];
	}
}

void CDlgSmartBasicSet::OnBtnSave() 
{
	SaveParamToServer();
	SaveTrackType();
	SaveTrackSensitivity();
}
//显示规则绘制的内容
void CDlgSmartBasicSet::ShowRuleContents()
{
	int ruleType = m_cureditrule.ruletype;
	switch (ruleType)
	{
	case LINERULE:
		{
			CSingleLineRule* singleline = new CSingleLineRule;
			VSNETVASINGLELINERULE lineinfo;
			memset(&lineinfo, 0, sizeof(VSNETVASINGLELINERULE));
			lineinfo.m_sgline_beg_x = g_dlgSmartBaseSet->m_singleRules.m_lines[m_cureditrule.pos].m_sgline_beg_x;
			lineinfo.m_sgline_beg_y = g_dlgSmartBaseSet->m_singleRules.m_lines[m_cureditrule.pos].m_sgline_beg_y;
			lineinfo.m_sgline_drc = g_dlgSmartBaseSet->m_singleRules.m_lines[m_cureditrule.pos].m_sgline_drc;
			lineinfo.m_sgline_end_x = g_dlgSmartBaseSet->m_singleRules.m_lines[m_cureditrule.pos].m_sgline_end_x;
			lineinfo.m_sgline_end_y = g_dlgSmartBaseSet->m_singleRules.m_lines[m_cureditrule.pos].m_sgline_end_y;
			singleline->SetRule(&lineinfo);
			m_rulesStatic.SetCurRule(singleline);
			break;
		}
	case REGIONINRULE:
		{
			CRegionRule* region = new CRegionRule(0);
			VSNETVAREGIONERULE reginfo;
			memset(&reginfo, 0, sizeof(VSNETVAREGIONERULE));
			reginfo.m_region_valid = 1;
			memcpy(reginfo.m_packed_msk, g_dlgSmartBaseSet->m_regionInRule.m_packed_msk, sizeof(reginfo.m_packed_msk));
			region->SetRule(&reginfo);
			m_rulesStatic.SetCurRule(region);
			break;	
		}
	case REGIONOUTRULE:
		{
			CRegionRule* region = new CRegionRule(1);
			VSNETVAREGIONERULE reginfo;
			memset(&reginfo, 0, sizeof(VSNETVAREGIONERULE));
			reginfo.m_region_valid = 1;
			memcpy(reginfo.m_packed_msk, g_dlgSmartBaseSet->m_regionOutRule.m_packed_msk, sizeof(reginfo.m_packed_msk));
			region->SetRule(& reginfo);
			m_rulesStatic.SetCurRule(region);
			break;
		}
	case REGIONHOVERRULE:
		{
			CRegionRule* region = new CRegionRule(2);
			VSNETVAREGIONERULE reginfo;
			memset(&reginfo, 0, sizeof(VSNETVAREGIONERULE));
			reginfo.m_region_valid = 1;
			memcpy(reginfo.m_packed_msk, g_dlgSmartBaseSet->m_regionHoverRule.m_packed_msk, sizeof(reginfo.m_packed_msk));	  
			region->SetRule(&reginfo);
			m_rulesStatic.SetCurRule(region);
			break;
		}
	default:
		break;
	}
}
void CDlgSmartBasicSet::SetState()
{
    int index = -1;
	index =	m_myrulelist.GetCurSel();
	char strruletype[60];
	if (-1 != index)
	{
		m_myrulelist.GetItemText(index, 1, strruletype, sizeof(strruletype));
		CString temp = _T("");
		temp.Format("%s", strruletype);
		if (temp == "警戒线跨越")
		{
			GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(TRUE);
		}
		else
		{
			GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(FALSE);
		}
		m_cureditrule.ruletype = -1;
		m_cureditrule.pos = -1;
		GetRuleInfobyIndex(index,m_cureditrule);
		if (m_cureditrule.ruletype != -1)
		{
			ShowRuleContents();
		}
	}
	else
	{
		GetDlgItem(IDC_BTN_COPYRULE)->EnableWindow(TRUE);
	}
	UpdateData(FALSE);
}

BOOL CDlgSmartBasicSet::LoadTrackType()
{
	LONG	handle = m_rulesStatic.MessageOpen();
	if(handle <=0)return FALSE;
	VSNETSMARTTRACKTYPE track_type = {0};
	m_rulesStatic.MessageOpt(handle,MESSAGE_CMD_GETVATRACKTYPE,&track_type);
	m_tracktype = track_type.m_smart_tracktype;
	UpdateData(FALSE);
	m_rulesStatic.MessageClose(handle);
	return TRUE;
}

BOOL CDlgSmartBasicSet::SaveTrackType()
{
	if(!UpdateData())return FALSE;
	LONG	handle = m_rulesStatic.MessageOpen();
	if(handle <=0)return FALSE;
	VSNETSMARTTRACKTYPE track_type = {0};
	track_type.m_smart_tracktype = m_tracktype;
	TRACE("SaveTrackType(): %d\n",m_tracktype);
	m_rulesStatic.MessageOpt(handle,MESSAGE_CMD_SETVATRACKTYPE,&track_type);
	m_rulesStatic.MessageClose(handle);
	return TRUE;
}

BOOL CDlgSmartBasicSet::LoadTrackSensitivity()
{
	LONG	handle = m_rulesStatic.MessageOpen();
	if(handle <=0)return FALSE;
	VSNETSMARTSENSE track_sensi = {0};
	m_rulesStatic.MessageOpt(handle,MESSAGE_CMD_GETVASENSE,&track_sensi);
	m_tracksensi.SetPos(track_sensi.m_smart_sense);
	UpdateData(FALSE);
	m_rulesStatic.MessageClose(handle);
	return TRUE;
}

BOOL CDlgSmartBasicSet::SaveTrackSensitivity()
{
	if(!UpdateData())return FALSE;
	LONG	handle = m_rulesStatic.MessageOpen();
	if(handle <=0)return FALSE;
	VSNETSMARTSENSE track_sensi = {0};
	track_sensi.m_smart_sense = m_tracksensi.GetPos();
	m_rulesStatic.MessageOpt(handle,MESSAGE_CMD_SETVASENSE,&track_sensi);
	m_rulesStatic.MessageClose(handle);
	return TRUE;
}

void CDlgSmartBasicSet::OnCustomdrawSliderSensi(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	//SaveTrackSensitivity();
	CString str;
	str.Format("%d",m_tracksensi.GetPos());
	GetDlgItem(IDC_STATIC_SENSI)->SetWindowText(str);

	*pResult = 0;
}

void CDlgSmartBasicSet::OnRadioAll() 
{
	// TODO: Add your control notification handler code here
	SaveTrackType();
}

void CDlgSmartBasicSet::OnRadioCar() 
{
	// TODO: Add your control notification handler code here
	SaveTrackType();
}

void CDlgSmartBasicSet::OnRadioPerson() 
{
	// TODO: Add your control notification handler code here
	SaveTrackType();
}
