// DlgSmartOther.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Fire.h"
#include "DlgSmartOther.h"
#include "DlgRuleSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgSmartOther dialog
#define  OSDTEXT_MAX  24
extern CDlgSmartBasicSet*   g_dlgSmartBaseSet;

CDlgSmartOther::CDlgSmartOther(CWnd* pParent /*=NULL*/)
: CDialog(CDlgSmartOther::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSmartOther)
	m_bEnableSerOsd = FALSE;
	m_bEnableChOsd = FALSE;
	m_hSelectChOsd = -1;
	m_lpszRegionEnter = _T("");
	m_lpszRegionExit = _T("");
	m_lpszRegionHover = _T("");
	m_lpszLineSingle = _T("");
	m_lpszLineMulti = _T("");
	m_cx = 0;
	m_cy = 0;
	m_chtocopy = -1;
	//}}AFX_DATA_INIT
	m_hPrevSelectCh = 0;
	m_lastway = NULL;
    memset(&m_osdinfo[0], 0, sizeof(VSNETSMOSD));
	memset(&m_osdinfo[1], 0, sizeof(VSNETSMOSD));
}


void CDlgSmartOther::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSmartOther)
	DDX_Check(pDX, IDC_CHECK_ENABLE_OSD, m_bEnableSerOsd);
	DDX_Check(pDX, IDC_CHECK_ENABLE_CHOSD, m_bEnableChOsd);
	DDX_CBIndex(pDX, IDC_COMBO_OSDCH, m_hSelectChOsd);
	DDX_Text(pDX, IDC_EDIT_ENTER_REGION_ALARMOSD, m_lpszRegionEnter);
	DDV_MaxChars(pDX, m_lpszRegionEnter, 32);
	DDX_Text(pDX, IDC_EDIT_EXIT_REGION_ALARMOSD, m_lpszRegionExit);
	DDV_MaxChars(pDX, m_lpszRegionExit, 32);
	DDX_Text(pDX, IDC_EDIT_HOVER_REGION_ALARMOSD, m_lpszRegionHover);
	DDV_MaxChars(pDX, m_lpszRegionHover, 32);
	DDX_Text(pDX, IDC_EDIT_SLINE_ALARMOSD, m_lpszLineSingle);
	DDV_MaxChars(pDX, m_lpszLineSingle, 32);
	DDX_Text(pDX, IDC_EDIT_MLINE_ALARMOSD, m_lpszLineMulti);
	DDV_MaxChars(pDX, m_lpszLineMulti, 32);
	DDX_Text(pDX, IDC_EDIT_X_COORDINATE, m_cx);
	DDV_MinMaxInt(pDX, m_cx, 0, 704);
	DDX_Text(pDX, IDC_EDIT_Y_COORDINATE, m_cy);
	DDV_MinMaxInt(pDX, m_cy, 0, 576);
	DDX_CBIndex(pDX, IDC_COMBO_COPYCH, m_chtocopy);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSmartOther, CDialog)
//{{AFX_MSG_MAP(CDlgSmartOther)
ON_BN_CLICKED(IDC_BTN_LOAD, OnBtnLoad)
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
ON_WM_DESTROY()
ON_CBN_SELCHANGE(IDC_COMBO_OSDCH, OnSelchangeComboOsdch)
ON_BN_CLICKED(IDC_BTN_COPYTOCH, OnBtnCopytoch)
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_EN_CHANGE(IDC_EDIT_SLINE_ALARMOSD, OnChangeEditSlineAlarmosd)
	ON_WM_MOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSmartOther message handlers

