/* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. */ 

// clientUI.cpp : Defines the class behaviors for the application.

#include "stdafx.h"
#include "clientUI.h"
#include "MainFrm.h"

#include "clientUIDoc.h"
#include "clientUIView.h"

#include "AboutDlg.h"
#include "DicomService.h"
#include "ListDicomDlg.h"
#include "SearchDicomDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CclientUIApp

BEGIN_MESSAGE_MAP(CclientUIApp, CWinApp)
	ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
	ON_COMMAND(ID_APP_ABOUT, &CclientUIApp::OnAppAbout)
	ON_COMMAND(ID_DATABASE_SYNC, &CclientUIApp::OnDatabaseSync)
	ON_COMMAND(ID_OPEN_ALL, &CclientUIApp::OnViewerOpenall)
	ON_COMMAND(ID_VIEWER_SEARCH, &CclientUIApp::OnViewerSearch)
	ON_COMMAND(ID_DATABASE_EDIT, &CclientUIApp::OnDatabaseEdit)
END_MESSAGE_MAP()

// CclientUIApp construction
CclientUIApp::CclientUIApp()
	: CWinApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

CclientUIApp::~CclientUIApp()
{
	// Close bdb.
	if (close_dbs(&dbs) != 0)
		MessageBox(*(this->GetRoutingFrame()), 
                    TEXT("Fail to close bdb !"), 
                    TEXT("Close ERROR"), MB_OK);
}

// The one and only CclientUIApp object
CclientUIApp theApp;

// CclientUIApp initialization

BOOL CclientUIApp::InitInstance()
{
    // SHInitExtraControls should be called once during your application's 
    // initialization to initialize any of the Windows Mobile specific 
    // controls such as CAPEDIT and SIPPREF.
    SHInitExtraControls();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	
	// Change the registry key under which our settings are stored
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MENU,
		RUNTIME_CLASS(CclientUIDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CclientUIView));
	if (!pDocTemplate)
		return FALSE;
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line.  Will return FALSE 
        // if app was launched with /RegServer, /Register, /Unregserver or 
        // /Unregister.
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	initialize_dbs(&dbs);
	open_dbs(&dbs);

	return TRUE;
}

void CclientUIApp::OnAppAbout()
{
	AboutDlg aboutDlg;
	aboutDlg.DoModal();
}

void CclientUIApp::OnDatabaseSync()
{
	DicomService server;          
	CString stasMsg;
	int recvDicomAmount;
	
	// Download DICOM from DICOM server and store them in bdb. 
	recvDicomAmount = server.Download(&this->dbs);
	if (recvDicomAmount == 0) {
		stasMsg += "Downloaded 0 DICOM image! ";
		stasMsg += "Please check the connection and retry!";
	} else {
		stasMsg.Format(_T("Downloaded %d"), recvDicomAmount);
		stasMsg += " DICOM images! ";
	}

	MessageBox(*(this->GetRoutingFrame()), stasMsg, 
            TEXT("Download status"), MB_OK);
}

void CclientUIApp::OnViewerOpenall()
{
	ListDicomDlg listDlg(dbs);
	listDlg.DoModal();
}

void CclientUIApp::OnViewerSearch()
{
	SearchDicomDlg searchDlg(dbs);
	searchDlg.DoModal();
}

void CclientUIApp::OnDatabaseEdit()
{
	ListDicomDlg listDlg(dbs, true);
	listDlg.DoModal();
}


