// SideBar.cpp : implementation file
//

#include "stdafx.h"
#include "SideBar.h"
#include "INKS.h"
#include "INKSDoc.h"
#include "ChildFrm.h"

// CSideBar dialog

IMPLEMENT_DYNAMIC(CSideBar, CDialogBar)

CSideBar::CSideBar(CWnd* pParent /*=NULL*/)
	: CDialogBar(), m_nSelectedSource(-1)
{
	m_StaticText = new CStatic*[6];
	m_Buttons = new CButton*[2];
	m_pRotEdit = new CEdit*[2];
	m_pRotSpin = new CSpinButtonCtrl*[2];

	m_pStaticInfoText = new CStatic*[11];
	m_pRenameObjectEdit = NULL;
	m_pApplyRename = NULL;
	m_pInOutList = new CListBox*[2];
	//m_pInOutCombo = new CComboBox*[2];
}

CSideBar::~CSideBar()
{
	SAFE_DELETE_ARRAY(m_StaticText);
	SAFE_DELETE_ARRAY(m_Buttons);
	SAFE_DELETE_ARRAY(m_pStaticInfoText);
	SAFE_DELETE_ARRAY(m_pInOutList);
	SAFE_DELETE_ARRAY(m_pRotEdit);
	SAFE_DELETE_ARRAY(m_pRotSpin);
}
void CSideBar::Initialize()
{
	m_pRotSpin[0] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN1);
	m_pRotSpin[1] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN2);
	
	m_pRotEdit[0] = (CEdit*)GetDlgItem(IDC_SIDEROTEDIT1);
	m_pRotEdit[0]->SetWindowTextW(_T("0"));
	m_pRotEdit[1] = (CEdit*)GetDlgItem(IDC_SIDEROTEDIT2);
	m_pRotEdit[1]->SetWindowTextW(_T("0"));

	m_pRotSpin[0]->SetBuddy(m_pRotEdit[0]);
	m_pRotSpin[0]->SetRange(-1,360);
	m_pRotSpin[0]->SetPos(0);
	
	m_pRotSpin[1]->SetBuddy(m_pRotEdit[1]);
	m_pRotSpin[1]->SetRange(-1,360);
	m_pRotSpin[1]->SetPos(0);
}

void CSideBar::UpdatePtrs()
{
	m_ComboBox1 = (CComboBox*)GetDlgItem(IDC_SIDECOMBO1);
	m_ComboBox2 = (CComboBox*)GetDlgItem(IDC_SIDECOMBO2);

	m_StaticText[0] = (CStatic*)GetDlgItem(IDC_SIDEAPPRES);
	m_StaticText[1] = (CStatic*)GetDlgItem(IDC_SIDELABEL1);
	m_StaticText[2] = (CStatic*)GetDlgItem(IDC_SIDEPRORES);
	m_StaticText[3] = (CStatic*)GetDlgItem(IDC_SIDELABEL2);
	m_StaticText[4] = (CStatic*)GetDlgItem(IDC_SIDEROT1);
	m_StaticText[5] = (CStatic*)GetDlgItem(IDC_SIDEROT2);

	m_pRotEdit[0] = (CEdit*)GetDlgItem(IDC_SIDEROTEDIT1);
	m_pRotEdit[1] = (CEdit*)GetDlgItem(IDC_SIDEROTEDIT2);

	m_Buttons[0] = (CButton*)GetDlgItem(IDC_SIDEBUTTON1);
	m_Buttons[1] = (CButton*)GetDlgItem(IDC_SIDEBUTTON2);

	m_pRotSpin[0] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN1);
	m_pRotSpin[1] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN2);

	m_pStaticInfoText[0] = (CStatic*)GetDlgItem(IDC_SIDEOBJINFO);
	m_pStaticInfoText[1] = (CStatic*)GetDlgItem(IDC_SIDEOBJNAME);
	m_pStaticInfoText[2] = (CStatic*)GetDlgItem(IDC_SIDEOBJNAME2);
	m_pStaticInfoText[3] = (CStatic*)GetDlgItem(IDC_SIDEOBJTYPE);
	m_pStaticInfoText[4] = (CStatic*)GetDlgItem(IDC_SIDEOBJTYPE2);
	m_pStaticInfoText[5] = (CStatic*)GetDlgItem(IDC_SIDEOBJRENAME);
	m_pStaticInfoText[6] = (CStatic*)GetDlgItem(IDC_SIDEOBJCONNECTIONS);
	m_pStaticInfoText[7] = (CStatic*)GetDlgItem(IDC_SIDEOBJIN);
	m_pStaticInfoText[8] = (CStatic*)GetDlgItem(IDC_SIDEOBJOUT);
	m_pStaticInfoText[9] = (CStatic*)GetDlgItem(IDC_SIDEOBJORIGIN);
	m_pStaticInfoText[10] = (CStatic*)GetDlgItem(IDC_SIDEOBJORIGIN2);


	m_pRenameObjectEdit = (CEdit*)GetDlgItem(IDC_SIDEOBJRENAMEEDIT1);
	m_pApplyRename = (CButton*)GetDlgItem(IDC_SIDEOJAPPLYRENAMEBUTTON1);

	m_pInOutList[0] = (CListBox*)GetDlgItem(IDC_SIDEOBJINLIST);
	m_pInOutList[1] = (CListBox*)GetDlgItem(IDC_SIDEOBJOUTLIST);
}

