/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

/******************************************************************
Change history

Ref#0002	29/05/2002	mashok@in.ibm.com
Ref#0003    30/06/2003  pmanimur@in.ibm.com
******************************************************************/
// SessionPane.cpp : implementation file
//

#include "stdafx.h"
#include "SCTS3DevMan.h"
#include "SessionPane.h"
#include "SctsDevManSession.h"

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

static void uiCallback(void *data,SctsCommand *command,int flag,bool build)
{
	SctsPcdata tempPcdata;
	SctsTargetSource tempTS;
	SctsItemList itemList;
	SctsItem item;


	CSessionPane *sessionPane=(CSessionPane *)data;
	//[Ref#0003    30/06/2003  pmanimur@in.ibm.com
	// The status of the atomic command needs to be updated for 
	// after the EndAtomic is recieved..
	// See SctsDMClientSession.cpp Ref #0002
	//]

	if((SCTS_ATOMIC_END == flag)&& !build &&(NULL==command))
	{
		sessionPane = (CSessionPane*)((PARAMPKT*)data)->pParam1; 
	}

	int itemCnt=sessionPane->m_commands.GetItemCount();
	int msgID;
	int itemID=0;
	char *cmdID;
	char *cmdType;
	char *target=0;
	char *source=0;
	char msgIDStr[50];
	char status[10];
	if (build) 
		msgID=sessionPane->session->currentMsgID;
	else
		msgID=sessionPane->session->processMsgID;
	sctsInt2Str(msgID,msgIDStr,10);
	if (!command)
	{
		sessionPane->m_commands.InsertItem(itemCnt,"-",build?1:0);
		sessionPane->m_commands.SetItemData(itemCnt,(DWORD)-1);
		sessionPane->m_commands.SetItem(itemCnt,1,LVIF_TEXT,msgIDStr,0,0,0,0);
		sessionPane->m_commands.SetItem(itemCnt,2,LVIF_TEXT,sctsCommandStrings[flag],0,0,0,0);
	
				
		//Check whether there is an update on AtomicStaus along with 
		//Ref 0003 Patches..
		if((SCTS_ATOMIC_END == flag)&& !build)
		{
			sessionPane = (CSessionPane*)((PARAMPKT*)data)->pParam1; 
			int nStatus = *(int*)((PARAMPKT*)data)->pParam2;
			//Now search for the atomic command just before this..
			//
			int nItemCount =  sessionPane->m_commands.GetItemCount();
			CString strText = "Atomic";
			
			while(--nItemCount)
			{
			  if(strText == sessionPane->m_commands.GetItemText(nItemCount,2))
					break;
				  
			}
			char buf[10];
			memset(buf,0,sizeof(buf));

			sctsInt2Str(nStatus,buf,10);
			sessionPane->m_commands.SetItemText(nItemCount,3,buf);


		}

		return;
	}
	if (!build) 
	{
		if (command->GetType()==SCTS_STATUS)
		{
			/* Ref#0035 : Find the command based on the CmdRef and MsgRef */
			int cmdRef=((SctsStatus *)command)->GetCmdRef(tempPcdata).GetInt();
			int cnt=sessionPane->m_commands.GetItemCount();
			int msgRef=((SctsStatus *)command)->GetMsgRef();
			LVITEM item;
			item.mask=LVIF_IMAGE | LVIF_PARAM;
			item.iSubItem=0;
			for (item.iItem=0;item.iItem<cnt;++item.iItem)
			{
				sessionPane->m_commands.GetItem(&item);
				if (item.iImage!=1 || cmdRef!=item.lParam || atoi(sessionPane->m_commands.GetItemText(item.iItem,1))!=msgRef) continue;
				break;
			}
			if (item.iItem==cnt) return;
			sctsInt2Str(((SctsStatus *)command)->GetData(),status,10);
			if (flag)
			{
				sessionPane->m_commands.SetItem(item.iItem+flag-1,3,LVIF_TEXT,status,0,0,0,0);
				return;
			}
			for (;sessionPane->m_commands.GetItemData(item.iItem)==cmdRef;++item.iItem)
				sessionPane->m_commands.SetItem(item.iItem,3,LVIF_TEXT,status,0,0,0,0);
			return;
		}

		sctsInt2Str(command->status,status,10);
	}
	cmdID=(char *)command->GetCommandID(tempPcdata);
	cmdType=sctsCommandStrings[command->GetType()];
	switch(command->GetType())
	{
		case SCTS_ADD:
		case SCTS_DELETE:
		case SCTS_REPLACE:
			((SctsGeneric *)command)->GetItemList(itemList);
			break;
		case SCTS_GET:
			((SctsGet *)command)->GetItemList(itemList);
			break;
		case SCTS_RESULTS:
			((SctsResults *)command)->GetItemList(itemList);
			break;
		case SCTS_ALERT:
			((SctsAlert *)command)->GetItemList(itemList);
			break;
		case SCTS_HEADER:
			source=(char *)(((SctsSyncHdr *)command)->GetSource(tempTS).GetLocURI(tempPcdata));
			target=(char *)(((SctsSyncHdr *)command)->GetTarget(tempTS).GetLocURI(tempPcdata));
			break;
	}
	if (itemList)
	{
		itemList.GetData(item);
		do
		{
			++itemID;
			if (!build && flag && itemID!=flag) continue;
			source=(char *)(item.GetSource(tempTS).GetLocURI(tempPcdata));
			target=(char *)(item.GetTarget(tempTS).GetLocURI(tempPcdata));
			sessionPane->m_commands.InsertItem(itemCnt,cmdID,build?1:0);
			sessionPane->m_commands.SetItemData(itemCnt,atoi(cmdID));
			sessionPane->m_commands.SetItem(itemCnt,1,LVIF_TEXT,msgIDStr,0,0,0,0);
			sessionPane->m_commands.SetItem(itemCnt,2,LVIF_TEXT,cmdType,0,0,0,0);
			if (!build)
				sessionPane->m_commands.SetItem(itemCnt,3,LVIF_TEXT,status,0,0,0,0);
			sessionPane->m_commands.SetItem(itemCnt,4,LVIF_TEXT,build?source:target,0,0,0,0);
			sessionPane->m_commands.SetItem(itemCnt,5,LVIF_TEXT,build?target:source,0,0,0,0);
			/* Ref#0004 */
			++itemCnt;
		} while ((bool)itemList.GetNextData(item));
	}
	else
	{
		if (command->GetType()==SCTS_HEADER)
		{
			sessionPane->m_commands.InsertItem(itemCnt,"0",build?1:0);
			sessionPane->m_commands.SetItemData(itemCnt,0);
		}
		else
		{
			sessionPane->m_commands.InsertItem(itemCnt,cmdID,build?1:0);
			sessionPane->m_commands.SetItemData(itemCnt,atoi(cmdID));
		}
		sessionPane->m_commands.SetItem(itemCnt,1,LVIF_TEXT,msgIDStr,0,0,0,0);
		sessionPane->m_commands.SetItem(itemCnt,2,LVIF_TEXT,cmdType,0,0,0,0);
		if (!build) sessionPane->m_commands.SetItem(itemCnt,3,LVIF_TEXT,status,0,0,0,0);
		if (source) sessionPane->m_commands.SetItem(itemCnt,4,LVIF_TEXT,build?source:target,0,0,0,0);
		if (target) sessionPane->m_commands.SetItem(itemCnt,5,LVIF_TEXT,build?target:source,0,0,0,0);
	}
	if (build && command->GetType()==SCTS_HEADER) 
		sessionPane->lastRecvItemStatus=itemCnt;
	
}

