// SymbolSelectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestWrapperAPI.h"
#include "SymbolSelectDlg.h"
#include "afxdialogex.h"


// CSymbolSelectDlg dialog

IMPLEMENT_DYNAMIC(CSymbolSelectDlg, CDialogEx)

CSymbolSelectDlg::CSymbolSelectDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CSymbolSelectDlg::IDD, pParent)
{
	m_strSymbol = _T("");
}

CSymbolSelectDlg::~CSymbolSelectDlg()
{
}

void CSymbolSelectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_cmbRoot, m_cmbRoot);
	DDX_Control(pDX, IDC_cmbExchange, m_cmbExchange);
	DDX_Control(pDX, IDC_cmbGroup1, m_cmbGroup1);
	DDX_Control(pDX, IDC_cmbGroup2, m_cmbGroup2);
	DDX_Control(pDX, IDC_LIST_Symbol, m_ListSymbol);
	DDX_Control(pDX, IDC_cmbGroup3, m_cmbGroup3);
	DDX_Control(pDX, IDC_cmbGroup4, m_cmbGroup4);
}


BEGIN_MESSAGE_MAP(CSymbolSelectDlg, CDialogEx)
	ON_CBN_SELCHANGE(IDC_cmbRoot, &CSymbolSelectDlg::OnCbnSelchangecmbroot)
	ON_CBN_SELCHANGE(IDC_cmbExchange, &CSymbolSelectDlg::OnCbnSelchangecmbexchange)
	ON_CBN_SELCHANGE(IDC_cmbGroup1, &CSymbolSelectDlg::OnCbnSelchangecmbgroup1)
	ON_CBN_SELCHANGE(IDC_cmbGroup2, &CSymbolSelectDlg::OnCbnSelchangecmbgroup2)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDOK, &CSymbolSelectDlg::OnBnClickedOk)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_Symbol, &CSymbolSelectDlg::OnDblclkListSymbol)
	ON_CBN_SELCHANGE(IDC_cmbGroup3, &CSymbolSelectDlg::OnCbnSelchangecmbgroup3)
	ON_CBN_SELCHANGE(IDC_cmbGroup4, &CSymbolSelectDlg::OnCbnSelchangecmbgroup4)
END_MESSAGE_MAP()


// CSymbolSelectDlg message handlers
void CSymbolSelectDlg::SetParent(CTestWrapperAPIDlg* Parent)
{
	m_Parent = Parent;
}

BOOL CSymbolSelectDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
	m_ListSymbol.InsertColumn( 0, _T("商品代码"), LVCFMT_LEFT, 155);
	m_ListSymbol.InsertColumn( 1, _T("商品名称"), LVCFMT_LEFT, 155); 
	DWORD dwStyle = m_ListSymbol.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	m_ListSymbol.SetExtendedStyle(dwStyle);

	GetSymbolClassification();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CSymbolSelectDlg::OnCbnSelchangecmbroot()
{
	// TODO: Add your control notification handler code here
	GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(m_cmbRoot.GetCurSel());
	GetSymbolClassification(pData->strGroup);
}


void CSymbolSelectDlg::OnCbnSelchangecmbexchange()
{
	// TODO: Add your control notification handler code here
	GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(m_cmbRoot.GetCurSel());
	GroupData *pData2 = (GroupData*)m_cmbExchange.GetItemData(m_cmbExchange.GetCurSel());
	GetSymbolClassification(pData->strGroup,pData2->strGroup);
}


void CSymbolSelectDlg::OnCbnSelchangecmbgroup1()
{
	// TODO: Add your control notification handler code here
	GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(m_cmbRoot.GetCurSel());
	GroupData *pData2 = (GroupData*)m_cmbExchange.GetItemData(m_cmbExchange.GetCurSel());
	GroupData *pData3 = (GroupData*)m_cmbGroup1.GetItemData(m_cmbGroup1.GetCurSel());
	GetSymbolClassification(pData->strGroup,pData2->strGroup,pData3->strGroup);
}


void CSymbolSelectDlg::OnCbnSelchangecmbgroup2()
{
	// TODO: Add your control notification handler code here
	GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(m_cmbRoot.GetCurSel());
	GroupData *pData2 = (GroupData*)m_cmbExchange.GetItemData(m_cmbExchange.GetCurSel());
	GroupData *pData3 = (GroupData*)m_cmbGroup1.GetItemData(m_cmbGroup1.GetCurSel());
	GroupData *pData4 = (GroupData*)m_cmbGroup2.GetItemData(m_cmbGroup2.GetCurSel());
	GetSymbolClassification(pData->strGroup,pData2->strGroup,pData3->strGroup,pData4->strGroup);
}