void CSideBar::UpdateConnections()
{
	UpdatePtrs();

}

void CSideBar::UpdateSpin1(int nDelta)
{
	m_pRotSpin[0] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN1);
	m_pRotEdit[0] = (CEdit*)GetDlgItem(IDC_SIDEROTEDIT1);
	CString strNumber;
	m_pRotEdit[0]->GetWindowTextW(strNumber);
	int nNumber = _ttoi(strNumber);
	if(nDelta > 0)
	{
		nNumber += 90;
	}else if(nDelta < 0)
	{
		nNumber += 270;
	}
	nNumber = 90 * (nNumber / 90 % 4);
/*	if(m_pRotSpin[0]->GetPos()+nDelta > 359 || nNumber > 359){
		m_pRotSpin[0]->SetPos(0);
	}
	else if(m_pRotSpin[0]->GetPos()+nDelta < 0 || nNumber < 0){
		m_pRotSpin[0]->SetPos(359);
	}
	else {
		m_pRotSpin[0]->SetPos(m_pRotSpin[0]->GetPos()+nDelta);
	}*/
	m_pRotSpin[0]->SetPos(nNumber);
}

void CSideBar::UpdateSpin2(int nDelta)
{
	m_pRotSpin[1] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN2);
	m_pRotEdit[1] = (CEdit*)GetDlgItem(IDC_SIDEROTEDIT2);
	CString strNumber;
	m_pRotEdit[1]->GetWindowTextW(strNumber);
	int nNumber = _ttoi(strNumber);
	if(m_pRotSpin[1]->GetPos()+nDelta > 359 || nNumber > 359){
		m_pRotSpin[1]->SetPos(0);
	}
	else if(m_pRotSpin[1]->GetPos()+nDelta < 0 || nNumber < 0){
		m_pRotSpin[1]->SetPos(359);
	}
	else {
		m_pRotSpin[1]->SetPos(m_pRotSpin[1]->GetPos()+nDelta);
	}
}

int CSideBar::GetObjRotation()
{
	if(m_nSelectedSource == 1){	// application source spin box
		m_pRotSpin[0] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN1);
		return m_pRotSpin[0]->GetPos();
	}
	else if(m_nSelectedSource == 2){ // project source spin box
		m_pRotSpin[1] = (CSpinButtonCtrl*)GetDlgItem(IDC_SIDEROTSPIN2);
		return m_pRotSpin[1]->GetPos();
	}
	return 0;
}

void CSideBar::DoDataExchange(CDataExchange* pDX)
{
	CDialogBar::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CSideBar, CDialogBar)
