// OutputBlockDialog.cpp : implementation file
//

#include "stdafx.h"
#include <fstream>				// rqd. for file i/o in OnOutputBlkDlgBtnSaveData()
#include "DiagramEng.h"
#include "OutputBlockDialog.h"
#include "MainFrm.h"			// rqd. since a ptr-to-CMainFrame is used in OnOutputBlkDlgBtnShowGraph()
#include "OutputBlockView.h"	// rqd. since a ptr-to-COutputBlockView is used in OnOutputBlkDlgBtnShowGraph()

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

// DiagramEng (start)
// theApp needs to be accessed from within the OutputBlockDialog.cpp file, although it's declared in the DiagramEng.cpp file.
// It is used in the fn OnOutputBlkDlgBtnShowGraph() below.
extern CDiagramEngApp theApp;
// DiagramEng (end)

/////////////////////////////////////////////////////////////////////////////
// COutputBlockDialog dialog


//COutputBlockDialog::COutputBlockDialog(CWnd* pParent /*=NULL*/)
//	: CDialog(COutputBlockDialog::IDD, pParent)
COutputBlockDialog::COutputBlockDialog(COutputBlock *ref_blk, CWnd* pParent /*=NULL*/)
	: CDialog(COutputBlockDialog::IDD, pParent)
{
	// DiagramEng (start)
	m_pOutputBlk = ref_blk;
	// DiagramEng (end)

	//{{AFX_DATA_INIT(COutputBlockDialog)
	m_iNotation = -1;
	m_iTimePtDisplay = -1;
	//}}AFX_DATA_INIT
}


void COutputBlockDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COutputBlockDialog)
	DDX_Radio(pDX, ID_OUTPUT_BLK_DLG_RB_STDNOT, m_iNotation);
	DDX_Radio(pDX, ID_OUTPUT_BLK_DLG_RB_TPTS, m_iTimePtDisplay);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COutputBlockDialog, CDialog)
	//{{AFX_MSG_MAP(COutputBlockDialog)
	ON_BN_CLICKED(ID_OUTPUT_BLK_DLG_BTN_OK, OnOutputBlkDlgBtnOk)
	ON_BN_CLICKED(ID_OUTPUT_BLK_DLG_BTN_SG, OnOutputBlkDlgBtnShowGraph)
	ON_BN_CLICKED(ID_OUTPUT_BLK_DLG_BTN_SD, OnOutputBlkDlgBtnSaveData)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COutputBlockDialog message handlers

void COutputBlockDialog::OnOutputBlkDlgBtnOk() 
{
	// TODO: Add your control notification handler code here

	// DiagramEng (start)

	//AfxMessageBox("\n COutputBlockDialog::OnOutputBlkDlgBtnOk()\n", MB_OK, 0);
	
	// Update variable values with the Dlg Wnd control values
	UpdateData(TRUE);

	// Close the dialog wnd with OnOK()
	OnOK();

	// DiagramEng (end)	
}

void COutputBlockDialog::OnOutputBlkDlgBtnShowGraph() 
{
	// TODO: Add your control notification handler code here
	
	// DiagramEng (start)
	//AfxMessageBox("\n COutputBlockDialog::OnOutputBlkDlgBtnShowGraph()\n", MB_OK, 0);

	// VIEW CONSTRUCTION
	CMainFrame *main_frame = (CMainFrame *)theApp.m_pMainWnd;			// Get a pointer-to-CMainFrame.
	CMDIChildWnd *pActiveChild = main_frame->MDIGetActive();			// Get a pointer to the active child window.

	if(pActiveChild != 0)
	{
		CDocument *pDocument = pActiveChild->GetActiveDocument();		// Get a pointer to the active document.
		if(pDocument != 0)
		{
			CDiagramEngApp *pApp = (CDiagramEngApp *)AfxGetApp();
			CDocTemplate *pTemp = pApp->GetCOutputViewTemplate();				// Get the document template for the new view.
			CFrameWnd *pFrame = pTemp->CreateNewFrame(pDocument, pActiveChild);	// Create a new frame.

			if(pFrame != 0)
			{
				pTemp->InitialUpdateFrame(pFrame, pDocument);			// Update the frame.
			}

			// Set the COutputBlockView ref-to-block
			COutputBlockView *out_blk_view = (COutputBlockView *)pFrame->GetActiveView();
			out_blk_view->SetRefOutputBlock(m_pOutputBlk);
		}
	}

	// Close the dialog wnd with OnOK()
	OnOK();

	// DiagramEng (end)	
}

