// LatticeLinkDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed.h"
#include "LatticeLinkDlg.h"
#include "EditorWaypoint.h"
#include <Adept\Map.hpp>


/////////////////////////////////////////////////////////////////////////////
// CLatticeLinkDlg dialog


CLatticeLinkDlg::CLatticeLinkDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLatticeLinkDlg::IDD, pParent),
	m_UndoDeleteChain(NULL),
	m_UndoDataChain(NULL)
{
	//{{AFX_DATA_INIT(CLatticeLinkDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

}

CLatticeLinkDlg::~CLatticeLinkDlg()
{
}

void CLatticeLinkDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLatticeLinkDlg)
	DDX_Control(pDX, IDC_LINKLIST, m_LinksList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLatticeLinkDlg, CDialog)
	//{{AFX_MSG_MAP(CLatticeLinkDlg)
	ON_BN_CLICKED(IDC_LLCHECKFLY, OnLlcheckfly)
	ON_BN_CLICKED(IDC_LLCHECKHELI, OnLlcheckheli)
	ON_BN_CLICKED(IDC_LLCHECKHOVER, OnLlcheckhover)
	ON_BN_CLICKED(IDC_LLCHECKJUMP, OnLlcheckjump)
	ON_BN_CLICKED(IDC_LLCHECKLEG, OnLlcheckleg)
	ON_BN_CLICKED(IDC_LLCHECKTRACK, OnLlchecktrack)
	ON_BN_CLICKED(IDC_LLCHECKWHEEL, OnLlcheckwheel)
	ON_EN_CHANGE(IDC_LL_WEIGHTFLY, OnChangeLlWeightfly)
	ON_EN_CHANGE(IDC_LL_WEIGHTHELI, OnChangeLlWeightheli)
	ON_EN_CHANGE(IDC_LL_WEIGHTHOVER, OnChangeLlWeighthover)
	ON_EN_CHANGE(IDC_LL_WEIGHTJUMP, OnChangeLlWeightjump)
	ON_EN_CHANGE(IDC_LL_WEIGHTLEG, OnChangeLlWeightleg)
	ON_EN_CHANGE(IDC_LL_WEIGHTTRACK, OnChangeLlWeighttrack)
	ON_EN_CHANGE(IDC_LL_WEIGHTWHEEL, OnChangeLlWeightwheel)
	ON_LBN_SELCHANGE(IDC_LINKLIST, OnSelchangeLinklist)
	ON_BN_CLICKED(IDC_BLOCKLINKBTN, OnBlocklinkbtn)
	ON_BN_CLICKED(IDC_REMOVELINKBTN, OnRemovelinkbtn)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDOK, OnMyOK)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLatticeLinkDlg message handlers

BOOL CLatticeLinkDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

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

void CLatticeLinkDlg::SetNode(Entity* newNode) 
{
	editNode = newNode;

	m_LinksList.ResetContent();

	char string[32];
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);

	for (int i = 0; i < node->railNode->NumLinks(); i++)
	{
		sprintf(string,"Link_%d",node->railNode->Link(i)->ID());
		m_LinksList.SetItemData(m_LinksList.AddString(string),i);

		OriginalLinkData* orgData = new OriginalLinkData(node->railNode->Link(i));
		m_UndoDataChain.Add(orgData);
	}

	m_LinksList.SetCurSel(0);

	UpdateFields();
	OnSelchangeLinklist();
}

void CLatticeLinkDlg::UpdateFields() 
{
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	int	curSel = m_LinksList.GetItemData(m_LinksList.GetCurSel());


	((CEdit*)GetDlgItem(IDC_LLNODE1EDIT))->SetWindowText((char*)node->instanceName);

	// Find the other one, This might take some time

	CRailNode*	railNode = node->railNode->Link(curSel)->Location(0);
	if (railNode == node->railNode)
		railNode = node->railNode->Link(curSel)->Location(1);

	Check_Object(Map::Instance);
	ChainIteratorOf<Entity *> iterator(&Map::Instance->childEntityChain);
	Entity* entity;

	while((entity = iterator.ReadAndNext()) != NULL)
	{
		if (entity->IsDerivedFrom(EditorLatticeNode::DefaultData))
		{
			EditorLatticeNode* latNode = Cast_Object(EditorLatticeNode*,entity);
			if (latNode->railNode == railNode)
			{
				((CEdit*)GetDlgItem(IDC_LLNODE2EDIT))->SetWindowText((char*)latNode->instanceName);
				break;
			}
		}
	}

	SetTypeCombo(curSel,IDC_LLCHECKLEG,IDC_LL_WEIGHTLEG,MW4AI::LEGINDEX);
	SetTypeCombo(curSel,IDC_LLCHECKJUMP,IDC_LL_WEIGHTJUMP,MW4AI::JUMPINDEX);
	SetTypeCombo(curSel,IDC_LLCHECKTRACK,IDC_LL_WEIGHTTRACK,MW4AI::TRACKINDEX);
	SetTypeCombo(curSel,IDC_LLCHECKHOVER,IDC_LL_WEIGHTHOVER,MW4AI::HOVERINDEX);
	SetTypeCombo(curSel,IDC_LLCHECKWHEEL,IDC_LL_WEIGHTWHEEL,MW4AI::WHEELINDEX);
	SetTypeCombo(curSel,IDC_LLCHECKFLY,IDC_LL_WEIGHTFLY,MW4AI::FLYINDEX);
	SetTypeCombo(curSel,IDC_LLCHECKHELI,IDC_LL_WEIGHTHELI,MW4AI::HELIINDEX);
}

