// ProgDemoView.cpp : Implementierung der Klasse CProgDemoView
//

#include "stdafx.h"
#include "ProgDemo.h"

#include "ProgDemoDoc.h"
#include "ProgDemoView.h"

#include "MainFrm.h"
#include "ProgDLLN.h"

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

/////////////////////////////////////////////////////////////////////////////
// CProgDemoView

IMPLEMENT_DYNCREATE(CProgDemoView, CFormView)

BEGIN_MESSAGE_MAP(CProgDemoView, CFormView)
	//{{AFX_MSG_MAP(CProgDemoView)
	ON_BN_CLICKED(IDC_SET_CHANNEL, OnSetChannel)
	ON_BN_CLICKED(IDC_PROGRAM_ALL, OnProgramAll)
	ON_WM_TIMER()
	ON_BN_CLICKED(ID_APP_EXIT, OnAppExit)
	ON_BN_CLICKED(IDC_ADD_FILE, OnAddFile)
	ON_BN_CLICKED(IDC_DELETE_FILE, OnDeleteFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProgDemoView Konstruktion/Destruktion

CProgDemoView::CProgDemoView()
	: CFormView(CProgDemoView::IDD)
{
	//{{AFX_DATA_INIT(CProgDemoView)
	m_nChannel = 0;
	m_strCOMPort = _T("COM1");
	//}}AFX_DATA_INIT

	hinstLib = LoadLibrary("ProgDLLN.dll");
}

CProgDemoView::~CProgDemoView()
{
	KillTimer(42);
}

void CProgDemoView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProgDemoView)
	DDX_Control(pDX, IDC_PROJECT_LIST, m_cbProjectList);
	DDX_Text(pDX, IDC_CHANNEL, m_nChannel);
	DDX_Text(pDX, IDC_COMPORT, m_strCOMPort);
	//}}AFX_DATA_MAP
}

BOOL CProgDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// ZU ERLEDIGEN: Ändern Sie hier die Fensterklasse oder das Erscheinungsbild, indem Sie
	//  CREATESTRUCT cs modifizieren.

	return CFormView::PreCreateWindow(cs);
}

void CProgDemoView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	GetDlgItem(IDC_PROJECT_LIST)->ModifyStyle(0, WS_DISABLED);
	GetDlgItem(IDC_ADD_FILE)->ModifyStyle(0, WS_DISABLED);
	GetDlgItem(IDC_DELETE_FILE)->ModifyStyle(0, WS_DISABLED);
	GetDlgItem(IDC_PROGRAM_ALL)->ModifyStyle(0, WS_DISABLED);
}

/////////////////////////////////////////////////////////////////////////////
// CProgDemoView Diagnose

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

