// QPTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "QPTest.h"
#include "QPTestDlg.h"

//TESTING SDB!!!!!!!!!!!!!!!
//#include "CalcLibData.h"		
//#include "CalcLibSMA.h"
#include "CalcLibEMA.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{

public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQPTestDlg dialog

CQPTestDlg::CQPTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQPTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQPTestDlg)
	m_symbol = _T("");
	m_price = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQPTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQPTestDlg)
	DDX_Text(pDX, IDC_EDIT_SYMBOL, m_symbol);
	DDX_Text(pDX, IDC_STATIC_PRICE, m_price);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQPTestDlg, CDialog)
	//{{AFX_MSG_MAP(CQPTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQPTestDlg message handlers

BOOL CQPTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	HRESULT hr;

	//  Note - afxdisp.h  must be included in the stdafx.h header file in order to use com in mfc

	// Initialize com for this process.
	
	if (FAILED(hr = ::CoInitialize(NULL)))
	{
		MessageBox("Unable to initialize COM", "Error" );
		return TRUE;
	} 

	CLSID clsid;

	// Now find the class id for each object, and create the dispatch driver for the object
	// If the class id can't be found, then Quotes Plus is probably not installed.

	// IPrice

	hr = ::CLSIDFromProgID( L"QuotesPlus.Price", &clsid);
	if (FAILED(hr))
	{
		MessageBox("Unable to obtain a class ID for QuotesPlus.Price\n\nQuotes Plus may not be installed.", "Error" );
		return TRUE;
	} 

	hr = qpPrice.CreateDispatch( clsid );  // Create the dispath driver for the class

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQPTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CQPTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CQPTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CQPTestDlg::OnOK() 
{
	DOUBLEVECTOR values;
	DATEVECTOR dates;
	int periods;
	char *s = (char *)malloc(10);

	values.push_back(64.75);
	values.push_back(63.79);
	values.push_back(63.73);
	values.push_back(63.73);
	values.push_back(63.55);
	values.push_back(63.19);
	values.push_back(63.91);
	values.push_back(63.85);
	values.push_back(62.95);
	values.push_back(63.37);
	values.push_back(61.33);
	values.push_back(61.51);
	values.push_back(61.87);
	values.push_back(60.25);
	values.push_back(59.35);
	values.push_back(59.95);
	values.push_back(58.93);
	values.push_back(57.68);
	values.push_back(58.82);
	values.push_back(58.87);
	values.push_back(64.75);
	values.push_back(63.79);
	values.push_back(63.73);
	values.push_back(63.73);
	values.push_back(63.55);
	values.push_back(63.19);
	values.push_back(63.91);
	values.push_back(63.85);
	values.push_back(62.95);
	values.push_back(63.37);
	values.push_back(61.33);
	values.push_back(61.51);
	values.push_back(61.87);
	values.push_back(60.25);
	values.push_back(59.35);
	values.push_back(59.95);
	values.push_back(58.93);
	values.push_back(57.68);
	values.push_back(58.82);
	values.push_back(58.87);


	dates.push_back(COleDateTime::COleDateTime(1999, 10, 20, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 21, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 22, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 23, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 24, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 25, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 26, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 27, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 28, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 29, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 30, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 10, 31, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 1, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 2, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 3, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 4, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 5, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 6, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 7, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 8, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 9, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 10, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 11, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 12, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 15, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 16, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 17, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 18, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 19, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 22, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 23, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 24, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 26, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 29, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 11, 30, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 12, 1, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 12, 2, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 12, 3, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 12, 6, 12, 0, 0)); 
	dates.push_back(COleDateTime::COleDateTime(1999, 12, 7, 12, 0, 0)); 

	periods = 12;		//values.size();

	CalcLibEMA cle(values, dates, periods);
	cle.doCalc();

	int length = dates.size();
	for (int i = 0; i < length; i++)
	{
		CalcLibData cld = cle.getOutputPointByIndex(i);
		double d = cld.getValue();
		_gcvt(d,4,s);
		CString *myString = new CString(s);
		//if (i == (length - 1))
		MessageBox(myString->GetBuffer(0));
	}

	UpdateData(TRUE);
	qpPrice.SetSymbol(m_symbol);

	long m_DaysLoaded = qpPrice.GetDaysLoaded();

	VARIANT v = qpPrice.Close(-89);
	float f = v.fltVal;
	
	_gcvt(f,4,s);

	CString *myString = new CString(s);

	//MessageBox(myString->GetBuffer(0));
	m_price = myString->GetBuffer(0);
	UpdateData(FALSE);

	free(s);


	//Close down
	//CDialog::OnOK();
}