BOOL CDlgSmartOther::OnInitDialog() 
{
	CDialog::OnInitDialog();
	m_third = (CDlgSmartTime*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(2);
	m_third->ShowWindow(SW_HIDE);
    
    g_dlgSmartBaseSet->m_ptrsetways.Add(this);
	m_tooltip.Create(this);
	m_tooltip.Activate(TRUE);
	m_tooltip.SetDelayTime(200);
	
	m_tooltip.AddTool(GetDlgItem(IDC_EDIT_X_COORDINATE),"x坐标必须为4的整数倍！");
	m_tooltip.AddTool(GetDlgItem(IDC_EDIT_Y_COORDINATE),"y坐标任意，但是设置后发现无OSD叠加时，可能是发生重叠，请重新填写y坐标！");
	
	
	//	if(m_TermSer == NULL)return TRUE;
	
	CString lpString = _T("所有通道");
	((CComboBox*)GetDlgItem(IDC_COMBO_COPYCH))->AddString(lpString);
	for(int ch = 0 ; ch < 2 ; ch ++)
	{
		lpString.Format("通道 %d",ch + 1);
		((CComboBox*)GetDlgItem(IDC_COMBO_OSDCH))->AddString(lpString);
		((CComboBox*)GetDlgItem(IDC_COMBO_COPYCH))->AddString(lpString);
		
		VSNETCHVAALARMOSD *pInfo = new VSNETCHVAALARMOSD;
		memset(pInfo,0,sizeof(VSNETCHVAALARMOSD));
		m_osdlist.AddTail(pInfo);
	}
	
	m_hSelectChOsd = 0;
	m_chtocopy = 0;
	int curoperate = g_dlgSmartBaseSet->m_ioperatestate;
	if (curoperate == 3)//编辑规则初始化对话框
	{
	   InitialDlgData();
	   LoadOsdSetting(0);
	}
	else//增加规则初始化
	{
       LocalInitDlgData();
	}
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
}

void CDlgSmartOther::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	ReleaseOsdList();
}

void CDlgSmartOther::ReleaseOsdList()
{
	VSNETCHVAALARMOSD *pInfo = NULL;
	while(m_osdlist.GetCount() > 0)
	{
		pInfo = (VSNETCHVAALARMOSD*)m_osdlist.RemoveHead();
		delete pInfo;
	}
}

void CDlgSmartOther::OnBtnLoad() 
{
	// TODO: Add your control notification handler code here
	if(!UpdateData())return;
//	LoadOsdSetting();
	LoadOsdSetting(m_hSelectChOsd);
	UpdateData(FALSE);
}

void CDlgSmartOther::OnBtnSave() 
{
	// TODO: Add your control notification handler code here
	if(!UpdateData())return;
	SaveOsdSetting(m_hSelectChOsd);
	SaveOsdSetting();
}

BOOL CDlgSmartOther::LoadOsdSetting(int ch)
{    
	m_lpszLineSingle.Format("%s",m_osdinfo[ch].m_text);
	m_bEnableChOsd = m_osdinfo[ch].m_enable;
	m_cx = m_osdinfo[ch].m_x;
	m_cy = m_osdinfo[ch].m_y;
	UpdateData(FALSE);
	return TRUE;           
}

BOOL CDlgSmartOther::SaveOsdSetting()
{
	VSNETVAALARMOSD osd_info;
	memset(&osd_info,0,sizeof(VSNETVAALARMOSD));
	
	LONG hHandle = VSNET_ClientMessageOpen((char*)(LPCTSTR)g_dlgSmartBaseSet->m_sername,
		(char*)(LPCTSTR)g_dlgSmartBaseSet->m_url,(char*)(LPCTSTR)g_dlgSmartBaseSet->m_username,
		(char*)(LPCTSTR)g_dlgSmartBaseSet->m_password,g_dlgSmartBaseSet->m_port);
	
	int iRet = 0;
	
	if(hHandle != -1)
	{
		GetOsd(&osd_info);
		iRet = VSNET_ClientMessageOpt(hHandle,MESSAGE_CMD_SETANALYSEOSD,0,&osd_info);
		VSNET_ClientMessageClose(hHandle);
	}
	
	return iRet;
}

BOOL CDlgSmartOther::SaveOsdSetting(int ch)
{   
	sprintf(m_osdinfo[ch].m_text, "%s", m_lpszLineSingle.GetBuffer(0));
	m_osdinfo[ch].m_enable = m_bEnableChOsd;
	m_osdinfo[ch].m_x = m_cx;
    m_osdinfo[ch].m_y = m_cy;
    return TRUE;
}

