// DlgSelect.cpp : implementation file
//

#include "stdafx.h"

#include "ACP_Base.h"
#include "ITF.h"

#include "TAC.h"
#include "TFa.h"

#include "DlgSelect.hpp"

#include "..\Main\Inc\_EditID.h"
#include "TUT.h"

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

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// MACRO

#define M_LB()      ((CListBox*)GetDlgItem(IDC_LBOX))

/////////////////////////////////////////////////////////////////////////////
// CDlgSelection dialog


CDlgSelection::CDlgSelection(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSelection::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSelection)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    m_p_oFamily = NULL;
    m_oListOfSelectedObjects . RemoveAll();
}


void CDlgSelection::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSelection)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlgSelection, CDialog)
	//{{AFX_MSG_MAP(CDlgSelection)
    ON_WM_DESTROY()
	ON_WM_DRAWITEM()
	ON_LBN_DBLCLK(IDC_LBOX, OnDblclkLbox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CDlgSelection message handlers

void CDlgSelection::OnDestroy()
{
    // unregister controls
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_LBOX );
    TUT_M_vUnregisterControlID( IDOK     );
    TUT_M_vUnregisterControlID( IDCANCEL );
    //
    CDialog::OnDestroy();
}
void CDlgSelection::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
{
	// TODO: Add your message handler code here and/or call default
    if ( (nIDCtl == IDC_LBOX) && (M_LB()->IsWindowEnabled() ) && (lpDIS->itemID != LB_ERR) )
    {
        OnDrawItemListBox( lpDIS );
    }
    else
	    CDialog::OnDrawItem(nIDCtl, lpDIS);
}

BOOL CDlgSelection::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class

	return CDialog::PreTranslateMessage(pMsg);
}

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

    // register controls
    TUT_M_vGetTutDll();
    TUT_M_vRegisterControlID( IDC_LBOX, "TAC_SELSTATES_LIST",  TUT_e_ListBox);
    TUT_M_vRegisterControlID( IDOK,     "TAC_SELSTATES_OK",    TUT_e_Button );
    TUT_M_vRegisterControlID( IDCANCEL, "TAC_SELSTATES_CANCEL",TUT_e_Button );
    //

    if( m_p_oFamily )
        mfn_vFillListBox();

	// TODO: Add extra initialization here

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

void CDlgSelection::OnDblclkLbox()
{
    CListBox *pLB = M_LB();
    BOOL      bOutside;
    int       iIndex;

    // search item under cursor
    CPoint oPoint;

    GetCursorPos( &oPoint );
    pLB -> ScreenToClient( &oPoint );

    iIndex = pLB->ItemFromPoint(oPoint, bOutside ) ;
    if( ! bOutside && ( iIndex != LB_ERR ) )
    {
        BOOL bSelState          = (BOOL) pLB -> GetSel( iIndex ) ;
        CPA_State *p_oState     = (CPA_State*) pLB -> GetItemData( iIndex );
        CPA_Action *p_oAction   = p_oState -> mfn_p_oGetAction();
        int iBegin              = iIndex - 1;
        int iEnd                = iIndex + 1;

        while( (iBegin > 0) && ( ((CPA_State*) pLB -> GetItemData( iBegin ))-> mfn_p_oGetAction() == p_oAction ) )
        {
            iBegin--;
        }
        iBegin++;

        while( (iEnd < pLB -> GetCount() ) && ( ((CPA_State*) pLB -> GetItemData( iEnd ))-> mfn_p_oGetAction() == p_oAction ) )
        {
            iEnd++;
        }
        iEnd--;
        pLB -> SelItemRange( bSelState, iBegin, iEnd );
    }

}

void CDlgSelection::OnCancel()
{
    m_oListOfSelectedObjects . RemoveAll();
	CDialog::OnCancel();
}

void CDlgSelection::OnOK()
{
	// TODO: Add extra validation here
    mfn_vFillListWithSelectedObjects();
	CDialog::OnOK();
}

void CDlgSelection::OnDrawItemListBox(LPDRAWITEMSTRUCT lpDIS)
{
    CDC         *pDC      = CDC::FromHandle( lpDIS->hDC );
    BOOL        bSelected = (lpDIS->itemState & ODS_SELECTED );
    CPA_State   *p_oState = (CPA_State*)M_LB()->GetItemData( lpDIS->itemID );

    pDC->FillSolidRect( &lpDIS->rcItem, GetSysColor( COLOR_WINDOW ));

    pDC->SetBkMode( TRANSPARENT);
    pDC->SetTextColor( GetSysColor( COLOR_WINDOWTEXT ) );

    // display action name
    lpDIS->rcItem.left += 2;
    pDC->DrawText( p_oState -> mfn_p_oGetAction() -> GetName(), &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
    // display state name
    lpDIS->rcItem.right -= 2;
    pDC->DrawText( p_oState -> GetName(), &lpDIS->rcItem, DT_SINGLELINE | DT_RIGHT | DT_VCENTER );

    lpDIS->rcItem.left -= 2;
    lpDIS->rcItem.right += 2;

    if (bSelected)
        pDC->InvertRect( &lpDIS->rcItem );
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CDlgSelection owner methods
void CDlgSelection::mfn_vSetFamily(CPA_Family *_p_oFamily)
{
    m_p_oFamily = _p_oFamily;
}

void CDlgSelection::mfn_vFillListBox(void)
{
    CListBox *pLB = M_LB();
    POSITION xPosAct, xPosSta;

    pLB -> ResetContent();

    xPosAct = m_p_oFamily -> m_oListOfActions . GetHeadPosition();
    while( xPosAct )
    {
        CPA_Action *p_oAction = m_p_oFamily -> m_oListOfActions . GetNext( xPosAct );
        //
        xPosSta = p_oAction -> m_oListOfStates . GetHeadPosition();
        while( xPosSta )
        {
            CPA_State   *p_oState   = p_oAction -> m_oListOfStates . GetNext( xPosSta );
            int         iIndex      = pLB -> AddString( p_oAction -> GetName() + " - " + p_oState -> GetName() );

            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) p_oState );
        }
    }
}

void CDlgSelection::mfn_vFillListWithSelectedObjects(void)
{
    CListBox *pLB = M_LB();
    int      iNbSel = pLB -> GetSelCount();

    m_oListOfSelectedObjects . RemoveAll();

    if( iNbSel > 0 )
    {
        // get selected items
        int *a_iIdxSel = (int*) malloc( iNbSel * sizeof( int ) );
        pLB -> GetSelItems( iNbSel, a_iIdxSel );
        // fill list
        for( int i=0 ; i< iNbSel ; i++ )
        {
            CPA_BaseObject* p_oState = (CPA_BaseObject*) pLB -> GetItemData( a_iIdxSel[i] );
            m_oListOfSelectedObjects . AddTail( p_oState ); 
        }
        free( a_iIdxSel );
    }
}