void CLatticeLinkDlg::SetTypeCombo(int curSel, int checkID, int editID, int index)
{
	char string[32];
	int checked;
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	CEdit* edit;

	checked = node->railNode->Link(curSel)->Legged(node->railNode);
	((CButton*)GetDlgItem(checkID))->SetCheck(checked);
	edit = (CEdit*)GetDlgItem(editID);
	if (checked)
	{
		edit->EnableWindow();
		sprintf(string,"%f",node->railNode->Link(curSel)->Weight(index,node->railNode));
		edit->SetWindowText(string);
	}
	else
	{
		edit->EnableWindow(FALSE);
		edit->SetWindowText("N/A");
	}
}

void CLatticeLinkDlg::OnLlcheckfly() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Flyer(((CButton*)GetDlgItem(IDC_LLCHECKFLY))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKFLY,IDC_LL_WEIGHTFLY,MW4AI::FLYINDEX);
}

void CLatticeLinkDlg::OnLlcheckheli() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Heli(((CButton*)GetDlgItem(IDC_LLCHECKHELI))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKHELI,IDC_LL_WEIGHTHELI,MW4AI::HELIINDEX);
}

void CLatticeLinkDlg::OnLlcheckhover() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Hover(((CButton*)GetDlgItem(IDC_LLCHECKHOVER))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKHOVER,IDC_LL_WEIGHTHOVER,MW4AI::HOVERINDEX);
}

void CLatticeLinkDlg::OnLlcheckjump() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Jump(((CButton*)GetDlgItem(IDC_LLCHECKJUMP))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKJUMP,IDC_LL_WEIGHTJUMP,MW4AI::JUMPINDEX);
}

void CLatticeLinkDlg::OnLlcheckleg() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Legged(((CButton*)GetDlgItem(IDC_LLCHECKLEG))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKLEG,IDC_LL_WEIGHTLEG,MW4AI::LEGINDEX);
}

void CLatticeLinkDlg::OnLlchecktrack() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Tracked(((CButton*)GetDlgItem(IDC_LLCHECKTRACK))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKTRACK,IDC_LL_WEIGHTTRACK,MW4AI::TRACKINDEX);
}

void CLatticeLinkDlg::OnLlcheckwheel() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Wheeled(((CButton*)GetDlgItem(IDC_LLCHECKWHEEL))->GetCheck(),node->railNode);
	SetTypeCombo(m_LinksList.GetItemData(m_LinksList.GetCurSel()),IDC_LLCHECKWHEEL,IDC_LL_WEIGHTWHEEL,MW4AI::WHEELINDEX);
}

void CLatticeLinkDlg::OnChangeLlWeightfly() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTFLY))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::FLYINDEX,node->railNode);
}

void CLatticeLinkDlg::OnChangeLlWeightheli() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTHELI))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::HELIINDEX,node->railNode);
}

void CLatticeLinkDlg::OnChangeLlWeighthover() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTHOVER))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::HOVERINDEX,node->railNode);
}

void CLatticeLinkDlg::OnChangeLlWeightjump() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTJUMP))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::JUMPINDEX,node->railNode);
}

void CLatticeLinkDlg::OnChangeLlWeightleg() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTLEG))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::LEGINDEX,node->railNode);
}

void CLatticeLinkDlg::OnChangeLlWeighttrack()
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTTRACK))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::TRACKINDEX,node->railNode);
}

void CLatticeLinkDlg::OnChangeLlWeightwheel() 
{
#pragma warning (disable :4800)
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	char string[32];
	((CEdit*)GetDlgItem(IDC_LL_WEIGHTWHEEL))->GetWindowText(string,31);
	node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()))->Weight((float)atof(string),MW4AI::WHEELINDEX,node->railNode);
}

