// DlgZAL.cpp : implementation file
//

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

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

#include "ACInterf.hpp"

#include "DlgZAL.hpp"
#include "DlgInput.hpp"
#include "ZAModif.hpp"
#include "FAModif.hpp"

#include "X:\Cpa\Main\inc\_EditID.h"
#include "TUT.h"

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

///////////////////////////////////////////////////////////////////////////////
// MACROS
///////////////////////////////////////////////////////////////////////////////
#define M_LBZAList()              ( (CListBox*) GetDlgItem( IDC_LB_ZALIST ) )

#define M_Enable( nID, bEnable )    GetDlgItem( nID ) -> EnableWindow( bEnable )
#define M_bEnabled( nID )           GetDlgItem( nID ) -> IsWindowEnabled()

#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 )

///////////////////////////////////////////////////////////////////////////////
// CONSTANTS
///////////////////////////////////////////////////////////////////////////////
#define C_uiPMFirst     2000
#define C_uiPMNew       (C_uiPMFirst+1)
#define C_uiPMCopy      (C_uiPMFirst+2)
#define C_uiPMRename    (C_uiPMFirst+3)
#define C_uiPMDelete    (C_uiPMFirst+4)

#define C_iBorderSize       4

/////////////////////////////////////////////////////////////////////////////
// CDlgZAL

IMPLEMENT_DYNCREATE(CDlgZAL, CFormView)

CDlgZAL::CDlgZAL()
	: CFormView(CDlgZAL::IDD)
{
	//{{AFX_DATA_INIT(CDlgZAL)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    m_p_oDLL                = NULL;
    m_p_oCurrentNamesList   = NULL;
    m_p_oCurrentZAList      = NULL;
    m_bFirstShow            = TRUE;
}

CDlgZAL::~CDlgZAL()
{
}

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

BOOL CDlgZAL::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_LB_ZALIST, "TAC_ZAM_ZALIST_LIST", TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDC_BT_NEW, "TAC_ZAM_ZALIST_BTNEW", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_RENAME, "TAC_ZAM_ZALIST_BTRENAME", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_COPY, "TAC_ZAM_ZALIST_BTCOPY", TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BT_DELETE, "TAC_ZAM_ZALIST_BTDELETE", TUT_e_Button );
    //
    return bResult;
}

BEGIN_MESSAGE_MAP(CDlgZAL, CFormView)
	//{{AFX_MSG_MAP(CDlgZAL)
    ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BT_COPY, OnBtCopy)
	ON_BN_CLICKED(IDC_BT_DELETE, OnBtDelete)
	ON_BN_CLICKED(IDC_BT_NEW, OnBtNew)
	ON_BN_CLICKED(IDC_BT_RENAME, OnBtRename)
	ON_LBN_SELCHANGE(IDC_LB_ZALIST, OnSelchangeLbZalist)
	ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgZAL diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgZAL message handlers

