// edirsele.cpp : implementation file
//

#include "stdafx.h"
#include "EdIRRes.h"
#include "EdIRSele.hpp"
#include "EdIRFrm.hpp"
#include "EdIRIRD.hpp"
#include "EdIRBeEn.hpp"


#include "EdIRIAGl.hpp"

#include "ai_intf.hpp"

#include "_Actors.hpp"


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

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_Select_IAObj dialog


CPA_EdIR_Select_IAObj::CPA_EdIR_Select_IAObj(CWnd* pParent /*=NULL*/, BOOL bPShow /*=FALSE*/, CString csIncomplete /*=""*/)
	: CDialog(CPA_EdIR_Select_IAObj::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPA_EdIR_Select_IAObj)
	//}}AFX_DATA_INIT
    m_bParentsShow = bPShow;
    m_csIncompleteWord = csIncomplete;
	m_pclTypesList = fn_pclGetListIATypeObjects();
	m_pclParentsList = new CStringList();
	m_pclDataList = new CStringList();
	CPA_BaseObject *p = g_pclAIInterface->m_pclIRMainFrame->m_pclBrain->m_fn_pclGetModel();
	ASSERT( p && p->GetOwner() );
	m_csCurrentModel = p->GetName();
	m_csCurrentFamily = p->GetOwner()->GetName();
	m_csCurrentParent = "";
	m_csCurrentType = "";
}


void CPA_EdIR_Select_IAObj::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_EdIR_Select_IAObj)
	DDX_Control(pDX, IDC_LIST_TYPES, m_clCListTypes);
	DDX_Control(pDX, IDC_LIST_PARENTS, m_clCListParents);
	DDX_Control(pDX, IDC_LIST_DATA, m_clCListData);
	DDX_Control(pDX, IDC_BUTTON_SHOW_HIDE, m_cbCButton);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPA_EdIR_Select_IAObj, CDialog)
	//{{AFX_MSG_MAP(CPA_EdIR_Select_IAObj)
	ON_BN_CLICKED(IDC_BUTTON_SHOW_HIDE, OnButtonShowHide)
	ON_NOTIFY(NM_CLICK, IDC_LIST_TYPES, OnClickListTypes)
	ON_NOTIFY(NM_CLICK, IDC_LIST_PARENTS, OnClickListParents)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_DATA, OnDblclkListData)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_Select_IAObj message handlers

void CPA_EdIR_Select_IAObj::OnButtonShowHide() 
{
	m_bParentsShow = !m_bParentsShow;  
	m_fn_vToggleParents();
}

BOOL CPA_EdIR_Select_IAObj::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
    m_clCListTypes.GetWindowRect( &m_crCRectTypes1 );
    ScreenToClient( &m_crCRectTypes1 );

    m_clCListData.GetWindowRect( &m_crCRectData1 );
	ScreenToClient( &m_crCRectData1 );

	CRect CRectParents;
    m_clCListParents.GetWindowRect( &CRectParents );

    m_crCRectTypes2 = m_crCRectTypes1;
    m_crCRectData2  = m_crCRectData1;
    m_crCRectTypes2.DeflateRect( 0, 0, ( CRectParents.Width()/2 ), 0 );
	m_crCRectData2.DeflateRect( ( CRectParents.Width()/2 ), 0, 0, 0 );


	m_clCListTypes.InsertColumn( 0, "Object type", LVCFMT_LEFT, 300,0 );
    POSITION pos=m_pclTypesList->GetHeadPosition();

	int ipos = 0;
	while(pos)
	    m_clCListTypes.InsertItem(ipos++, m_pclTypesList->GetNext(pos));

    m_clCListParents.InsertColumn( 0, "Object name", LVCFMT_LEFT, 300, 0 );
	//m_clCListParents.InsertColumn( 1, "Object type", LVCFMT_LEFT, CRectParents.Width()/2, 1 );

	m_clCListData.InsertColumn( 0, "Object name", LVCFMT_LEFT, 300, 0 );

    m_fn_vToggleParents();

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

void CPA_EdIR_Select_IAObj::m_fn_vToggleParents()
{
	if( m_bParentsShow )
	{ m_clCListParents.ShowWindow( SW_SHOW );
	  m_clCListTypes.MoveWindow( m_crCRectTypes2, TRUE );
	  m_clCListData.MoveWindow( m_crCRectData2, TRUE );
	  m_cbCButton.SetWindowText( "&Hide parents" );
	  OnTypeSelection();
	  m_clCListParents.SetFocus();
	}
	else
	{ m_clCListParents.ShowWindow( SW_HIDE );
	  m_clCListTypes.MoveWindow( m_crCRectTypes1, TRUE );
	  m_clCListData.MoveWindow( m_crCRectData1, TRUE );
	  m_cbCButton.SetWindowText( "&Show parents" );
	  OnTypeSelection();
	  m_clCListTypes.SetFocus();
	}
    
}

CPA_EdIR_Select_IAObj::~CPA_EdIR_Select_IAObj()
{
   delete m_pclTypesList;
   delete m_pclParentsList;
   delete m_pclDataList;
}

void CPA_EdIR_Select_IAObj::OnTypeSelection( )
{
   int SelectedItem = -1;

   for( int i = 0; i < m_clCListTypes.GetItemCount(); i++ )
	  if( m_clCListTypes.GetItemState( i, LVIS_SELECTED ) )
	  {	
		  SelectedItem = i;
		  break;
	  }
   if( SelectedItem == -1 ) 
	   return;
   m_csCurrentType = m_clCListTypes.GetItemText( SelectedItem, 0);
   
   m_pclParentsList->RemoveAll();
   m_pclDataList->RemoveAll();

   fn_vAddListOfIAObjectsForType( m_pclParentsList, m_pclDataList, "", m_csCurrentType );
   
   if( m_bParentsShow )
   { 
	   m_fn_vRefreshListParents( );
	   if( m_fn_bTrySelectModelOrFamily() )
	   {
		   OnParentsSelection();
		   m_clCListParents.SetFocus( );
	   }
	   else
		   m_fn_vRefreshListData( );
	   
   }
   else
	   m_fn_vRefreshListData( );

}