END_MESSAGE_MAP()

void CSideBar::HideInfo()
{
	for(int i=0; i<11; i++){
		m_pStaticInfoText[i]->ShowWindow(SW_HIDE);
	}
	m_pRenameObjectEdit->ShowWindow(SW_HIDE);
	m_pApplyRename->ShowWindow(SW_HIDE);

	m_pInOutList[0]->ShowWindow(SW_HIDE);
	m_pInOutList[1]->ShowWindow(SW_HIDE);
}

void CSideBar::ShowInfo(CINKSDoc *pDoc)
{
	UpdatePtrs();
	if(!pDoc){
		HideInfo();
		return;
	}
	
	INT_PTR nSize = pDoc->m_SOList.GetSize();
	if(nSize == 0){ // no element selected = >display info about opened document
		HideInfo();
		INT_PTR nCOSize = pDoc->m_COList.GetSize();

		CString strHead;
		CString strType;
		if(pDoc->m_nDocumentType == 0){ // project document
			m_pStaticInfoText[0]->SetWindowTextW(_T("Project"));
			m_pStaticInfoText[2]->SetWindowTextW(pDoc->GetTitle());
			m_pStaticInfoText[0]->ShowWindow(SW_SHOW);
			m_pStaticInfoText[1]->ShowWindow(SW_SHOW);
			m_pStaticInfoText[2]->ShowWindow(SW_SHOW);
			strType = _T("Project");
		}
		else if(pDoc->m_nDocumentType == 1){ // scheme document
			strType = _T("Scheme");
		}
		else if(pDoc->m_nDocumentType == 2){ // block document
			strType = _T("Block");
		}
		strHead = strType;
		strHead.Append(_T(" information ("));
		CString strNumber;
		strNumber.Format(_T("%d"), (int)nCOSize);
		strHead.Append(strNumber);
		strHead.Append(_T(") objects"));

		m_pStaticInfoText[0]->SetWindowTextW(strHead);
		m_pStaticInfoText[2]->SetWindowTextW(pDoc->GetTitle());
		m_pStaticInfoText[4]->SetWindowTextW(strType);
		for(int i=0; i<5; i++){
			m_pStaticInfoText[i]->ShowWindow(SW_SHOW);
		}
		return;
	}
	else if(nSize == 1){ // one element selected => display its info
		CCommonObject *pObj =  pDoc->m_SOList.GetHead();
		m_pStaticInfoText[10]->SetWindowTextW((CString)(pObj->m_strOrigin));
		m_pStaticInfoText[2]->SetWindowTextW(pObj->m_strUniqueName);
		CString strType;
		switch(pObj->m_nObjType){
			case 2:
				strType = _T("Block");
				break;
			case 3:
				strType = _T("Gate");
				break;
			case 4:
				strType = _T("Wire");
				break;
			case 5:
				strType = _T("Tag");
				break;
			case 6:
				strType = _T("Invertor");
				break;
			default:
				TRACE(_T("Unrecognized object type in CSideBar::ShowInfo\n"));
				return;
				strType = _T("");
				break;
		}
		m_pStaticInfoText[4]->SetWindowTextW(strType);
		CString strHead = strType;
		strHead.Append(_T(" information"));
		m_pStaticInfoText[0]->SetWindowTextW(strHead);

		m_pInOutList[0]->ResetContent();
		m_pInOutList[1]->ResetContent();
		CString strShow;
		std::vector<CCommonObject *> inObjs = pObj->GetConnectedObjs(true);
		std::vector<CCommonObject *> outObjs = pObj->GetConnectedObjs(false);
		for(int i=0; i<inObjs.size(); i++){
			if(inObjs[i]){
				strShow.Format(_T("(%d) "), i);
				strShow.Append(inObjs[i]->m_strUniqueName);
				m_pInOutList[0]->AddString(strShow);
			}
		}
		for(int i=0; i<outObjs.size(); i++){
			
			if(outObjs[i]){
				strShow.Format(_T("(%d) "), i);
				strShow.Append(outObjs[i]->m_strUniqueName);
				m_pInOutList[1]->AddString(strShow);
			}
		}
//TODO nahradit m_Connection
	}
	else{ // multiple selected
		HideInfo();
		CString strHead = _T("Objects information (");
		CString strNumber;
		strNumber.Format(_T("%d"), (int)nSize);
		strHead.Append(strNumber);
		strHead.Append(_T(")"));
		m_pStaticInfoText[0]->SetWindowTextW(strHead);
		m_pStaticInfoText[2]->SetWindowTextW(_T("multiple"));
		m_pStaticInfoText[0]->ShowWindow(SW_SHOW);
		m_pStaticInfoText[1]->ShowWindow(SW_SHOW);
		m_pStaticInfoText[2]->ShowWindow(SW_SHOW);
		return;

	}
	for(int i=0; i<11; i++)
		m_pStaticInfoText[i]->ShowWindow(SW_SHOW);
	m_pRenameObjectEdit->ShowWindow(SW_SHOW);
	m_pApplyRename->ShowWindow(SW_SHOW);

	m_pInOutList[0]->ShowWindow(SW_SHOW);
	m_pInOutList[1]->ShowWindow(SW_SHOW);
}

