/*==========================================================================*/
/* Copyright (c) 1999 - Sykes Enterprises Inc. - All rights reserved        */
/*--------------------------------------------------------------------------*/
/* Sykes grants you ("Licensee") a non-exclusive, royalty free, license to  */
/* use, modify and redistribute this software in source and binary code     */
/* form, provided that i) this copyright notice and license appear on all   */
/* copies of the software; and ii) Licensee does not utilize the software   */
/* in a manner which is disparaging to Sykes.                               */
/*                                                                          */
/* This software is provided "AS IS", without a warranty of any kind. ALL   */
/* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING */
/* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR        */
/* PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SYKES AND ITS          */
/* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A  */
/* RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS           */
/* DERIVATIVES. IN NO EVENT WILL SYKES OR ITS LICENSORS BE LIABLE FOR ANY   */
/* LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,          */
/* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND        */
/* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR      */
/* INABILITY TO USE SOFTWARE, EVEN IF SYKES HAS BEEN ADVISED OF THE         */
/* POSSIBILITY OF SUCH DAMAGES.                                             */
/*==========================================================================*/

#include "StdAfx.h"
#include "Waikoloa.h"
#include "WaikoloaDoc.h"
#include "DlgNewTM.h"
#include "DlgAdd.h"


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


/*--------------------------------------------------------------------------*/

IMPLEMENT_DYNCREATE(CWaikoloaDoc, CDocument)

BEGIN_MESSAGE_MAP(CWaikoloaDoc, CDocument)
	//{{AFX_MSG_MAP(CWaikoloaDoc)
	ON_COMMAND(ID_TM_ADD, OnTmAdd)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_FILE_SAVEASTMX, OnFileSaveastmx)
	ON_COMMAND(ID_TM_ADDFILE, OnTmAddfile)
	ON_UPDATE_COMMAND_UI(ID_TM_ADD, OnUpdateTmAdd)
	ON_UPDATE_COMMAND_UI(ID_TM_ADDFILE, OnUpdateTmAddfile)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVEASTMX, OnUpdateFileSaveastmx)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/*--------------------------------------------------------------------------*/

CWaikoloaDoc::CWaikoloaDoc()
{
	m_bFirstTime = true;
}


/*--------------------------------------------------------------------------*/

CWaikoloaDoc::~CWaikoloaDoc()
{
}


/*--------------------------------------------------------------------------*/

BOOL CWaikoloaDoc::OnNewDocument()
{
	CDlgNewTM            Dlg;
	FILE*                pFile;


	if ( m_bFirstTime )
	{
		if (!CDocument::OnNewDocument())
			return(FALSE);
		
		m_bFirstTime = false;
		return(TRUE);
	}

	if ( Dlg.DoModal() != IDOK )
	{
		if ( m_TmQ.IsOpen() ) SetTitle(m_TmQ.GetTM()->GetPath());
		return(TRUE);
	}

	// Check if the user wants to overwrite
	if ( (pFile = fopen(Dlg.GetPath(), "rb")) != NULL )
	{
		fclose(pFile);
		CString csMsg;
		csMsg.Format(IDS_WANTOVERWRITE, Dlg.GetPath());
		if ( AfxMessageBox(csMsg, MB_ICONEXCLAMATION | MB_YESNO, 0) == IDYES )
		{
			unlink(Dlg.GetPath());
		}
		else
		{
			return(TRUE);
		}
	}

	// Now start creating the new file
	if (!CDocument::OnNewDocument())
	{
		if ( m_TmQ.IsOpen() ) SetTitle(m_TmQ.GetTM()->GetPath());
		return(FALSE);
	}

	// Create/open the TM (close automatically opened TM if any)
	if ( !m_TmQ.GetTM()->Open(Dlg.GetPath(), false, Dlg.GetSource(),
		Dlg.GetTarget(), "windows-1252") )
	{
		AfxMessageBox(IDS_CANTOPENTM, MB_OK | MB_ICONEXCLAMATION, 0);
		return(FALSE);
	}

	SetPathName(Dlg.GetPath());
	return(TRUE);
}


/*--------------------------------------------------------------------------*/