void CSymbolSelectDlg::GetSymbolClassification(CString strParam1, CString strParam2, CString strParam3, CString strParam4, CString strParam5)
{
	CString strData = m_Parent->GetSymbolClassifications(strParam1, strParam2, strParam3, strParam4, strParam5);
	if ( strData.Find(_T("TC.F.")) > -1 || strData.Find(_T("TC.F2.")) > -1 ||strData.Find(_T("TC.O.")) > -1 || strData.Find(_T("TC.S.")) > -1)
	{
		CString strTemp;
		int nPos = 0;
		int nIndex=0;
		m_ListSymbol.DeleteAllItems();
		while(nPos != -1)
		{
			strTemp = strData.Tokenize(_T("|"), nPos);
			if (nPos == -1)
				break;
			
			int nTemp = 0;
			CString strKey = strTemp.Tokenize(_T(","),nTemp);
			CString strName = strTemp.Tokenize(_T(","),nTemp);

			m_ListSymbol.InsertItem(nIndex,strKey);
			//m_ListSymbol.SetItem(nIndex,0,LVIF_TEXT,strKey,0,NULL,NULL,NULL);
			m_ListSymbol.SetItem(nIndex,1,LVIF_TEXT,strName,0,NULL,NULL,NULL);
			nIndex++;
		}
	}
	else
	{
		CString strTemp;
		
		if (strParam1 == _T(""))
		{
			for (int i=0;i<m_cmbRoot.GetCount();i++)
			{
				GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(i);
				delete pData;
			}
			m_cmbRoot.ResetContent();

			int nPos = 0;
			while(nPos != -1)
			{
				strTemp = strData.Tokenize(_T("|"), nPos);
				if (nPos == -1)
					break;
				
				int nTemp = 0;
				CString strKey = strTemp.Tokenize(_T(","),nTemp);
				CString strName = strTemp.Tokenize(_T(","),nTemp);
				if (m_mapFilter.Lookup(strKey,CString()))
				{
					GroupData *pData = new GroupData;
					pData->strGroup = strKey;
					pData->strGroupText = strName;
					int nIndex = m_cmbRoot.AddString(strName);
					m_cmbRoot.SetItemData(nIndex,(DWORD)pData);
				}
			}
			if (m_cmbRoot.GetCount()>0)
			{
				m_cmbRoot.SetCurSel(0);
				OnCbnSelchangecmbroot();
			}
		}
		else if (strParam2 == _T(""))
		{
			for (int i=0;i<m_cmbExchange.GetCount();i++)
			{
				GroupData *pData = (GroupData*)m_cmbExchange.GetItemData(i);
				delete pData;
			}
			m_cmbExchange.ResetContent();

			int nPos = 0;
			while(nPos != -1)
			{
				strTemp = strData.Tokenize(_T("|"), nPos);
				if (nPos == -1)
					break;

				int nTemp = 0;
				CString strKey = strTemp.Tokenize(_T(","),nTemp);
				CString strName = strTemp.Tokenize(_T(","),nTemp);

				GroupData *pData = new GroupData;
				pData->strGroup = strKey;
				pData->strGroupText = strName;
				int nIndex = m_cmbExchange.AddString(strName);
				m_cmbExchange.SetItemData(nIndex,(DWORD)pData);
			}
			if (m_cmbExchange.GetCount()>0)
			{
				m_cmbExchange.SetCurSel(0);
				OnCbnSelchangecmbexchange();
			}
		}
		else if (strParam3 == _T(""))
		{
			for (int i=0;i<m_cmbGroup1.GetCount();i++)
			{
				GroupData *pData = (GroupData*)m_cmbGroup1.GetItemData(i);
				delete pData;
			}
			m_cmbGroup1.ResetContent();

			int nPos = 0;
			while(nPos != -1)
			{
				strTemp = strData.Tokenize(_T("|"), nPos);
				if (nPos == -1)
					break;

				int nTemp = 0;
				CString strKey = strTemp.Tokenize(_T(","),nTemp);
				CString strName = strTemp.Tokenize(_T(","),nTemp);

				GroupData *pData = new GroupData;
				pData->strGroup = strKey;
				pData->strGroupText = strName;
				int nIndex = m_cmbGroup1.AddString(strName);
				m_cmbGroup1.SetItemData(nIndex,(DWORD)pData);
			}
			if (m_cmbGroup1.GetCount()>0)
			{
				m_cmbGroup1.SetCurSel(0);
				OnCbnSelchangecmbgroup1();
			}
		}
		else if (strParam4 == _T(""))
		{
			for (int i=0;i<m_cmbGroup2.GetCount();i++)
			{
				GroupData *pData = (GroupData*)m_cmbGroup2.GetItemData(i);
				delete pData;
			}
			m_cmbGroup2.ResetContent();

			int nPos = 0;
			while(nPos != -1)
			{
				strTemp = strData.Tokenize(_T("|"), nPos);
				if (nPos == -1)
					break;

				int nTemp = 0;
				CString strKey = strTemp.Tokenize(_T(","),nTemp);
				CString strName = strTemp.Tokenize(_T(","),nTemp);

				GroupData *pData = new GroupData;
				pData->strGroup = strKey;
				pData->strGroupText = strName;
				int nIndex = m_cmbGroup2.AddString(strName);
				m_cmbGroup2.SetItemData(nIndex,(DWORD)pData);
			}
			if (m_cmbGroup2.GetCount()>0)
			{
				m_cmbGroup2.SetCurSel(0);
				OnCbnSelchangecmbgroup2();
			}
		}
		else if (strParam5 == _T(""))
		{
			for (int i=0;i<m_cmbGroup3.GetCount();i++)
			{
				GroupData *pData = (GroupData*)m_cmbGroup3.GetItemData(i);
				delete pData;
			}
			m_cmbGroup3.ResetContent();

			int nPos = 0;
			while(nPos != -1)
			{
				strTemp = strData.Tokenize(_T("|"), nPos);
				if (nPos == -1)
					break;

				int nTemp = 0;
				CString strKey = strTemp.Tokenize(_T(","),nTemp);
				CString strName = strTemp.Tokenize(_T(","),nTemp);

				GroupData *pData = new GroupData;
				pData->strGroup = strKey;
				pData->strGroupText = strName;
				int nIndex = m_cmbGroup3.AddString(strName);
				m_cmbGroup3.SetItemData(nIndex,(DWORD)pData);
			}
			if (m_cmbGroup3.GetCount()>0)
			{
				m_cmbGroup3.SetCurSel(0);
				OnCbnSelchangecmbgroup3();
			}
		}
		else
		{

		}
	}
}