void CPA_EdIR_Select_IAObj::OnClickListTypes(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	OnTypeSelection( );
	
	*pResult = 0;
}


BOOL CPA_EdIR_Select_IAObj::m_fn_bCheckComplete( CString csName )
{
	int iIndex = csName.Find( m_csIncompleteWord );
	if( iIndex )
		return FALSE;
	return TRUE;
}

void CPA_EdIR_Select_IAObj::OnParentsSelection()
{
	int SelectedItem = -1;

	for( int i = 0; i < m_clCListParents.GetItemCount(); i++ )
		if( m_clCListParents.GetItemState( i, LVIS_SELECTED ) )
		{
			SelectedItem = i;
			break;
		}
		if( SelectedItem == -1 ) 
			return;
	m_csCurrentParent = m_clCListParents.GetItemText( SelectedItem, 0 );
	m_pclDataList->RemoveAll();
	fn_vAddListOfIAObjectsForType( m_pclParentsList, m_pclDataList, m_csCurrentParent, m_csCurrentType );
	m_fn_vRefreshListData( );
		
}

void CPA_EdIR_Select_IAObj::m_fn_vRefreshListData( )
{
   int ipos = 0;
   POSITION pos;
   CString csString;

   m_clCListData.DeleteAllItems();

   pos = m_pclDataList->GetHeadPosition();
   while(pos)
   {
	   csString = m_pclDataList->GetNext( pos );
	   if( m_fn_bCheckComplete( csString ) )
		   m_clCListData.InsertItem( ipos++, csString );
   }
   
   m_clCListData.UpdateWindow();
}

void CPA_EdIR_Select_IAObj::m_fn_vRefreshListParents( )
{
   int ipos = 0;
   POSITION pos;
   m_clCListParents.DeleteAllItems();

   pos = m_pclParentsList->GetHeadPosition();
   while(pos)
   	   m_clCListParents.InsertItem( ipos++, m_pclParentsList->GetNext( pos ) );
   
   m_clCListParents.UpdateWindow();
}

void CPA_EdIR_Select_IAObj::OnClickListParents(NMHDR* pNMHDR, LRESULT* pResult) 
{
	OnParentsSelection();
	
	*pResult = 0;
}

BOOL CPA_EdIR_Select_IAObj::m_fn_bTrySelectModelOrFamily()
{
	LV_FINDINFO lvInfo;
	int iIndex;

	lvInfo.flags = LVFI_STRING;
	lvInfo.psz = m_csCurrentParent;

	iIndex = m_clCListParents.FindItem( &lvInfo );
	if( iIndex >=0 )
	{
		m_clCListParents.SetItemState( iIndex, LVIS_SELECTED, LVIS_SELECTED );
		m_clCListParents.EnsureVisible( iIndex, FALSE );
		return TRUE;
	}
	
	lvInfo.psz = m_csCurrentFamily;
	iIndex = m_clCListParents.FindItem( &lvInfo );
	if( iIndex >=0 )
	{
		m_clCListParents.SetItemState( iIndex, LVIS_SELECTED, LVIS_SELECTED );
		m_clCListParents.EnsureVisible( iIndex, FALSE );
		return TRUE;
	}

	lvInfo.psz = m_csCurrentModel;
	iIndex = m_clCListParents.FindItem( &lvInfo );
	if( iIndex >=0 )
	{
		m_clCListParents.SetItemState( iIndex, LVIS_SELECTED, LVIS_SELECTED );
		m_clCListParents.EnsureVisible( iIndex, FALSE );
		return TRUE;
	}
	
	return FALSE;
}

void CPA_EdIR_Select_IAObj::OnDblclkListData(NMHDR* pNMHDR, LRESULT* pResult) 
{
	OnDataSelection();
	
	*pResult = 0;
}


void CPA_EdIR_Select_IAObj::OnOK()
{
	if( GetFocus() == &m_clCListData )
		OnDataSelection();
}

void CPA_EdIR_Select_IAObj::OnDataSelection()
{
	int SelectedItem = -1;

	for( int i = 0; i < m_clCListData.GetItemCount(); i++ )
		if( m_clCListData.GetItemState( i, LVIS_SELECTED ) )
		{
			SelectedItem = i;
			break;
		}
		if( SelectedItem == -1 ) 
			return;
	
	if ( ( GetKeyState(VK_CONTROL) & (1<<15) ) && m_fn_bTrySelectModelOrFamily() && m_csIncompleteWord.IsEmpty() )
		m_csRetWord = m_csCurrentParent + "@";
	else
		m_csRetWord = "";

	m_csRetWord = m_csRetWord + m_clCListData.GetItemText( SelectedItem, 0 );

	CDialog::OnOK();
}



BOOL CPA_EdIR_Select_IAObj::OnCommand(WPARAM wParam, LPARAM lParam) 
{

	if( wParam == ID_IR_LIST_REFRESH )
	{
		if( ::GetDlgItem( m_hWnd, IDC_LIST_TYPES ) == (HWND)lParam )
			OnTypeSelection();
		else
			if( ::GetDlgItem( m_hWnd, IDC_LIST_PARENTS ) == (HWND)lParam )
				OnParentsSelection();
		return TRUE;
	}
	return CDialog::OnCommand(wParam, lParam);
}