void CDlgSmartOther::GetOsd(VSNETVAALARMOSD* pOsdInfo)
{
	if(!UpdateData())return;
	
	if(pOsdInfo == NULL)return;
	
	pOsdInfo->m_enable = m_bEnableSerOsd;
	
	POSITION  pos = NULL;
	VSNETCHVAALARMOSD *pInfo = NULL;
	
	for(int ch = 0 ; ch < m_osdlist.GetCount() ; ch ++)
	{
		pos = m_osdlist.FindIndex(ch);
		pInfo = (VSNETCHVAALARMOSD*)m_osdlist.GetAt(pos);
		pOsdInfo->m_chAlarmOsd[ch].m_enable = pInfo->m_enable;
		sprintf(pOsdInfo->m_chAlarmOsd[ch].m_regionEnter_osdText,"%s",pInfo->m_regionEnter_osdText);
		sprintf(pOsdInfo->m_chAlarmOsd[ch].m_regionLeave_osdText,"%s",pInfo->m_regionLeave_osdText);
		sprintf(pOsdInfo->m_chAlarmOsd[ch].m_regionWander_osdText,"%s",pInfo->m_regionWander_osdText);
		sprintf(pOsdInfo->m_chAlarmOsd[ch].m_tripwire_osdText,"%s",pInfo->m_tripwire_osdText);
		pOsdInfo->m_chAlarmOsd[ch].m_x = pInfo->m_x;
		pOsdInfo->m_chAlarmOsd[ch].m_y = pInfo->m_y;
	}
}

void CDlgSmartOther::SetOsd(VSNETVAALARMOSD* pOsdInfo)
{
	//if(!UpdateData())return;
	
	if(pOsdInfo == NULL)return;
	
	m_bEnableSerOsd = pOsdInfo->m_enable == 0 ? 0 : 1;
	
	POSITION  pos = NULL;
	VSNETCHVAALARMOSD *pInfo = NULL;	
	for(int len = 0,int ch = 0 ; ch < m_osdlist.GetCount() ; ch ++)
	{
		pos = m_osdlist.FindIndex(ch);
		pInfo = (VSNETCHVAALARMOSD*)m_osdlist.GetAt(pos);
		pInfo->m_enable = pOsdInfo->m_chAlarmOsd[ch].m_enable;
		strncpy(pInfo->m_regionEnter_osdText,pOsdInfo->m_chAlarmOsd[ch].m_regionEnter_osdText,30);
		strncpy(pInfo->m_regionLeave_osdText,pOsdInfo->m_chAlarmOsd[ch].m_regionLeave_osdText,30);
		strncpy(pInfo->m_regionWander_osdText,pOsdInfo->m_chAlarmOsd[ch].m_regionWander_osdText,30);
		strncpy(pInfo->m_tripwire_osdText,pOsdInfo->m_chAlarmOsd[ch].m_tripwire_osdText,30);
		pInfo->m_x = pOsdInfo->m_chAlarmOsd[ch].m_x;
		pInfo->m_y = pOsdInfo->m_chAlarmOsd[ch].m_y;
	}
	UpdateData(FALSE);
}

void CDlgSmartOther::OnSelchangeComboOsdch() 
{
	// TODO: Add your control notification handler code here
	if(!UpdateData())return;
	SaveOsdSetting(m_hPrevSelectCh);
	LoadOsdSetting(m_hSelectChOsd);
	m_hPrevSelectCh = m_hSelectChOsd;
	UpdateData(FALSE);
}