void CSymbolSelectDlg::SetFilter(CString strFilter)
{
	int nPos =0;
	while(nPos!=-1)
	{
		CString str = strFilter.Tokenize(_T(";"),nPos);
		if (nPos == -1)
			break;
		m_mapFilter.SetAt(str,str);
	}
}



void CSymbolSelectDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: Add your message handler code here
	for (int i=0;i<m_cmbRoot.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(i);
		delete pData;
	}
	m_cmbRoot.ResetContent();

	for (int i=0;i<m_cmbExchange.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbExchange.GetItemData(i);
		delete pData;
	}
	m_cmbExchange.ResetContent();

	for (int i=0;i<m_cmbGroup1.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbGroup1.GetItemData(i);
		delete pData;
	}
	m_cmbGroup1.ResetContent();

	for (int i=0;i<m_cmbGroup2.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbGroup2.GetItemData(i);
		delete pData;
	}
	m_cmbGroup2.ResetContent();
}


void CSymbolSelectDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	for (int i=0;i<m_cmbRoot.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(i);
		delete pData;
	}
	m_cmbRoot.ResetContent();

	for (int i=0;i<m_cmbExchange.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbExchange.GetItemData(i);
		delete pData;
	}
	m_cmbExchange.ResetContent();

	for (int i=0;i<m_cmbGroup1.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbGroup1.GetItemData(i);
		delete pData;
	}
	m_cmbGroup1.ResetContent();

	for (int i=0;i<m_cmbGroup2.GetCount();i++)
	{
		GroupData *pData = (GroupData*)m_cmbGroup2.GetItemData(i);
		delete pData;
	}
	m_cmbGroup2.ResetContent();

	CString str;
	for(int i=0;i<m_ListSymbol.GetItemCount();i++)
	{ 
		if( m_ListSymbol.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED ) 
		{ 
			m_strSymbol = m_ListSymbol.GetItemText(i,0);
		} 
	}

	CDialogEx::OnOK();
}




void CSymbolSelectDlg::OnDblclkListSymbol(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: Add your control notification handler code here
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 

	if(pNMListView->iItem != -1) 
	{
		OnBnClickedOk();
	}
	*pResult = 0;
}


void CSymbolSelectDlg::OnCbnSelchangecmbgroup3()
{
	// TODO: Add your control notification handler code here
	GroupData *pData = (GroupData*)m_cmbRoot.GetItemData(m_cmbRoot.GetCurSel());
	GroupData *pData2 = (GroupData*)m_cmbExchange.GetItemData(m_cmbExchange.GetCurSel());
	GroupData *pData3 = (GroupData*)m_cmbGroup1.GetItemData(m_cmbGroup1.GetCurSel());
	GroupData *pData4 = (GroupData*)m_cmbGroup2.GetItemData(m_cmbGroup2.GetCurSel());
	GroupData *pData5 = (GroupData*)m_cmbGroup3.GetItemData(m_cmbGroup3.GetCurSel());
	GetSymbolClassification(pData->strGroup,pData2->strGroup,pData3->strGroup,pData4->strGroup,pData5->strGroup);
}


void CSymbolSelectDlg::OnCbnSelchangecmbgroup4()
{
	// TODO: Add your control notification handler code here
}