/////////////////////////////////////////////////////////////////////////////
// CSessionPane dialog


CSessionPane::CSessionPane(SctsDMSession *_session,CWnd* pParent /*=NULL*/)
	: CDialog(CSessionPane::IDD, pParent),session(_session)
{
	//{{AFX_DATA_INIT(CSessionPane)
	//}}AFX_DATA_INIT
	lastRecvItemStatus=-1;
}


void CSessionPane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSessionPane)
	DDX_Control(pDX, IDC_COMMANDS, m_commands);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSessionPane, CDialog)
	//{{AFX_MSG_MAP(CSessionPane)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSessionPane message handlers

BOOL CSessionPane::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	imgList.Create(16,16,ILC_MASK | ILC_COLOR8,2,0);
	imgList.Add(LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_IN)));
	imgList.Add(LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_OUT)));

	m_commands.SetExtendedStyle(LVS_EX_FULLROWSELECT | m_commands.GetExtendedStyle());
	m_commands.SetImageList(&imgList,LVSIL_SMALL);
	m_commands.InsertColumn(0,"Cmd ID",0,50,3);
	m_commands.InsertColumn(1,"Msg ID",0,50,2);
	m_commands.InsertColumn(2,"Command",0,70,4);
	m_commands.InsertColumn(3,"Status",0,45,5);
	m_commands.InsertColumn(4,"Source",0,110,4);
	m_commands.InsertColumn(5,"Target",0,110,4);
	
	/* Set the UI callback */
	session->SetUICallback(this,uiCallback);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSessionPane::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	if (m_commands.m_hWnd)
	{
		RECT rect;
		GetClientRect(&rect);
		m_commands.MoveWindow(&rect);

	}
}

BOOL CSessionPane::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN && (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN)) return TRUE;
	
	return CDialog::PreTranslateMessage(pMsg);
}
