// StateParamView.cpp : implementation file
//

#include "stdafx.h"
#include "AnimScript.h"
#include "StateParamView.h"
#include "AnimScriptDoc.h"

#include "mw4headers.hpp"
#include "Vehicle.hpp"
#include "MechAnimationState.hpp"

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

/////////////////////////////////////////////////////////////////////////////
// CStateParamView
IMPLEMENT_DYNCREATE(CStateParamView, CFormView)

CStateParamView::CStateParamView()
	: CFormView(CStateParamView::IDD)
{
	//{{AFX_DATA_INIT(CStateParamView)
	//}}AFX_DATA_INIT

}

CStateParamView::~CStateParamView()
{
}

void CStateParamView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStateParamView)
	DDX_Control(pDX, IDC_CREATE, m_ctrlCreateButton);
	DDX_Control(pDX, IDC_STATEPARAM_TITLE, m_ctrlTitle);
	DDX_Control(pDX, IDC_COMBO_ANIMTYPE, m_ctrlAnimType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStateParamView, CFormView)
	//{{AFX_MSG_MAP(CStateParamView)
	ON_BN_CLICKED(IDC_CREATE, OnCreateExec)
	ON_WM_CREATE()
	ON_CBN_SELENDOK(IDC_COMBO_ANIMTYPE, OnSelendokComboAnimtype)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStateParamView diagnostics

#ifdef _DEBUG
void CStateParamView::AssertValid() const
{
	CFormView::AssertValid();
}

void CStateParamView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CStateParamView message handlers

void CStateParamView::OnInitialUpdate() 
{
	CAnimScriptDoc* pDoc = (CAnimScriptDoc *)GetDocument();
	CFormView::OnInitialUpdate();
	m_ctrlAnimType.EnableWindow(false);

	m_ctrlAnimType.ResetContent();
	POSITION pos = pDoc->m_listAnimTypes.GetHeadPosition();
	while (pos)
	{
		CObject *pObj = pDoc->m_listAnimTypes.GetNext(pos);
		CAnimTypeNode *pNode = (CAnimTypeNode *)pObj;
		int index = m_ctrlAnimType.AddString(pNode->m_strName);
		m_ctrlAnimType.SetItemData(index,(DWORD)pNode);
	}
	// Second pass - set the index on data because the items may be sorted when entered
	for (int x=0;x<m_ctrlAnimType.GetCount();x++)
	{
		CAnimTypeNode *pNode = (CAnimTypeNode *)m_ctrlAnimType.GetItemData(x);
		pNode->m_iIndex = x;
	}
}

void CStateParamView::OnCreateExec() 
{
	CAnimScriptDoc* pDoc = (CAnimScriptDoc *)GetDocument();
	CAnimNode *pNode = pDoc->m_pSelectedNode;
	if (pNode)
	{
		if (pNode->m_bValid)
		{
			pNode->m_bValid = false;
		}
		else
		{
			pNode->m_bValid = true;
			CAnimTypeNode *pType = (CAnimTypeNode *)pNode->GetParam(PARAM_ANIMTYPE);
			if (NULL == pType)
			{
				pType = (CAnimTypeNode *)m_ctrlAnimType.GetItemData(0);
				assert(NULL != pType);
				pNode->SetParam(PARAM_ANIMTYPE,pType);
			}

			pNode->LoadState(NULL);
		}
		for (int x=0;x<3;x++)
			m_arrPages[x].OnCreateExec();
		pDoc->UpdateAllViews(NULL,0,NULL);
	}
}

void CStateParamView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	CAnimScriptDoc* pDoc = (CAnimScriptDoc *)GetDocument();
	CAnimNode *pNode = pDoc->m_pSelectedNode;

	CString strTitle = "Parameters";
	CString strCreate = "Create";
	int iCurSel = -1;
	bool bEnable = false;
	if (pNode)
	{
		strTitle = pNode->GetName();
		if (pNode->m_bValid)
		{
			bEnable = true;
			strCreate = "Delete";
			CAnimTypeNode *pType = (CAnimTypeNode *)pNode->GetParam(PARAM_ANIMTYPE);
			iCurSel = (pType)?pType->m_iIndex:-1;
		}
	}
	m_ctrlAnimType.EnableWindow(bEnable);
	m_ctrlAnimType.SetCurSel(iCurSel);
	m_ctrlTitle.SetWindowText(strTitle);
	m_ctrlCreateButton.SetWindowText(strCreate);
	for (int x=0;x<3;x++)
		m_arrPages[x].OnApply();
}

int CStateParamView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	CAnimScriptDoc* pDoc = (CAnimScriptDoc *)GetDocument();

	char szTitle[][30] = 
	{
		"Animation",
		"Transition",
		"Animation Type Parameters"
	};
	// Create the Property Sheet
	for (int x=0;x<3;x++)
	{
		m_arrPages[x].m_pDocument = pDoc;
		m_arrPages[x].m_iPageType = x;
		m_arrPages[x].m_psp.pszTitle = szTitle[x];
		m_arrPages[x].m_psp.dwFlags |= PSP_USETITLE;
		m_ctrlPropertySheet.AddPage(&m_arrPages[x]);		
	}
	m_ctrlPropertySheet.EnableStackedTabs(false);
	m_ctrlPropertySheet.Create(this,WS_VISIBLE | WS_CHILD);
	CRect rc;
	GetClientRect(&rc);
	// TODO:  Need to have the property sheet at this location
	CWnd *pWnd = this->GetDlgItem(IDC_PROPSHEET_POS);
	//CRect rc1;
	//pWnd->GetWindowRect(&rc1);
	CPoint pt(280,5);
	//pWnd->ScreenToClient(&pt);
	rc.OffsetRect(pt);
	m_ctrlPropertySheet.SetWindowPos(this,rc.left,rc.top,rc.right,rc.bottom,SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER |SWP_NOACTIVATE);
	return 0;
}

void CStateParamView::OnSelendokComboAnimtype() 
{
	int index = m_ctrlAnimType.GetCurSel();
	if (index>=0)
	{
		CAnimTypeNode *pNode = (CAnimTypeNode *)m_ctrlAnimType.GetItemData(index);
		// index should be equal to pNode->m_iIndex
		assert(index == pNode->m_iIndex);
		CAnimScriptDoc* pDoc = (CAnimScriptDoc *)GetDocument();
		if (pDoc->m_pSelectedNode)
		{
			pDoc->m_pSelectedNode->SetParam(PARAM_ANIMTYPE,pNode);
			pDoc->m_pSelectedNode->CreateAnimTypeParams(pNode);
			pDoc->UpdateAllViews(NULL,1,NULL);
		}
	}
}