void COutputBlockDialog::OnOutputBlkDlgBtnSaveData() 
{
	// TODO: Add your control notification handler code here

	// DiagramEng (start)
	int btnSel;																// button selection
	ifstream fin;															// infile stream
	ofstream fout;															// outfile stream
	BOOL dlgType = FALSE;													// TRUE => File Open dlg, FALSE => File Save dlg
	CString sCaption = "DiagramEng - Overwrite";							// message box caption
	CString strDefExt = ".txt";												// default file extension
	CString strFileName = "output_data.txt";								// file name		
	CString strFilePath;													// local file path var
	CString strFilter = "All Files (*.*)|*.*|Plain Text (*.txt)|*.txt||";	// string pairs specifying filters for file list box
	CString sMsg;															// main msg string
	CString sMsgTemp;														// temp msg string
	CWnd l_wnd;																// local CWnd
	DWORD dwFlags = OFN_ENABLESIZING | OFN_HIDEREADONLY;					// customization flags
	UINT nType = MB_OK;														// style of msg. box
	UINT nIDhelp = 0;														// help context ID for the msg.

	//sMsg.Format("\n COutputBlockDialog::OnOutputBlkDlgBtnSaveData()\n");
	//AfxMessageBox(sMsg, nType, nIDhelp);

	// CREATE A CFileDialog DIALOG WND
	CFileDialog dlgFile(dlgType, strDefExt, strFileName, dwFlags, strFilter, NULL);

	// CHECK BUTTON SELECTION
	if(dlgFile.DoModal() == IDOK)
	{
		strFilePath = dlgFile.GetPathName();
		
		// CHECK TO SEE IF FILE EXISTS
		fin.open(strFilePath, ios::in);		// open file in input mode
		if(!fin)
		{
			fout.open(strFilePath, ios::out);	// open file in output mode
		}
		else
		{
			// Question user about overwriting existing file
			sMsgTemp.Format(" %s already exists.  \n", strFilePath);
			sMsg += sMsgTemp;
			sMsgTemp.Format(" Do you want to replace it?\n");
			sMsg += sMsgTemp;
			nType = MB_YESNO | MB_ICONEXCLAMATION;

			btnSel = l_wnd.MessageBox(sMsg, sCaption, nType);
					
			// Check button selection
			if(btnSel == IDYES)
			{
				fout.open(strFilePath, ios::out);	// open file in output mode
			}
			else if(btnSel == IDNO)
			{
				sMsg.Format("");
				sMsg.Format("\n Aborting writing model data to file.    \n");
				nType = MB_OK | MB_ICONSTOP;
				//AfxMessageBox(sMsg, nType, nIDhelp);

				fin.close();
				fin.clear();
				return;
			}
		}
		fin.close();
		fin.clear();
		
		// CHECK IF FILE OPEN
		if(!fout)				// if(!false) then execute
		{
			sMsg.Format("");
			sMsgTemp.Format("\n COutputBlockDialog::OnOutputBlkDlgBtnSaveData()\n");
			sMsg += sMsgTemp;
			sMsgTemp.Format("\n Unable to open file: %s\n", strFilePath);
			sMsg += sMsgTemp;
			sMsgTemp.Format(" Aborting file i/o.\n");
			sMsg += sMsgTemp;
			nType = MB_OK | MB_ICONSTOP;
			AfxMessageBox(sMsg, nType, nIDhelp);

			// Close the outfile stream and clear fail flag
			fout.close();									
			fout.clear();	// clears fail flag setting from previous file io.
			return;
		}
		
		// CALL THE COutputBlock FN TO WRITE THE NUMERICAL DATA TO A FILE
		m_pOutputBlk->WriteNumericalOutputDataToFile(fout);
	}

	// Close the outfile stream and clear fail flag
	fout.close();									
	fout.clear();	// clears fail flag setting from previous file i/o.

	// DiagramEng (end)
}