void CLatticeLinkDlg::OnCancel() 
{
	OriginalLinkData*	datLink;
	ChainIteratorOf<OriginalLinkData*> iterator(&m_UndoDataChain);

	iterator.First();
	while((datLink = iterator.GetCurrent()) != NULL)
	{
		datLink->link->Flyer(datLink->m_flyUse,datLink->link->Location (0));
		datLink->link->Heli(datLink->m_heliUse,datLink->link->Location (0));
		datLink->link->Hover(datLink->m_hoverUse,datLink->link->Location (0));
		datLink->link->Jump(datLink->m_jumpUse,datLink->link->Location (0));
		datLink->link->Legged(datLink->m_legUse,datLink->link->Location (0));
		datLink->link->Tracked(datLink->m_trackUse,datLink->link->Location (0));
		datLink->link->Wheeled(datLink->m_wheelUse,datLink->link->Location (0));

		datLink->link->Weight(datLink->m_flyWeight,MW4AI::JUMPINDEX,datLink->link->Location (0));
		datLink->link->Weight(datLink->m_heliWeight,MW4AI::TRACKINDEX,datLink->link->Location (0));
		datLink->link->Weight(datLink->m_hoverWeight,MW4AI::FLYINDEX,datLink->link->Location (0));
		datLink->link->Weight(datLink->m_jumpWeight,MW4AI::WHEELINDEX,datLink->link->Location (0));
		datLink->link->Weight(datLink->m_legWeight,MW4AI::LEGINDEX,datLink->link->Location (0));
		datLink->link->Weight(datLink->m_trackWeight,MW4AI::HOVERINDEX,datLink->link->Location (0));
		datLink->link->Weight(datLink->m_wheelWeight,MW4AI::HELIINDEX,datLink->link->Location (0));

		m_UndoDataChain.Remove(datLink);
		iterator.First();
	}
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	CRailLink* link;
	DeletedLink*		delLink;
	ChainIteratorOf<DeletedLink*> deliterator(&m_UndoDeleteChain);
	RGBAColor gray(192,192,192,0);

	deliterator.First();
	while((delLink = deliterator.GetCurrent()) != NULL)
	{
		link = node->railNode->Link(delLink->id);
		link->SetLinkColor(gray);
		link->ShowLink(true);

		m_UndoDeleteChain.Remove(delLink);
		deliterator.First();
	}

	CDialog::OnCancel();
}

void CLatticeLinkDlg::OnOK() 
{
//	CDialog::OnOK();
}

void CLatticeLinkDlg::OnSelchangeLinklist() 
{
	static CRailLink* last = NULL;
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);

	RGBAColor pink(255,255,255,0),gray(192,192,192,0);
	if (last)
		last->SetLinkColor(gray);

	last = node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()));
	last->SetLinkColor(pink);
	UpdateFields();
}

void CLatticeLinkDlg::OnBlocklinkbtn() 
{
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	CRailLink* link = node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()));

	link->ShowLink(false);
	DeletedLink* delLink = new DeletedLink(m_LinksList.GetItemData(m_LinksList.GetCurSel()),true);
	delLink->node1 = link->Location(0)->ID();
	delLink->node2 = link->Location(1)->ID();
	m_UndoDeleteChain.Add(delLink);
	m_LinksList.DeleteString(m_LinksList.GetCurSel());
}

void CLatticeLinkDlg::OnRemovelinkbtn() 
{
	EditorLatticeNode* node = Cast_Object(EditorLatticeNode*,editNode);
	CRailLink* link = node->railNode->Link(m_LinksList.GetItemData(m_LinksList.GetCurSel()));

	link->ShowLink(false);
	DeletedLink* delLink = new DeletedLink(m_LinksList.GetItemData(m_LinksList.GetCurSel()),false);
	m_UndoDeleteChain.Add(delLink);
	m_LinksList.DeleteString(m_LinksList.GetCurSel());
}

void CLatticeLinkDlg::OnClose() 
{
	ShowWindow(SW_HIDE);	
//	CDialog::OnClose();
}

void CLatticeLinkDlg::OnMyOK() 
{
	DeletedLink*		delLink;
	ChainIteratorOf<DeletedLink*> iterator(&m_UndoDeleteChain);

	iterator.First();
	while((delLink = iterator.GetCurrent()) != NULL)
	{
		MW4AI::g_MissionGraph->DeleteLink(delLink->id);
		if (delLink->block)
			MW4AI::g_MissionGraph->BlockLink(delLink->node1,delLink->node2);

		m_UndoDeleteChain.Remove(delLink);
		iterator.First();
	}
	CDialog::OnOK();
}
