#pragma once
#include "DuiLib\UIlib.h"
using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib\\DuiLib_ud.lib")
#   else
#       pragma comment(lib, "DuiLib\\DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib\\DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib\\DuiLib.lib")
#   endif
#endif

#include "CChart\Chart.h"
#if defined(_UNICODE) || defined(UNICODE)
#	pragma comment(lib,"CChart\\CChartu.lib")
#else
#	pragma comment(lib,"CChart\\CChart.lib")
#endif
using namespace NsCChart;

class CWndUI: public CControlUI
{
public:
    CWndUI(): m_hWnd(NULL){}
 
    virtual void SetInternVisible(bool bVisible = true)
    {
        __super::SetInternVisible(bVisible);
        ::ShowWindow(m_hWnd, bVisible);
    }
 
    virtual void SetPos(RECT rc)
    {
        __super::SetPos(rc);
        ::SetWindowPos(m_hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOACTIVATE);
    }
 
    BOOL Attach(HWND hWndNew)
    {
        if (! ::IsWindow(hWndNew))
        {
            return FALSE;
        }
 
        m_hWnd = hWndNew;
        return TRUE;
    }
 
    HWND Detach()
    {
        HWND hWnd = m_hWnd;
        m_hWnd = NULL;
        return hWnd;
    }

protected:
    HWND m_hWnd;
};

LRESULT CALLBACK ChartProc1(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	static CChart chart;

	switch (message) 
	{
	case WM_CREATE:
		chart.CreateChart(kTypeXY);
		chart.AddPoint2D(1, 1);
		chart.AddPoint2D(2, 2);
		chart.AddPoint2D(3, 2);
		chart.AddPoint2D(4, 1);
		break;
	case WM_LBUTTONDOWN:
	case WM_LBUTTONUP:
	case WM_LBUTTONDBLCLK:
	case WM_MOUSEMOVE:
	case WM_CONTEXTMENU:
	case WM_ERASEBKGND:
		chart.Interactive(hWnd, message, wParam, lParam);
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		chart.OnDraw(hWnd);
		EndPaint(hWnd, &ps);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

LRESULT CALLBACK ChartProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	static CChart chart;

	switch (message) 
	{
	case WM_CREATE:
		chart.CreateChart(kTypePie);
		chart.AddPie(2, _T("ÁõÑÒ"));
		chart.AddPie(2, _T("öÄÓ±"));
		chart.AddPie(4, _T("ÖÜÑ¸"));
		chart.AddPie(8, _T("Íõ·Æ"));
		break;
	case WM_LBUTTONDOWN:
	case WM_LBUTTONUP:
	case WM_LBUTTONDBLCLK:
	case WM_MOUSEMOVE:
	case WM_CONTEXTMENU:
	case WM_ERASEBKGND:
		chart.Interactive(hWnd, message, wParam, lParam);
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		chart.OnDraw(hWnd);
		EndPaint(hWnd, &ps);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

ATOM MyRegisterWnd(TCHAR *szClass, WNDPROC proc)
{	
	WNDCLASS wcls;
	// check to see if class already registered
	if (GetClassInfo(GetModuleHandle(NULL), szClass, &wcls))
	{
		return 1;// name already registered - ok if it was us
	}
	// Use standard "button" control as a template.
	GetClassInfo(NULL, _T("button"), &wcls);
	// set new values
	wcls.style |= CS_DBLCLKS; // Make it to receive double clicks
	wcls.lpfnWndProc = proc;
	wcls.hInstance = GetModuleHandle(NULL);
	wcls.lpszClassName = szClass;
	
	return RegisterClass(&wcls);
}

class CDuiFrameWnd : public WindowImplBase
{
public:
    virtual LPCTSTR    GetWindowClassName() const   {   return _T("DUIMainFrame");  }
    virtual CDuiString GetSkinFile()                {   return _T("duilib.xml");  }
    virtual CDuiString GetSkinFolder()              {   return _T("");  }

	virtual CControlUI* CreateControl(LPCTSTR pstrClassName)
    {
        if (_tcsicmp(pstrClassName, _T("Wnd1")) == 0)
        {
            CWndUI  *pUI  = new CWndUI;            
            HWND    hWnd  = CreateWindow(_T("mychart1"), _T("win32"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 0, 0, 0, 0, m_PaintManager.GetPaintWindow(), NULL, NULL, NULL);
            pUI->Attach(hWnd);  
 
            return pUI;
        }
		else if (_tcsicmp(pstrClassName, _T("Wnd2")) == 0)
        {
            CWndUI  *pUI  = new CWndUI;            
            HWND    hWnd  = CreateWindow(_T("mychart2"), _T("win32"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 0, 0, 0, 0, m_PaintManager.GetPaintWindow(), NULL, NULL, NULL);
            pUI->Attach(hWnd);  
 
            return pUI;
        }
        //return NULL;
		return WindowImplBase::CreateControl(pstrClassName);
    }
};

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    CPaintManagerUI::SetInstance(hInstance);

	MyRegisterWnd(_T("mychart1"), ChartProc1);
	MyRegisterWnd(_T("mychart2"), ChartProc2);

    CDuiFrameWnd duiFrame;
    duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
    duiFrame.CenterWindow();
    duiFrame.ShowModal();
    return 0;
}