#ifdef _DEBUG
void CWaikoloaDoc::AssertValid() const
{
	CDocument::AssertValid();
}

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


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnUpdateTmAdd(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_TmQ.IsOpen());
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnTmAdd() 
{
	CDlgAdd              Dlg;


	if ( Dlg.DoModal() != IDOK ) return;

	switch ( m_TmQ.GetTM()->Add(Dlg.GetSource(), Dlg.GetTarget(),
		Dlg.GetType(), Dlg.GetId(), Dlg.GetX(), Dlg.GetY(), Dlg.GetCX(),
		Dlg.GetCY()) )
	{
		case okLTM_ADD_ERROR:
			AfxMessageBox(IDS_ERR_CANTADD, MB_OK | MB_ICONEXCLAMATION, 0);
			break;
		case okLTM_ADD_EMPTY:
			AfxMessageBox(IDS_ERR_EMPTY, MB_OK | MB_ICONEXCLAMATION, 0);
			break;
		case okLTM_ADD_DUPLICATE:
			AfxMessageBox(IDS_ERR_DUPLICATE, MB_OK | MB_ICONEXCLAMATION, 0);
			break;
		case okLTM_ADD_ADDED:
			// Add was successful
			break;
	}

	// Save the updated index on disk
	m_TmQ.GetTM()->UpdateFile();

	// Update view
	UpdateAllViews(NULL);
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnUpdateFileSaveAs(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_TmQ.IsOpen());
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnFileSaveAs() 
{
	CString              csTitle;
	CString              csFilter;


	csTitle.LoadString(IDS_OTFTITLE);
	csFilter.LoadString(IDS_OTFFILTER);

	CFileDialog Dlg(FALSE, "otf", NULL,
		OFN_EXPLORER | OFN_SHAREAWARE | OFN_HIDEREADONLY, csFilter);
	Dlg.m_ofn.lpstrTitle = csTitle;

	if ( Dlg.DoModal() != IDOK ) return;

	// Force redraw window and start hour-glass cursor
	AfxGetMainWnd()->RedrawWindow();
	CWaitCursor HourGlass;

	// Save as OpenTag
	((CFrameWnd*)AfxGetMainWnd())->SetMessageText(IDS_SAVEASOPENTAG);
	m_TmQ.GetTM()->SaveAsOpenTag(Dlg.GetPathName(), TOOL);
	((CFrameWnd*)AfxGetMainWnd())->SetMessageText(AFX_IDS_IDLEMESSAGE);
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnUpdateFileSaveastmx(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_TmQ.IsOpen());
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnFileSaveastmx() 
{
	CString              csTitle;
	CString              csFilter;


	csTitle.LoadString(IDS_TMXTITLE);
	csFilter.LoadString(IDS_TMXFILTER);

	CFileDialog Dlg(FALSE, "tmx", NULL,
		OFN_EXPLORER | OFN_SHAREAWARE | OFN_HIDEREADONLY, csFilter);
	Dlg.m_ofn.lpstrTitle = csTitle;

	if ( Dlg.DoModal() != IDOK ) return;

	// Force redraw window and start hour-glass cursor
	AfxGetMainWnd()->RedrawWindow();
	CWaitCursor HourGlass;

	// Save as TMX
	((CFrameWnd*)AfxGetMainWnd())->SetMessageText(IDS_SAVEASTMX);
	m_TmQ.GetTM()->SaveAsTMX(Dlg.GetPathName(), TOOL, TOOLVERSION);
	((CFrameWnd*)AfxGetMainWnd())->SetMessageText(AFX_IDS_IDLEMESSAGE);
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnUpdateTmAddfile(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_TmQ.IsOpen());
}


/*--------------------------------------------------------------------------*/

void CWaikoloaDoc::OnTmAddfile() 
{
	CString              csMsg;
	int                  nTotal;
	int                  nError;
	int                  nDuplicate;
	int                  nEmpty;
	int                  nAdded;
	CString              csTitle;
	CString              csFilter;


	// Info dialog box
	csMsg.LoadString(IDS_ADDFILEINFO);
	if ( AfxMessageBox(csMsg, MB_YESNO | MB_ICONINFORMATION, 0) != IDYES ) return;

	// Get the file name
	csTitle.LoadString(IDS_ADDFILETITLE);
	csFilter.LoadString(IDS_ADDFILEFILTER);
	CFileDialog Dlg(TRUE, "txt", NULL,
		OFN_EXPLORER | OFN_SHAREAWARE | OFN_HIDEREADONLY, csFilter);
	Dlg.m_ofn.lpstrTitle = csTitle;
	if ( Dlg.DoModal() != IDOK ) return;
	
	// Force redraw window and start hour-glass cursor
	AfxGetMainWnd()->RedrawWindow();
	CWaitCursor HourGlass;
	((CFrameWnd*)AfxGetMainWnd())->SetMessageText(IDS_ADDFILE);

	// Add multiple entries from the file
	if ( !m_TmQ.GetTM()->AddEntriesFromFile(Dlg.GetPathName(),
				&nTotal, &nAdded, &nEmpty, &nDuplicate, &nError) )
	{
		csMsg.Format(IDS_ERR_ADDFILE, Dlg.GetPathName());
		AfxMessageBox(csMsg, MB_OK | MB_ICONEXCLAMATION, 0);
	}
	else
	{
		csMsg.Format(IDS_ADDFILERESULTS, Dlg.GetPathName(), nTotal, nAdded,
			nEmpty, nDuplicate, nError);
		AfxMessageBox(csMsg, MB_OK | MB_ICONINFORMATION, 0);
	}

	// Update view
	UpdateAllViews(NULL);
	((CFrameWnd*)AfxGetMainWnd())->SetMessageText(AFX_IDS_IDLEMESSAGE);
}



/*--------------------------------------------------------------------------*/

BOOL CWaikoloaDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return(FALSE);
	
	// Create/open the TM (close automatically opened TM if any)
	if ( !m_TmQ.GetTM()->Open(lpszPathName, false) )
	{
		AfxMessageBox(IDS_CANTOPENTM, MB_OK | MB_ICONEXCLAMATION, 0);
		UpdateAllViews(NULL); // Force the update of the view
		return(FALSE);
	}

	SetPathName(lpszPathName);
	return(TRUE);
}

