// DlgNmLst.cpp : implementation file
//

#include "stdafx.h"

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

#include "TAC.h"
#include "OAc.h"
#include "TFA.h"
#include "x:\cpa\main\inc\_editid.h"
#include "TUT.h"

#include "ACInterf.hpp"
#include "DlgNmLst.hpp"
#include "FAModif.hpp"

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

/*=============================================================================
 MACROS
=============================================================================*/
#define M_BT( nID )         ( (CButton*) GetDlgItem( nID ) )
#define M_BTChecked( nID )  (M_BT( nID ) -> GetCheck() )

#define M_BTZDm()           M_BT( IDC_RADIO_ZDM )
#define M_BTZDe()           M_BT( IDC_RADIO_ZDE )
#define M_BTZDd()           M_BT( IDC_RADIO_ZDD )
#define M_BTZDr()           M_BT( IDC_RADIO_ZDR )
#define M_BTEdit()          M_BT( IDC_BT_EDITSTATE )
#define M_BTZDeChecked()    M_BTChecked( IDC_RADIO_ZDE )
#define M_BTZDmChecked()    M_BTChecked( IDC_RADIO_ZDM )
#define M_BTZDdChecked()    M_BTChecked( IDC_RADIO_ZDD )
#define M_BTZDrChecked()    M_BTChecked( IDC_RADIO_ZDR )

#define M_LBNames()         ( (CListBox*) GetDlgItem( IDC_LB_NAMES ) )

#define M_GetClientRect( pWnd, poRect )    (pWnd) -> GetWindowRect( poRect );ScreenToClient( poRect )
#define M_GetClientRectId( Id, poRect )    M_GetClientRect( GetDlgItem( Id ), poRect )
#define M_SetClientRect( pWnd, poRect )    (pWnd) -> MoveWindow( poRect )
#define M_SetClientRectId( Id, poRect )    M_SetClientRect( GetDlgItem( Id ), poRect )

#define M_MakeDo( p_oModif )                m_p_oDLL -> M_GetEditManager() -> AskFor( p_oModif )

#define M_GetCurrentNameList()              m_p_oDLL -> mfn_p_oGetCurrentNamesList()

/*=============================================================================
 CONSTANTS
=============================================================================*/
#define C_iBorderSize   2
/////////////////////////////////////////////////////////////////////////////
// CDlgNameList

IMPLEMENT_DYNCREATE(CDlgNameList, CFormView)

CDlgNameList::CDlgNameList()
	: CFormView(CDlgNameList::IDD)
{
	//{{AFX_DATA_INIT(CDlgNameList)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bFirstShow = TRUE;
    m_p_oCurrentFamily = NULL;
}

CDlgNameList::~CDlgNameList()
{
}

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

BOOL CDlgNameList::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
	DWORD dwRequestedStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
	CCreateContext* pContext)
{
    BOOL bResult = CFormView::Create( lpszClassName, lpszWindowName, dwRequestedStyle, rect, pParentWnd, nID, pContext );
    // register controls
    TUT_M_vGetTutDll();
    TUT_M_vRegisterControlID( IDC_RADIO_ZDD, "TAC_ZAM_NAMESLIST_ZDD", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_RADIO_ZDE, "TAC_ZAM_NAMESLIST_ZDE", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_RADIO_ZDM, "TAC_ZAM_NAMESLIST_ZDM", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_RADIO_ZDR, "TAC_ZAM_NAMESLIST_ZDR", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_LB_NAMES, "TAC_ZAM_NAMESLIST_LIST", TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDC_BT_EDITSTATE, "TAC_ZAM_NAMESLIST_SWAP", TUT_e_Button );
    //
    return bResult;
}

BEGIN_MESSAGE_MAP(CDlgNameList, CFormView)
	//{{AFX_MSG_MAP(CDlgNameList)
	ON_BN_CLICKED(IDC_RADIO_ZDD, OnRadioZdx)
	ON_BN_CLICKED(IDC_BT_EDITSTATE, OnBtEditstate)
	ON_LBN_SELCHANGE(IDC_LB_NAMES, OnSelchangeLbNames)
	ON_BN_CLICKED(IDC_RADIO_ZDE, OnRadioZdx)
	ON_BN_CLICKED(IDC_RADIO_ZDM, OnRadioZdx)
    ON_BN_CLICKED(IDC_RADIO_ZDR, OnRadioZdx)
	ON_WM_SHOWWINDOW()
	ON_WM_SIZE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgNameList diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgNameList message handlers

void CDlgNameList::OnDestroy()
{
    // register controls
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_RADIO_ZDD );
    TUT_M_vUnregisterControlID( IDC_RADIO_ZDE );
    TUT_M_vUnregisterControlID( IDC_RADIO_ZDM );
    TUT_M_vUnregisterControlID( IDC_RADIO_ZDR );
    TUT_M_vUnregisterControlID( IDC_LB_NAMES );
    TUT_M_vUnregisterControlID( IDC_BT_EDITSTATE );
    //
	CFormView::OnDestroy();
}

