// CollapseDialog.cpp : implementation file
//

#include "stdafx.h"
#include "INKS.h"
#include "CollapseDialog.h"
#include "INKSView.h"
#include "INKSDoc.h"
#include "CommonObject.h"
#include "Tag.h"
// CCollapseDialog dialog

IMPLEMENT_DYNAMIC(CCollapseDialog, CDialog)

CCollapseDialog::CCollapseDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CCollapseDialog::IDD, pParent)
{
	m_strlistOuts.RemoveAll();
	m_strlistIns.RemoveAll();
}

CCollapseDialog::~CCollapseDialog()
{
}

void CCollapseDialog::DoDataExchange(CDataExchange* pDX)
{
	DDX_Text(pDX, IDC_COLEDIT1, m_strBlockName);
	CDialog::DoDataExchange(pDX);
}

BOOL CCollapseDialog::OnInitDialog()
{
	POSITION p;
	p = m_strlistIns.GetHeadPosition();
	CListBox *list = (CListBox*)GetDlgItem(1012);
	while(p)
		list->AddString(m_strlistIns.GetNext(p));
	p = m_strlistOuts.GetHeadPosition();
	list = (CListBox*)GetDlgItem(1013);
	while(p)
		list->AddString(m_strlistOuts.GetNext(p));
	CDialog::OnInitDialog();
	return TRUE;
}

BEGIN_MESSAGE_MAP(CCollapseDialog, CDialog)
	ON_CONTROL(LBN_SELCHANGE, IDC_COLLIST1, OnInListChange)
	ON_CONTROL(LBN_SELCHANGE, IDC_COLLIST2, OnOutListChange)
	ON_BN_CLICKED(IDC_COLBUTTON1, OnChangeInName)
	ON_BN_CLICKED(IDC_COLBUTTON2, OnChangeOutName)
	
	ON_BN_CLICKED(IDOK, &CCollapseDialog::OnBnClickedOk)
END_MESSAGE_MAP()

bool CCollapseDialog::InitDialog(CList<CCommonObject*, CCommonObject*> *pList)
{
	bool bRet = false;
	POSITION pos = pList->GetHeadPosition();
	CCommonObject *pObj;
	while(pos){
		pObj = pList->GetNext(pos);
		if(dynamic_cast<CTag *>(pObj)){	// choose only tags
			if(((CTag*)pObj)->m_nTagType == 1){ // InTag
				m_strlistIns.AddTail(pObj->m_strUniqueName);
			}
			else{ // OutTag
				m_strlistOuts.AddTail(pObj->m_strUniqueName);
			}
			bRet = true;
		}
	}
	return bRet;
}

void CCollapseDialog::OnInListChange()
{
	CListBox *list = (CListBox*)GetDlgItem(IDC_COLLIST1);
	int nIndex;
	CString strSel;

	nIndex = list->GetCurSel();
	list->GetText(nIndex, strSel);

	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_COLEDIT2);
	pEdit->SetWindowTextW(strSel);
}

void CCollapseDialog::OnOutListChange()
{
	CListBox *list = (CListBox*)GetDlgItem(IDC_COLLIST2);
	int nIndex;
	CString strSel;

	nIndex = list->GetCurSel();
	list->GetText(nIndex, strSel);

	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_COLEDIT3);
	pEdit->SetWindowTextW(strSel);
}

void CCollapseDialog::OnChangeInName()
{
	CListBox *list = (CListBox*)GetDlgItem(IDC_COLLIST1);
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_COLEDIT2);
	CString strNewName;
	pEdit->GetWindowTextW(strNewName);
	int nIndex;
	CString strOldName;
	nIndex = list->GetCurSel();
	list->GetText(nIndex, strOldName);
	list->DeleteString(nIndex);
	list->InsertString(nIndex, strNewName);
	list->SetCurSel(nIndex);
	//check for unique input name
	POSITION p;
	p = m_strlistIns.Find(strOldName);
	m_strlistIns.SetAt(p, strNewName);
}

void CCollapseDialog::OnChangeOutName()
{
	CListBox *list = (CListBox*)GetDlgItem(IDC_COLLIST2);
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_COLEDIT3);
	CString strNewName;
	pEdit->GetWindowTextW(strNewName);
	int nIndex;
	CString strOldName;
	nIndex = list->GetCurSel();
	list->GetText(nIndex, strOldName);
	list->DeleteString(nIndex);
	list->InsertString(nIndex, strNewName);
	list->SetCurSel(nIndex);
	//check for unique input name
	POSITION p;
	p = m_strlistOuts.Find(strOldName);
	m_strlistOuts.SetAt(p, strNewName);
}


void CCollapseDialog::OnBnClickedOk()
{
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_COLEDIT1);
	CString strBlockName;
	pEdit->GetWindowTextW(strBlockName);
	if(!strBlockName.GetLength()){
		MessageBox(_T("Please enter block name"), _T("Information"), MB_ICONINFORMATION);
		return;
	}
	if(theApp.m_pElementCont->CanProContainElement(strBlockName)){
		MessageBox(_T("Block with this name already exists in this project"), _T("Error"), MB_ICONERROR);
		return;
	}

	if(strBlockName.CompareNoCase(_T("Block")) == 0){
		MessageBox(_T("Block name not allowed!"), _T("Error"), MB_ICONERROR);
		return;
	}

	m_strBlockName = strBlockName;
	// TODO: Add your control notification handler code here
	OnOK();
}