void CSideBar::SelNoSubdlg()
{
	UpdatePtrs();
	//ShowInfo();
	for(int i=0; i<6; i++)
		m_StaticText[i]->ShowWindow(SW_HIDE);
	m_Buttons[0]->ShowWindow(SW_HIDE);
	m_Buttons[1]->ShowWindow(SW_HIDE);
	m_ComboBox1->ResetContent();
	m_ComboBox1->ShowWindow(SW_HIDE);
	m_ComboBox2->ResetContent();
	m_ComboBox2->ShowWindow(SW_HIDE);
	m_nSelectedSource = -1;

	m_pRotEdit[0]->ShowWindow(SW_HIDE);
	m_pRotEdit[1]->ShowWindow(SW_HIDE);

	m_Buttons[0]->ShowWindow(SW_HIDE);
	m_Buttons[1]->ShowWindow(SW_HIDE);

	m_pRotSpin[0]->ShowWindow(SW_HIDE);
	m_pRotSpin[1]->ShowWindow(SW_HIDE);
}

void CSideBar::SelGateSubdlg()
{
	if(!theApp.m_pElementCont || !theApp.m_pOpenedProjectDoc->m_pElementCont)
		return;
	UpdatePtrs();
	HideInfo();
	//m_pDoc = CChildFrame::GetChildDocument();
	m_ComboBox1->ResetContent();

	POSITION pos = theApp.m_pElementCont->m_GateMap.GetStartPosition();
	CCommonObject* pObj= NULL;
	while(pos != NULL){
		CString string;
		theApp.m_pElementCont->m_GateMap.GetNextAssoc(pos, string, pObj);
		m_ComboBox1->AddString(string);
	}
	m_ComboBox1->SetCurSel(0);
	m_ComboBox1->ShowWindow(SW_SHOW);

	m_ComboBox2->ResetContent();

	pos = theApp.m_pOpenedProjectDoc->m_pElementCont->m_GateMap.GetStartPosition();
	pObj= NULL;
	while(pos != NULL){
		CString string;
		theApp.m_pOpenedProjectDoc->m_pElementCont->m_GateMap.GetNextAssoc(pos, string, pObj);
		m_ComboBox2->AddString(string);
	}
	m_ComboBox2->SetCurSel(0);
	m_ComboBox2->ShowWindow(SW_SHOW);

	for(int i=0; i<6; i++)
		m_StaticText[i]->ShowWindow(SW_SHOW);
	m_Buttons[0]->ShowWindow(SW_SHOW);
	m_Buttons[1]->ShowWindow(SW_SHOW);
	m_nSelectedSource = -1;

	m_pRotEdit[0]->ShowWindow(SW_SHOW);
	m_pRotEdit[1]->ShowWindow(SW_SHOW);

	m_Buttons[0]->ShowWindow(SW_SHOW);
	m_Buttons[1]->ShowWindow(SW_SHOW);

	m_pRotSpin[0]->ShowWindow(SW_SHOW);
	m_pRotSpin[1]->ShowWindow(SW_SHOW);
}

