// CPACMSBu.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "EDACBuM.hpp"

#include "EDACMStr.hpp"
#include "EDACMStA.hpp"
#include "EDACActr.hpp"
#include "EDACInst.hpp"
#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

//External Modules
#include "ErO.h"
//End of External Modules

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

/////////////////////////////////////////////////////////////////////////////
// CPA_Editor_MiniStructureButton

BEGIN_MESSAGE_MAP(CPA_Editor_MiniStructureButton, CButton)
	//{{AFX_MSG_MAP(CPA_Editor_MiniStructureButton)
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
CPA_Editor_MiniStructureButton::CPA_Editor_MiniStructureButton(	EdActors_MiniStructure *pclMS,
														CString csText,
														tdeCarMSButtonType tdeType)

{
	m_pclMS = pclMS;
	m_csBaseText = csText;	   
	m_tdeType = tdeType;
}

//***************************************************************************
CPA_Editor_MiniStructureButton::~CPA_Editor_MiniStructureButton()
{
}

/////////////////////////////////////////////////////////////////////////////
// CPA_Editor_MiniStructureButton message handlers

//***************************************************************************
void CPA_Editor_MiniStructureButton::OnClicked() 
{
	switch ( m_tdeType )
	{
		case ECAR_MS_BUTTON_TYPE_ALLOC :
			m_pclMS->m_fn_vOnButtonAlloc();
			break;

		case ECAR_MS_BUTTON_TYPE_OPEN :
			m_pclMS->m_fn_vOnButtonOpen();
			break;
	}
	
	m_fn_vUpdateMSButton();
}

//***************************************************************************
void CPA_Editor_MiniStructureButton::m_fn_vUpdateMSButton() 
{
	switch ( m_tdeType )
	{
		case ECAR_MS_BUTTON_TYPE_ALLOC :
			if ( m_pclMS->m_pclActorMS != NULL )
			{
				//Update buttons for Models
				//--------------------------------------------
				if ( !(m_pclMS->m_pclActorMS->m_pclParentActor->m_fn_bIsAnInstance()) )
				{
					EnableWindow(TRUE);

					if ( m_pclMS->m_pclActorMS->m_fn_bIsAllocated() )
					{
						SetCheck(1);

						if ( m_pclMS->m_fn_bCanBeUnallocated() )
						{
							SetWindowText("Unalloc");
						}
						else
						{
							EnableWindow(FALSE);
							
							if ( m_pclMS->m_fn_tdeGetManiability() == MS_MANIABILITY_ALWAYS_ALLOCATED )
 								SetWindowText("Always");
						}
					}
					else
					{
						SetCheck(0);
						if ( m_pclMS->m_fn_bCanBeAllocated() )
						{
							SetWindowText("Alloc");
						}
						else
						{
							EnableWindow(FALSE);

							SetWindowText("Once");
						}
					}
				}
				//Update buttons for Instances	 
				//--------------------------------------------
				else
				{
	 				// Check if model & instance have the same state
					EdActors_EditorActorInstance *pInstance = (EdActors_EditorActorInstance *) m_pclMS->m_pclActorMS->m_pclParentActor;
					EdActors_EditorActor *pModel = pInstance->m_fn_pclGetModel()->m_fn_pclGetEditorActor();

					EdActors_ActorMiniStructure *pclModelMS = pModel->m_clListOfAMSLists.m_fn_pclGetAMSFromName(m_pclMS->m_pclActorMS->m_fn_csGetScriptName());

					if (m_pclMS->m_pclActorMS->m_fn_bIsAllocated())
					{
						SetCheck(1);
						if (m_pclMS->m_fn_bCanBeUnallocated())
						{
							if (m_pclMS->m_pclActorMS->m_fn_bIsAllocated() != pclModelMS->m_fn_bIsAllocated())
								SetWindowText("Unalloc");
							else
								SetWindowText("All.d");
						}
						else
						{
							if ( m_pclMS->m_fn_tdeGetManiability() == MS_MANIABILITY_ALLOCATED_ONLY_ONCE )
								SetWindowText("Once");
							else if ( m_pclMS->m_fn_tdeGetManiability() == MS_MANIABILITY_ALWAYS_ALLOCATED )
 								SetWindowText("Always");
						}
					}
					else
					{
						SetCheck(0);
						if (m_pclMS->m_pclActorMS->m_fn_bIsAllocated() != pclModelMS->m_fn_bIsAllocated())
							SetWindowText("Alloc");
						else
							SetWindowText("Unall.d");

						ERROR_ASSERT( m_pclMS->m_fn_bCanBeUnallocated() );
					}

					if (m_pclMS->m_pclActorMS->m_fn_bIsAllocated() != pclModelMS->m_fn_bIsAllocated())
						EnableWindow(TRUE);
					else
						EnableWindow(FALSE);
				}
			}
			else
	 		{
				SetWindowText("");
				SetCheck(0);
				EnableWindow(FALSE);
			}

			break;

		case ECAR_MS_BUTTON_TYPE_OPEN :
  		if ( m_pclMS->m_pclActorMS != NULL )
			{		   
				if ( m_pclMS->m_pclActorMS->m_fn_bIsAllocated() )
				{
					EnableWindow(TRUE);

					if ( m_pclMS->m_pclActorMS->m_fn_bIsOpen() )
						SetCheck(1);
					else
						SetCheck(0);
				}
				else
				{
					SetCheck(0);
					EnableWindow(FALSE);
				}
			}
			else
			{
				SetCheck(0);
				EnableWindow(FALSE);
			}

			break;
	}
}
