// DlgWizardFirst.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Fire.h"
#include "DlgWizardFirst.h"
#include "DlgRuleSet.h"
#include "DlgWizardSecond.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgWizardFirst dialog

extern CDlgSmartBasicSet*   g_dlgSmartBaseSet;
#define  RULENAME_MAX  18
CDlgWizardFirst::CDlgWizardFirst(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgWizardFirst::IDD, pParent)
{  
	//{{AFX_DATA_INIT(CDlgWizardFirst)
	m_rule = -1;
	m_rulename = _T("");
	m_rulediscription = _T("");
	//}}AFX_DATA_INIT
	m_secondway = NULL;

}


void CDlgWizardFirst::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgWizardFirst)
	DDX_Radio(pDX, IDC_RADIO1, m_rule);
	DDX_Text(pDX, IDC_EDIT_RULENAME, m_rulename);
	DDX_Text(pDX, IDC_EDIT_DESCRIP, m_rulediscription);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgWizardFirst, CDialog)
	//{{AFX_MSG_MAP(CDlgWizardFirst)
	ON_BN_CLICKED(IDC_BTN_NEXT, OnBtnNext)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
	ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
	ON_BN_CLICKED(IDC_RADIO6, OnRadio6)
	ON_BN_CLICKED(IDC_RADIO7, OnRadio7)
	ON_BN_CLICKED(IDC_RADIO8, OnRadio8)
	ON_BN_CLICKED(IDC_RADIO9, OnRadio9)
	ON_WM_CLOSE()
	ON_EN_CHANGE(IDC_EDIT_RULENAME, OnChangeEditRulename)
	ON_WM_MOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgWizardFirst message handlers

void CDlgWizardFirst::OnBtnNext() 
{
    CString strTemp;
    strTemp = _T("");

    UpdateData();

    if (m_rulename.IsEmpty())
    {
        strTemp.LoadString(IDS_STRING517);
        AfxMessageBox(strTemp);
        return;
    }
    if (-1 == m_rule)
    {
        strTemp.LoadString(IDS_STRING518);
        AfxMessageBox(strTemp);
        return;
    }
    if (m_rulediscription.IsEmpty())
    {
        return;
    }

    if (g_dlgSmartBaseSet->m_ptrsetways.GetSize() == 1)
    {
        m_secondway = NULL;
    }

    if (!m_secondway)
    {
        if (!m_rule)
        {
            BOOL bNameSame = g_dlgSmartBaseSet->IsRuleNameSame(m_rulename);
            if (bNameSame)
            {
                AfxMessageBox("已经有该规则名字");
                return;
            }
        }
        m_secondway = new CDlgWizardSecond();
        m_secondway->Create(IDD_DLG_SMWIZARD2,g_dlgSmartBaseSet);
    }
    else
    {
        ShowWindow(SW_HIDE);
    }
    if (!((CButton*)GetDlgItem(IDC_RADIO4))->GetCheck())
    {
        m_secondway->GetDlgItem(IDC_STATIC_ROAM)->ShowWindow(SW_HIDE);
        m_secondway->GetDlgItem(IDC_STATIC_ROAMSET)->ShowWindow(SW_HIDE);
        m_secondway->GetDlgItem(IDC_STATIC_ROAMSET1)->ShowWindow(SW_HIDE);
        m_secondway->GetDlgItem(IDC_EDIT_INFINITETIME)->ShowWindow(SW_HIDE);
    }
    else
    {
        m_secondway->GetDlgItem(IDC_STATIC_ROAM)->ShowWindow(SW_SHOW);
        m_secondway->GetDlgItem(IDC_STATIC_ROAMSET)->ShowWindow(SW_SHOW);
        m_secondway->GetDlgItem(IDC_STATIC_ROAMSET1)->ShowWindow(SW_SHOW);
        m_secondway->GetDlgItem(IDC_EDIT_INFINITETIME)->ShowWindow(SW_SHOW);
    }
    m_secondway->ShowWindow(SW_SHOW);
    //::SetWindowPos(m_secondway->m_hWnd, wndTop, g_dlgSmartBaseSet->m_prodlgpos.left, 
//		           g_dlgSmartBaseSet->m_prodlgpos.top, 0 , 0, )
}