void CSideBar::SelBlockSubdlg()
{
	if(!theApp.m_pElementCont || !theApp.m_pOpenedProjectDoc->m_pElementCont)
		return;
	UpdatePtrs();
	HideInfo();
	//m_pDoc = CChildFrame::GetChildDocument();
	m_ComboBox1->ResetContent();

	POSITION pos = theApp.m_pElementCont->m_BlockMap.GetStartPosition();
	CCommonObject* pObj= NULL;
	while(pos != NULL){
		CString string;
		theApp.m_pElementCont->m_BlockMap.GetNextAssoc(pos, string, pObj);
		m_ComboBox1->AddString(string);
	}
	m_ComboBox1->SetCurSel(0);
	m_ComboBox1->ShowWindow(SW_SHOW);

	m_ComboBox2->ResetContent();

	pos = theApp.m_pOpenedProjectDoc->m_pElementCont->m_BlockMap.GetStartPosition();
	pObj= NULL;
	while(pos != NULL){
		CString string;
		theApp.m_pOpenedProjectDoc->m_pElementCont->m_BlockMap.GetNextAssoc(pos, string, pObj);
		m_ComboBox2->AddString(string);
	}
	m_ComboBox2->SetCurSel(0);
	m_ComboBox2->ShowWindow(SW_SHOW);

	for(int i=0; i<4; i++)
		m_StaticText[i]->ShowWindow(SW_SHOW);
	m_Buttons[0]->ShowWindow(SW_SHOW);
	m_Buttons[1]->ShowWindow(SW_SHOW);
	m_nSelectedSource = -1;

	m_pRotEdit[0]->ShowWindow(SW_SHOW);
	m_pRotEdit[1]->ShowWindow(SW_SHOW);

	m_Buttons[0]->ShowWindow(SW_SHOW);
	m_Buttons[1]->ShowWindow(SW_SHOW);

	m_pRotSpin[0]->ShowWindow(SW_SHOW);
	m_pRotSpin[1]->ShowWindow(SW_SHOW);
}

CString CSideBar::GetGateName()
{
	CString strText;
	CComboBox *pTemp = NULL;
	if(m_nSelectedSource == 1){
		pTemp = (CComboBox*)GetDlgItem(IDC_SIDECOMBO1);
	}
	else if(m_nSelectedSource == 2){
		pTemp = (CComboBox*)GetDlgItem(IDC_SIDECOMBO2);
	}
	else{
		MessageBox(_T("Pleas select source"), _T("Information"), MB_ICONINFORMATION);
		return _T("");
	}
	int nSel = pTemp->GetCurSel();
	if(nSel < 0)
		return _T("");
	pTemp->GetLBText(nSel, strText);
	return strText;
}

void CSideBar::InsertObjectHit(int nButton)
{
	CButton *pInsButton;
	if(nButton == 0){
		pInsButton = (CButton*)GetDlgItem(IDC_SIDEBUTTON1);
	}
	else if(nButton == 1){
		pInsButton = (CButton*)GetDlgItem(IDC_SIDEBUTTON2);
	}
	else return;
	pInsButton->SetState(!(pInsButton->GetState() & 0x0004));
}

void CSideBar::ObjectInserted()
{
	CButton *pInsButton;
	if(m_nSelectedSource == 1){
		pInsButton = (CButton*)GetDlgItem(IDC_SIDEBUTTON1);
	}
	else if(m_nSelectedSource == 2){
		pInsButton = (CButton*)GetDlgItem(IDC_SIDEBUTTON2);
	}
	else return;
	pInsButton->SetState(!(pInsButton->GetState() & 0x0004));
	m_nSelectedSource = -1;
}