// ----------------------------------------------------------------------------
// Description : handle function for message WM_SHOWWINDOW
// ----------------------------------------------------------------------------
void CDlgNameList::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CFormView::OnShowWindow(bShow, nStatus);

    if(bShow && m_bFirstShow)
    {
        M_BTZDm() -> SetCheck( 1 ); // active ZDm Type
    }
    m_bFirstShow = FALSE;
}

// ----------------------------------------------------------------------------
// Description : handle function for message WM_SIZE
// ----------------------------------------------------------------------------
void CDlgNameList::OnSize(UINT nType, int cx, int cy)
{
    if(GetDlgItem(IDC_LB_NAMES))
    {
        CRect oRectDlg, oRectList, oRectBTSwap, oRectBTZDe, oRectBTZDd, oRectBTZDm, oRectBTZDr;
        int iSize,iSpace;

        // get pos
        M_GetClientRect( this, &oRectDlg );
        M_GetClientRectId( IDC_BT_EDITSTATE, &oRectBTSwap );
        M_GetClientRectId( IDC_RADIO_ZDE, &oRectBTZDe );
        M_GetClientRectId( IDC_RADIO_ZDD, &oRectBTZDd );
        M_GetClientRectId( IDC_RADIO_ZDM, &oRectBTZDm );
        M_GetClientRectId( IDC_RADIO_ZDR, &oRectBTZDr );
        M_GetClientRectId( IDC_LB_NAMES, &oRectList );

        iSize = oRectBTZDe . Width();
        iSpace = (oRectDlg . Width() - (iSize<<1) - (C_iBorderSize<<1)) / 3;
        // Left Align
        oRectBTSwap . left = oRectList . left = oRectDlg . left + C_iBorderSize;
        oRectBTZDd . left = oRectBTZDm . left = oRectDlg . left + C_iBorderSize + iSpace;
        oRectBTZDe . left = oRectBTZDr . left = oRectBTZDd . left + iSize + iSpace;
        // Right Align
        oRectBTSwap . right = oRectBTZDm . right = oRectList . right = oRectDlg . right - C_iBorderSize;
        // Bottom Align
        oRectList . bottom = max( oRectList . top, oRectDlg . bottom - C_iBorderSize );

        // BTs
        oRectBTZDd . right = oRectBTZDd . left + iSize;
        oRectBTZDm . right = oRectBTZDm . left + iSize;
        oRectBTZDe . right = oRectBTZDe . left + iSize;
        oRectBTZDr . right = oRectBTZDr . left + iSize;

        // set pos
        M_SetClientRectId( IDC_BT_EDITSTATE, &oRectBTSwap );
        M_SetClientRectId( IDC_RADIO_ZDE, &oRectBTZDe );
        M_SetClientRectId( IDC_RADIO_ZDD, &oRectBTZDd );
        M_SetClientRectId( IDC_RADIO_ZDM, &oRectBTZDm );
        M_SetClientRectId( IDC_RADIO_ZDR, &oRectBTZDr );
        M_SetClientRectId( IDC_LB_NAMES, &oRectList );
    }
}

// ----------------------------------------------------------------------------
// Description : handle function for message BN_CLICKED on Radio button
// ----------------------------------------------------------------------------
void CDlgNameList::OnRadioZdx()
{
    CListBox *pLB = M_LBNames();
	mfn_vFillLBNames( m_p_oCurrentFamily, mfn_vGetZoneType() );
    if( pLB -> GetCount() > 0 )
        pLB -> SetCurSel( 0 );
    else
        pLB -> SetCurSel( -1 );
    M_LBNames() -> Invalidate();
    OnSelchangeLbNames();
}

// ----------------------------------------------------------------------------
// Description : handle function for message BN_CLICKED on Edit Button
// ----------------------------------------------------------------------------
void CDlgNameList::OnBtEditstate()
{
    Family_StateChange *p_oNLChange = new Family_StateChange( m_p_oDLL, C_eMode, (void*)TRUE, (void*)FALSE );
    M_MakeDo( p_oNLChange );
}

// ----------------------------------------------------------------------------
// Description : handle function for message LBN_SELCHANGE on ListBox
// ----------------------------------------------------------------------------
void CDlgNameList::OnSelchangeLbNames()
{
    CListBox *pLB = M_LBNames();
    int iIndex = pLB -> GetCurSel();
    CPA_tdoNameList *p_oNameList = NULL;

    if( iIndex != LB_ERR )
        p_oNameList = (CPA_tdoNameList*) pLB -> GetItemData( iIndex );

    Family_StateChange *p_oNLChange = new Family_StateChange( m_p_oDLL, C_eNameList, p_oNameList, M_GetCurrentNameList() );
    M_MakeDo( p_oNLChange );
}