void CDlgWizardFirst::OnRadio1() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 0;
	m_rulediscription.LoadString(IDS_STRING519);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio2() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 1;
	m_rulediscription.LoadString(IDS_STRING545);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio3() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 2;
	m_rulediscription.LoadString(IDS_STRING521);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio4() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule =3;
	m_rulediscription.LoadString(IDS_STRING522);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio5() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 4;
	m_rulediscription.LoadString(IDS_STRING523);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio6() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 5;
	m_rulediscription.LoadString(IDS_STRING524);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio7() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 6;
	m_rulediscription.LoadString(IDS_STRING525);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnRadio8() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 7;
	m_rulediscription.LoadString(IDS_STRING526);
	UpdateData(FALSE);
}

BOOL CDlgWizardFirst::OnInitDialog() 
{
	CDialog::OnInitDialog();
// 	for (int i = 0; i < 1; i ++)
// 	{
//         GetDlgItem(IDC_RADIO1 + i)->EnableWindow(g_dlgSmartBaseSet->m_allowedrules[i]);
// 	}
    GetDlgItem(IDC_RADIO2)->EnableWindow(g_dlgSmartBaseSet->m_allowedrules[1]);
    if (g_dlgSmartBaseSet->m_allowedrules[1])
    {
        OnRadio2();
    }
	InitialDlaData();//初始化编辑规则数据
	g_dlgSmartBaseSet->m_ptrsetways.Add(this);
	UpdateData(FALSE);
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgWizardFirst::OnRadio9() 
{
	UpdateData();
	// TODO: Add your control notification handler code here
	m_rule = 8;
	m_rulediscription.LoadString(IDS_STRING527);
	UpdateData(FALSE);
}

void CDlgWizardFirst::OnCancel() 
{
	// TODO: Add extra cleanup 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 CDlgWizardFirst::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	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 CDlgWizardFirst::InitialDlaData()
{
	if (g_dlgSmartBaseSet->m_ioperatestate == 3)
	{
		m_rule = g_dlgSmartBaseSet->m_cureditrule.ruletype;
        m_rulediscription.LoadString(IDS_STRING519 + m_rule);
		char name[60];
		memset(name, 0, sizeof(name));
	    switch (m_rule)
	    {
	    case LINERULE:
		//	sprintf(name, "%s", g_dlgSmartBaseSet->m_tempSigRules.m_lines[g_dlgSmartBaseSet->m_cureditrule.pos].m_rulename);
			m_rulename.Format("%s",g_dlgSmartBaseSet->m_singleRules.m_lines[g_dlgSmartBaseSet->m_cureditrule.pos].m_rulename);
	    	break;
        case REGIONINRULE:
			m_rulename.Format("%s", g_dlgSmartBaseSet->m_regionInRule.m_rulename);
            break;
		case REGIONOUTRULE:
			m_rulename.Format("%s",g_dlgSmartBaseSet->m_regionOutRule.m_rulename);
			break;
		case REGIONHOVERRULE:
			m_rulename.Format("%s", g_dlgSmartBaseSet->m_regionHoverRule.m_rulename);
			break;
		case THINGLEAVEDRULE:
		case THINGMOVERULE:
		case THINGSTEALEDRULE:
		case ILLEGALPARKRULE:
		case NUMCOUNTRULE:
			break;
	    }
	}
}

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

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

//限制规则名字在24字节
void CDlgWizardFirst::OnChangeEditRulename() 
{
	UpdateData();
	CString temp = _T("");
	temp = m_rulename;
	BOOL bWhole = TRUE;
	int count = 0;
	if (temp.GetLength() >= RULENAME_MAX)
	{
        temp = temp.Left(RULENAME_MAX);
	}
    for(int i = 0; i < temp.GetLength(); i ++)
	{
         if (temp.GetAt(i) > 0)
         {
			 bWhole = ! bWhole;
			 count ++;
         }
	}
	TRACE("temp:%s 数字个数:%d\n", temp.GetBuffer(0),count);
	temp.ReleaseBuffer();
	if (bWhole)
	{
		m_rulename = temp;
	}
	else
	{
       m_rulename = temp.Left(RULENAME_MAX - 1);
	}
	UpdateData(FALSE);
    if (temp.GetLength() >= 18)
    {
		((CEdit*)GetDlgItem(IDC_EDIT_RULENAME))->SetSel(100,100);
    }
}

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