void CProgDemoView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CProgDemoDoc* CProgDemoView::GetDocument() // Die endgültige (nicht zur Fehlersuche kompilierte) Version ist Inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CProgDemoDoc)));
	return (CProgDemoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CProgDemoView Nachrichten-Handler

void CProgDemoView::OnSetChannel() 
{
	if (hinstLib == NULL)
		return;

	UpdateData(TRUE);

	int res = 0;
	char resStr[64];
	
	// set channel
	if ((SetChannel = (RINT_STR_RINT_STR)GetProcAddress(hinstLib, "SetChannel")) == NULL)
	{
		MessageBox("Failed calling DLL function!");
		return;
	}
	SetChannel(m_nChannel, m_strCOMPort.GetBuffer(32), res, resStr);
 
	m_strStatusBarText = resStr;

	SetTimer(42, 200, NULL);

	UpdateData(FALSE);

	if (res != 2)
		return;

	// success -> activate buttons and enumerate projects
	GetDlgItem(IDC_ADD_FILE)->ModifyStyle(WS_DISABLED, 0);
	GetDlgItem(IDC_PROJECT_LIST)->ModifyStyle(WS_DISABLED, 0);

	GetProjects();
}

void CProgDemoView::GetProjects()
{
	UpdateData(TRUE);

	// get active project
	CString strRecentProject = GetRecentProject();

	// clear list
	m_cbProjectList.ResetContent();

	GetDlgItem(IDC_PROGRAM_ALL)->ModifyStyle(0, WS_DISABLED);
	GetDlgItem(IDC_DELETE_FILE)->ModifyStyle(0, WS_DISABLED);

	int n = 0;
	int i = 0;
	int nSelect = 0;

	// add all projects
	while (1)
	{
		int res = 0;
		char resStr[64];
	
		// get next file
		if ((GetFile = (INT_RINT_STR)GetProcAddress(hinstLib, "GetFile")) == NULL)
		{
			MessageBox("Failed calling DLL function!");
			return;
		}
		GetFile(i, res, resStr);

		if (res != resOk)
			break;

		// success -> add file to list
		n = m_cbProjectList.AddString(resStr);

		// check if project already in ICP
		if (strRecentProject.Compare(resStr) == 0)
			nSelect = n;

		i++;
	};

	if (i > 0)
	{
		GetDlgItem(IDC_PROGRAM_ALL)->ModifyStyle(WS_DISABLED, 0);
		GetDlgItem(IDC_DELETE_FILE)->ModifyStyle(WS_DISABLED, 0);
	}

	// select active project
	m_cbProjectList.SetCurSel(nSelect);

	UpdateData(FALSE);
}

CString CProgDemoView::GetRecentProject() 
{
	if (hinstLib == NULL)
		return "Failed loading DLL!";

	UpdateData(TRUE);

	int res = 0, age = 0;
	char resStr[64];

	// get active project in ICP
	if ((GetProjName = (INT_RINT_RINT_STR)GetProcAddress(hinstLib, "GetProjName")) == NULL)
	{
		MessageBox("Failed calling DLL function!");
		return "Failed calling DLL function!";
	}
	GetProjName(m_nChannel, age, res, resStr);
 
	return resStr;
}

void CProgDemoView::OnAddFile() 
{
	if (hinstLib == NULL)
		return;

	UpdateData(TRUE);

	int res = 0;
	char resStr[64];

	// setup and open file dialog
	CFileDialog Dlg(TRUE, "encr", "*.encr", OFN_FILEMUSTEXIST, "Encryption Files (*.encr)|*.encr|All Files (*.*)|*.*||", NULL);
	CString strFile;

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

	// add file
	strFile = Dlg.GetPathName();

	if ((AddNewFile = (STR_RINT_STR)GetProcAddress(hinstLib, "AddNewFile")) == NULL)
	{
		MessageBox("Failed calling DLL function!");
		return;
	}
	AddNewFile(strFile.GetBuffer(1024), res, resStr);

	m_strStatusBarText = resStr;

	UpdateData(FALSE);

	GetProjects();
}

void CProgDemoView::OnProgramAll() 
{
	if (hinstLib == NULL)
		return;

	UpdateData(TRUE);

	int res = 0;
	char resStr[64];

	// selected project in ICP?
	if (m_cbProjectList.GetCount() == 0)
	{
		MessageBox("No Project Available!");
		return;
	}

	CString strLine, strRecent;
	m_cbProjectList.GetLBText(m_cbProjectList.GetCurSel(), strLine);
	strRecent = GetRecentProject();

	if (strRecent.Compare(strLine) != 0)
	{
		// download file in ICP
		if ((DownloadFile = (STR_INT_INT_RINT_STR)GetProcAddress(hinstLib, "DownloadFile")) == NULL)
		{
			MessageBox("Failed calling DLL function!");
			return;
		}
		DownloadFile(strLine.GetBuffer(128), 0, m_nChannel, res, resStr);

		if (res != resOk)
		{
			MessageBox("Error downloading file!");
			return;
		}
	}

	// program device
	if ((ProgDevice = (INT_RINT_STR)GetProcAddress(hinstLib, "ProgDevice")) == NULL)
	{
		MessageBox("Failed calling DLL function!");
		return;
	}
	ProgDevice(m_nChannel, res, resStr);
 
	m_strStatusBarText = resStr;

	UpdateData(FALSE);
}

void CProgDemoView::SetStatusBarText()
{
	CMainFrame*	pMainFrame = (CMainFrame*)GetActiveWindow();
	pMainFrame->m_wndStatusBar.SetWindowText(m_strStatusBarText);

	Invalidate(FALSE);
}


void CProgDemoView::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent == 42)
		SetStatusBarText();
	
	CFormView::OnTimer(nIDEvent);
}

void CProgDemoView::OnAppExit() 
{
	ExitProcess(0);
}

void CProgDemoView::OnDeleteFile() 
{
	if (hinstLib == NULL)
		return;

	UpdateData(TRUE);

	int res = 0;
	char resStr[64];

	// add file
	CString strFile;
	m_cbProjectList.GetLBText(m_cbProjectList.GetCurSel(), strFile);

	if ((DeleteFileB = (STR_RINT_STR)GetProcAddress(hinstLib, "DeleteFile")) == NULL)
	{
		MessageBox("Failed calling DLL function!");
		return;
	}
	DeleteFileB(strFile.GetBuffer(1024), res, resStr);

	m_strStatusBarText = resStr;

	UpdateData(FALSE);

	GetProjects();
}

void CProgDemoView::OnDraw(CDC* pDC) 
{
	AfxGetMainWnd()->SetWindowText("Contronix - ICP Programmer Demo");
}