// ----------------------------------------------------------------------------
// Description : Set Current Family
// ----------------------------------------------------------------------------
void CDlgNameList::mfn_vSetFamily(CPA_Family * _p_oFamily)
{
    CListBox        *pLB         = M_LBNames();
    CPA_tdoNameList *p_oNameList = NULL;

    mfn_vFillLBNames( _p_oFamily, mfn_vGetZoneType() );

    GetDlgItem( IDC_RADIO_ZDD ) -> EnableWindow( _p_oFamily != NULL );
    GetDlgItem( IDC_RADIO_ZDM ) -> EnableWindow( _p_oFamily != NULL );
    GetDlgItem( IDC_RADIO_ZDE ) -> EnableWindow( _p_oFamily != NULL );
    GetDlgItem( IDC_RADIO_ZDR ) -> EnableWindow( _p_oFamily != NULL );

    m_p_oCurrentFamily = _p_oFamily;

    // select first item
    if( pLB -> GetCount() > 0 )
        m_p_oDLL -> mfn_vSetCurrentNameList( (CPA_tdoNameList*) pLB -> GetItemData( 0 ) );
    else
        m_p_oDLL -> mfn_vSetCurrentNameList( NULL );
}

// ----------------------------------------------------------------------------
// Description : Get Zone Type
// ----------------------------------------------------------------------------
void CDlgNameList::mfn_vSetCurrentNamesList(CPA_tdoNameList *_p_oNamesList)
{
    CListBox *pLB = M_LBNames();

    pLB -> SetCurSel( -1 );

    if( _p_oNamesList )
    {
        int             iIndex  = -1;
        tde_ZoneType    eType   = C_eError;

        if( _p_oNamesList -> fn_bIsOfType( C_szZDeNameListType ) )
            eType = C_eZDe;
        else if( _p_oNamesList -> fn_bIsOfType( C_szZDmNameListType ) )
            eType = C_eZDm;
        else if( _p_oNamesList -> fn_bIsOfType( C_szZDdNameListType ) )
            eType = C_eZDd;
        else if( _p_oNamesList -> fn_bIsOfType( C_szZDrNameListType ) )
            eType = C_eZDr;

        mfn_vSetZoneType( eType );
        mfn_vFillLBNames( (CPA_Family*)_p_oNamesList -> GetOwner(), eType );

        // select
        iIndex = pLB -> FindStringExact( -1, _p_oNamesList -> GetName() );
        if( iIndex != LB_ERR )
            pLB -> SetCurSel( iIndex );
    }
}

// ----------------------------------------------------------------------------
// Description : Get Zone Type
// ----------------------------------------------------------------------------
tde_ZoneType CDlgNameList::mfn_vGetZoneType()
{
    if( M_BTZDeChecked() )
        return C_eZDe;
    else if( M_BTZDmChecked() )
        return C_eZDm;
    else if( M_BTZDdChecked() )
        return C_eZDd;
    else if( M_BTZDrChecked() )
        return C_eZDr;
    else
        return C_eError;
}

// ----------------------------------------------------------------------------
// Description : Fill ListBox
// ----------------------------------------------------------------------------
void CDlgNameList::mfn_vSetZoneType(tde_ZoneType _eType)
{
    M_BTZDm() -> SetCheck( _eType == C_eZDm );
    M_BTZDd() -> SetCheck( _eType == C_eZDd );
    M_BTZDe() -> SetCheck( _eType == C_eZDe );
    M_BTZDr() -> SetCheck( _eType == C_eZDr );
}

// ----------------------------------------------------------------------------
// Description : Fill ListBox
// ----------------------------------------------------------------------------
void CDlgNameList::mfn_vFillLBNames(CPA_Family * _p_oFamily, tde_ZoneType _eZone)
{
    CListBox *pLB = M_LBNames();

    pLB -> ResetContent();

    if( _p_oFamily )
    {
        CPA_List<CPA_BaseObject> oList;

        switch( _eZone )
        {
        case C_eZDe:
            _p_oFamily -> GetMainWorld() -> fn_lFindObjects( &oList, "", C_szZDeNameListType, _p_oFamily);
            break;
        case C_eZDm:
            _p_oFamily -> GetMainWorld() -> fn_lFindObjects( &oList, "", C_szZDmNameListType, _p_oFamily);
            break;
        case C_eZDd:
            _p_oFamily -> GetMainWorld() -> fn_lFindObjects( &oList, "", C_szZDdNameListType, _p_oFamily);
            break;
        case C_eZDr:
            _p_oFamily -> GetMainWorld() -> fn_lFindObjects( &oList, "", C_szZDrNameListType, _p_oFamily);
            break;
        case C_eError:
            break;
        }

        POSITION xPos = oList . GetHeadPosition();
        while( xPos )
        {
            CPA_tdoNameList *p_oNameList = (CPA_tdoNameList*) oList . GetNext( xPos );
            int iIndex = pLB -> AddString( p_oNameList -> GetName() );
            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) p_oNameList );
        }
    }
}