BOOL CDlgSmartOther::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_tooltip.RelayEvent(pMsg);
	if (WM_KEYUP == pMsg->message)
	{
		if (pMsg->wParam == VK_RETURN)
		{
			return TRUE;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CDlgSmartOther::OnBtnCopytoch() 
{
	// TODO: Add your control notification handler code here
	if(!UpdateData())return;
	if(m_chtocopy == 0)// copy to all
	{
		for(int index = 0 ; index < 2 ; index ++)
			SaveOsdSetting(index);
	}
	else
	{
		SaveOsdSetting(m_chtocopy - 1);
	}
}

void CDlgSmartOther::OnBtnNext() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	SaveOsdSetting(m_hSelectChOsd);
	for (int i = 0; i < 2; i ++)
	{
		if (m_osdinfo[i].m_x < 0 || m_osdinfo[i].m_x > 704 || 
			m_osdinfo[i].m_y < 0 || m_osdinfo[i].m_y > 576)
		{	
			return;
		}
	}
	if ( ! m_lastway)
	{
		m_lastway = new CDlgEndSet();
		m_lastway->Create(IDD_DLG_SMWIZARD5,g_dlgSmartBaseSet);
	}
	else
	{
		ShowWindow(SW_HIDE);
	}
	m_lastway->ShowWindow(SW_SHOW);
//	m_lastway->MoveWindow(g_dlgSmartBaseSet->m_prodlgpos);
}	

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

void CDlgSmartOther::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 CDlgSmartOther::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 CDlgSmartOther::InitialDlgData()
{
	RULETYPEPOS ruletypos;
	memset(&ruletypos, 0, sizeof(RULETYPEPOS));
	ruletypos.pos = -1; 
	ruletypos.ruletype = -1;
	memcpy(&ruletypos,&g_dlgSmartBaseSet->m_cureditrule, sizeof(RULETYPEPOS));
	for (int i = 0; i < 2; i ++)
	{
		switch (ruletypos.ruletype)
		{
		case LINERULE:
            {
				memcpy(&m_osdinfo[i], &g_dlgSmartBaseSet->m_singleRules.m_lines[ruletypos.pos].m_osd[i], sizeof(VSNETSMOSD));
				m_bEnableSerOsd = g_dlgSmartBaseSet->m_singleRules.m_lines[ruletypos.pos].m_enableosd;
				break;
			}
		case REGIONINRULE:
			{
				m_bEnableSerOsd = g_dlgSmartBaseSet->m_regionInRule.m_enableosd;
				memcpy(&m_osdinfo[i], &g_dlgSmartBaseSet->m_regionInRule.m_osd[i], sizeof(VSNETSMOSD)); 
				break;
			}
		case REGIONOUTRULE:
			{
				m_bEnableSerOsd = g_dlgSmartBaseSet->m_regionOutRule.m_enableosd;
				memcpy(&m_osdinfo[i], &g_dlgSmartBaseSet->m_regionOutRule.m_osd[i], sizeof(VSNETSMOSD));			  
				break;
			}
			
		case REGIONHOVERRULE:
			{
				m_bEnableSerOsd = g_dlgSmartBaseSet->m_regionHoverRule.m_enableosd;
				memcpy(&m_osdinfo[i], &g_dlgSmartBaseSet->m_regionHoverRule.m_osd[i], sizeof(VSNETSMOSD));
				break;
			}
		case THINGLEAVEDRULE:
		case THINGMOVERULE:
		case THINGSTEALEDRULE:
		case ILLEGALPARKRULE:
		case NUMCOUNTRULE:
			break;			
		}
	}
}

void CDlgSmartOther::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CDlgSmartOther::OnOK() 
{
	// TODO: Add extra validation here
	OnBtnNext();
}

void CDlgSmartOther::LocalInitDlgData()
{
	CDlgWizardFirst* temp = NULL;
	temp = (CDlgWizardFirst*)g_dlgSmartBaseSet->m_ptrsetways.GetAt(0);
    m_bEnableSerOsd = TRUE;
	m_bEnableChOsd = TRUE;
	m_cx = 8;
	m_cy = 130;
	m_lpszLineSingle = temp->m_rulename;
	for (int i = 0; i < 2; i ++)
	{
		m_osdinfo[i].m_enable = TRUE;
		memcpy(m_osdinfo[i].m_text, temp->m_rulename.GetBuffer(0), sizeof(m_osdinfo[i].m_text));
		m_osdinfo[i].m_x  = 8;
		m_osdinfo[i].m_y = 130;
	}
}
//检测输入的OSD叠加的字符数最多允许24个字节
void CDlgSmartOther::OnChangeEditSlineAlarmosd() 
{
	UpdateData();
	CString temp = _T("");
	temp = m_lpszLineSingle;
	BOOL bWhole = TRUE;
	if (temp.GetLength() >= OSDTEXT_MAX)
	{
        temp = temp.Left(OSDTEXT_MAX);
	}
	 for(int i = 0; i < temp.GetLength(); i ++)
		{
	        if (temp.GetAt(i) > 0)
	       {
				 bWhole = ! bWhole;
	       }
		}
	if (bWhole)
	{
		m_lpszLineSingle = temp;
	}
	else
	{
		m_lpszLineSingle = temp.Left(OSDTEXT_MAX - 1);
	}
	UpdateData(FALSE);
	if (temp.GetLength() >= 24)
	{
       ((CEdit*)GetDlgItem(IDC_EDIT_SLINE_ALARMOSD))->SetSel(100,100);
	}
}

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