#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"
#ifdef _DEBUG
#	if defined(_UNICODE) || defined(UNICODE)
#		pragma comment(lib, "CChart\\PlotDll_ud.lib")
#	else
#		pragma comment(lib, "CChart\\PlotDll_d.lib")
#	endif
#else
#	if defined(_UNICODE) || defined(UNICODE)
#		pragma comment(lib, "CChart\\PlotDll_u.lib")
#	else
#		pragma comment(lib, "CChart\\PlotDll.lib")
#	endif
#endif

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;
};

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("Wnd")) == 0)
        {
            CWndUI  *pUI  = new CWndUI;            
            HWND    hWnd  = CreateWindow(_T("ChartCtrl"), _T("win32"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 0, 0, 0, 0, m_PaintManager.GetPaintWindow(), NULL, NULL, NULL);
            pUI->Attach(hWnd);  
 
            CChart *chart = GetChart(hWnd);
			chart->AddPoint2D(1, 1);
			chart->AddPoint2D(2, 2);
			chart->AddPoint2D(3, 2);
			chart->AddPoint2D(4, 1);

            return pUI;
        }
        //return NULL;
		return WindowImplBase::CreateControl(pstrClassName);
    }
};

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

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