void CDlgZAL::OnDestroy()
{
    // register controls
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_LB_ZALIST );
    TUT_M_vUnregisterControlID( IDC_BT_NEW );
    TUT_M_vUnregisterControlID( IDC_BT_RENAME );
    TUT_M_vUnregisterControlID( IDC_BT_COPY );
    TUT_M_vUnregisterControlID( IDC_BT_DELETE );
    //
    CFormView::OnDestroy();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CFormView::OnShowWindow(bShow, nStatus);
    /*
    if( m_bFirstShow && bShow )
    {
        mfn_vSetCurrentNameList( NULL );
    }
    m_bFirstShow = FALSE;
    */
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnSize(UINT nType, int cx, int cy)
{
    if( M_LBZAList() )
    {
        CRect oRectDlg, oRectBTNew, oRectBTCopy, oRectBTRename, oRectBTDelete, oRectLB;
        int iWidth;

        // Get Pos
        M_GetClientRect( this, &oRectDlg );
        M_GetClientRectId( IDC_BT_NEW, &oRectBTNew );
        M_GetClientRectId( IDC_BT_COPY, &oRectBTCopy );
        M_GetClientRectId( IDC_BT_RENAME, &oRectBTRename );
        M_GetClientRectId( IDC_BT_DELETE, &oRectBTDelete );
        M_GetClientRectId( IDC_LB_ZALIST, &oRectLB );

        // BTs
        oRectBTNew . left = oRectBTRename . left = oRectDlg . left + C_iBorderSize;
        oRectBTCopy . right = oRectBTDelete . right = oRectDlg . right - C_iBorderSize;
        iWidth = ((oRectDlg . Width() - (C_iBorderSize << 1) ) >> 1);
        oRectBTNew . right = oRectBTRename . right = oRectBTNew . left + iWidth;
        oRectBTCopy . left = oRectBTDelete . left = oRectBTCopy . right - iWidth;

        // LB
        oRectLB . left = oRectDlg . left + C_iBorderSize;
        oRectLB . right = oRectDlg . right - C_iBorderSize;
        oRectLB . bottom = oRectDlg . bottom - C_iBorderSize;

        // Set Pos
        M_SetClientRectId( IDC_BT_NEW, &oRectBTNew );
        M_SetClientRectId( IDC_BT_COPY, &oRectBTCopy );
        M_SetClientRectId( IDC_BT_RENAME, &oRectBTRename );
        M_SetClientRectId( IDC_BT_DELETE, &oRectBTDelete );
        M_SetClientRectId( IDC_LB_ZALIST, &oRectLB );
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
BOOL CDlgZAL::PreTranslateMessage(MSG* pMsg)
{
    if( (pMsg->hwnd == GetDlgItem(IDC_LB_ZALIST)->m_hWnd) && (pMsg->message == WM_RBUTTONDOWN) )
    {
        // create and display the context popmenu
        CMenu oPopMenu;
        POINT xPos;

        xPos.x = LOWORD(pMsg->lParam);
        xPos.y = HIWORD(pMsg->lParam);
        // create
        oPopMenu . CreatePopupMenu () ;
        // fill
        oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_NEW ) ? MF_ENABLED : MF_GRAYED ), C_uiPMNew, "New");
        oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_COPY ) ? MF_ENABLED : MF_GRAYED ), C_uiPMCopy, "Copy");
        oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_RENAME ) ? MF_ENABLED : MF_GRAYED ), C_uiPMRename, "Rename");
        oPopMenu . AppendMenu( MF_STRING + ( M_bEnabled( IDC_BT_DELETE ) ? MF_ENABLED : MF_GRAYED ), C_uiPMDelete, "Delete");
        // display
        M_LBZAList() -> ClientToScreen(&xPos);
	    TUT_M_vGetTutDll    ();
		TUT_M_vRegisterMenu (this -> m_hWnd , oPopMenu . m_hMenu , xPos . x , xPos . y);
        oPopMenu . TrackPopupMenu ( TPM_LEFTALIGN|TPM_RIGHTBUTTON, xPos.x, xPos.y, this);
        oPopMenu . DestroyMenu ();
    }
	return CFormView::PreTranslateMessage(pMsg);
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
BOOL CDlgZAL::OnCommand(WPARAM wParam, LPARAM lParam)
{
	switch(LOWORD(wParam))
    {
    case C_uiPMNew:     OnBtNew();      break;
    case C_uiPMRename:  OnBtRename();   break;
    case C_uiPMCopy:    OnBtCopy();     break;
    case C_uiPMDelete:  OnBtDelete();   break;
    default :
    	return CFormView::OnCommand(wParam, lParam);
    }

    return TRUE;
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnBtCopy()
{
    if( m_p_oCurrentZAList )
    {
	    ZAList_Copy *p_oZAListCopy = new ZAList_Copy( m_p_oDLL, m_p_oCurrentZAList );
	    M_MakeDo( p_oZAListCopy );
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnBtDelete()
{
    if( m_p_oCurrentZAList )
    {
        ZAList_Delete *p_oZAListDelete = new ZAList_Delete( m_p_oDLL, m_p_oCurrentZAList );
        M_MakeDo( p_oZAListDelete );
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnBtNew()
{
    HINSTANCE hOldInst = AfxGetResourceHandle();
    AfxSetResourceHandle( m_p_oDLL -> GetDLLIdentity() -> hModule );

    CDlgInputText *p_oDlgInput = new CDlgInputText();
    CRect oRect;

    M_LBZAList() -> GetWindowRect( &oRect );
    CPoint oPoint( oRect.left, oRect.top );

    p_oDlgInput -> SetTitle( "Create new list" );
    p_oDlgInput -> SetMessage( "New List Name :" );
    p_oDlgInput -> SetPos( &oPoint );
	p_oDlgInput -> SetDefaultValue( C_szZAListTypeName );
	p_oDlgInput -> SetSelect( TRUE );
    if( p_oDlgInput -> DoModal() == IDOK )
    {
		ZAList_New *p_oNewZAList = new ZAList_New( m_p_oDLL, m_p_oCurrentNamesList, p_oDlgInput -> GetInputText() );
	    M_MakeDo( p_oNewZAList ) ;
    }
    delete p_oDlgInput;
    AfxSetResourceHandle( hOldInst );
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnBtRename()
{
    if( m_p_oCurrentZAList )
    {
        HINSTANCE hOldInst = AfxGetResourceHandle();
        AfxSetResourceHandle( m_p_oDLL -> GetDLLIdentity() -> hModule );

        CDlgInputText *p_oDlgInput = new CDlgInputText();
        CRect oRect;
        char szTitle[200];
		char *szZAListName = m_p_oCurrentZAList -> fn_p_szGetName();

        M_LBZAList() -> GetWindowRect( &oRect );
        CPoint oPoint( oRect.left, oRect.top );
        sprintf( szTitle, "Rename List \"%s\"", szZAListName );
        p_oDlgInput -> SetTitle( szTitle );
        p_oDlgInput -> SetMessage( "New Name :" );
        p_oDlgInput -> SetPos( &oPoint );
		p_oDlgInput -> SetDefaultValue( szZAListName );
		p_oDlgInput -> SetSelect( TRUE );
        if( p_oDlgInput -> DoModal() == IDOK )
        {
	        ZAList_Rename *p_oZAListRename = new ZAList_Rename( m_p_oDLL, m_p_oCurrentZAList, p_oDlgInput->GetInputText() );
	        M_MakeDo( p_oZAListRename );
        }
        delete p_oDlgInput;
        AfxSetResourceHandle( hOldInst );
    }
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::OnSelchangeLbZalist()
{
    CListBox *pLB = M_LBZAList();
    int iIndex = pLB -> GetCurSel();

    if( iIndex != LB_ERR )
    {
        Family_StateChange *p_oZAListChange = new Family_StateChange( m_p_oDLL, C_eZAList, (CPA_ZonesActivatingList*) pLB -> GetItemData( iIndex ), m_p_oCurrentZAList );
        M_MakeDo( p_oZAListChange );
    }
    else
        m_p_oCurrentZAList = NULL;

    mfn_vUpdateButtons();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::mfn_vSetCurrentNameList(CPA_tdoNameList * _p_oNameList)
{
    CListBox *pLB = M_LBZAList();

    m_p_oCurrentNamesList = _p_oNameList;
    mfn_vFillLBZAList( m_p_oCurrentNamesList );
    // select first item
    if( pLB -> GetCount() > 0 )
        m_p_oDLL -> mfn_vSetCurrentZAList( (CPA_ZonesActivatingList*) pLB -> GetItemData( 0 ) );
    else
        m_p_oDLL -> mfn_vSetCurrentZAList( NULL );
    /*MM
    mfn_vSetCurrentZAList( NULL );
    mfn_vUpdateButtons();
     MM*/
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::mfn_vSetCurrentZAList(CPA_ZonesActivatingList * _p_oZAList)
{
    if( _p_oZAList )
    {
        m_p_oCurrentNamesList = (CPA_tdoNameList *) _p_oZAList -> GetOwner();
        mfn_vFillLBZAList( m_p_oCurrentNamesList );
        mfn_iSelectStringExact( M_LBZAList(), _p_oZAList -> GetName() );
        m_p_oCurrentZAList = _p_oZAList;
    }
    else
    {
        M_LBZAList() -> SetCurSel( -1 );
        m_p_oCurrentZAList = NULL;
    }
    mfn_vUpdateButtons();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::mfn_vFillLBZAList(CPA_tdoNameList * _p_oNameList)
{
    CListBox *pLB = M_LBZAList();

    pLB -> ResetContent();

    if ( _p_oNameList )
    {
        // get list
        CPA_List<CPA_BaseObject> oList;
        m_p_oDLL -> fn_lFindObjects (&oList, "", C_szZAListTypeName, (CPA_BaseObject*)_p_oNameList );
        // fill comboBox
        POSITION xPos = oList . GetHeadPosition();
        while ( xPos )
        {
            CPA_ZonesActivatingList *p_oZAList = (CPA_ZonesActivatingList*) oList . GetNext( xPos );
            int iIndex = pLB -> AddString( p_oZAList -> GetName() );
            if( iIndex != LB_ERR )
                pLB -> SetItemData( iIndex, (DWORD) p_oZAList );
        }
        oList . RemoveAll();
    }
    pLB -> SetCurSel( -1 );
    m_p_oCurrentZAList = NULL;
    mfn_vUpdateButtons();
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void CDlgZAL::mfn_vUpdateButtons()
{
    M_Enable(IDC_BT_NEW,    (m_p_oCurrentNamesList != NULL) );
    M_Enable(IDC_BT_COPY,   (m_p_oCurrentZAList != NULL) );
    M_Enable(IDC_BT_RENAME, (m_p_oCurrentZAList != NULL) );
    M_Enable(IDC_BT_DELETE, (m_p_oCurrentZAList != NULL) );
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
int CDlgZAL::mfn_iSelectStringExact(CListBox * _pLB, CString _csName)
{
    int iIndex = _pLB -> FindStringExact( -1, _csName );
    if( iIndex != LB_ERR )
        _pLB -> SetCurSel( iIndex );
    return iIndex;
}