#include "EEngineFrameWork.h"


#define MAX_LOADSTRING 100

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	EEngineFrameWork* pFrameWork = EEngineFrameWork::GetSingletonPtr();

	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_LBUTTONDOWN:
		{

		}
		break;
	case WM_LBUTTONUP:
		{

		}
		break;

	case WM_COMMAND:
		{
			wmId    = LOWORD(wParam);
			wmEvent = HIWORD(wParam);
			// 메뉴 선택을 구문 분석합니다.

			CBaseControl* pControl = pFrameWork->GetControl( wmId ) ;

			if( pControl )
			{
				pFrameWork->ControlEvent( pControl, wmEvent );
			}			
		}

		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);

		pFrameWork->OnPaint( ps );

		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		pFrameWork->_DeInitFrameWork();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}




EEngineFrameWork::EEngineFrameWork( void)
{

}

EEngineFrameWork::~EEngineFrameWork(void)
{
	
}

int EEngineFrameWork::InitFrameWork( HINSTANCE hInstance, HINSTANCE hPrevInstance )
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	//	UNREFERENCED_PARAMETER(lpCmdLine);		

	//------------------------------------------------------
	// 전역 문자열을 초기화합니다.
	//------------------------------------------------------
	// 	LoadString(hInstance, IDS_APP_TITLE, m_strTitle, MAX_LOADSTRING);
	// 	LoadString(hInstance, IDC_BASEENGINE, m_strWindowClass, MAX_LOADSTRING);

 	m_strWindowClass = L"FrameWork";
 	m_strTitle = L"FrameWork";


	// 

//	m_hAccelTable = LoadAccelerators(m_hInst, MAKEINTRESOURCE(109));
	MyRegisterClass(hInstance);


	// 응용 프로그램 초기화를 수행합니다.
	if (!InitInstance (hInstance))
	{
		return FALSE;
	}

//	CreateWindow(TEXT("edit"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,10,10,200,25,m_hWnd,(HMENU)0,m_hInst,NULL);


	_InitFrameWork();
	_InitMenuControl();

	return TRUE;
}


int EEngineFrameWork::Run( void )
{
	MSG msg = {0,};

	__int64 start,end, freq;
	QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
	QueryPerformanceCounter((LARGE_INTEGER*)&start);


	// 기본 메시지 루프입니다.
	while(msg.message != WM_QUIT )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			QueryPerformanceCounter((LARGE_INTEGER*)&end);
			double msec;
			msec=(double)(end-start)/freq*1000.0;
			
			QueryPerformanceCounter((LARGE_INTEGER*)&start);
			Update(msec/1000.0);
			
		}
	}

	return (int) msg.wParam;
}



ATOM EEngineFrameWork::MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	=  m_strWindowClass.c_str();
	wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);

	return RegisterClassEx(&wcex);
}

BOOL EEngineFrameWork::InitInstance(HINSTANCE hInstance )
{


	m_hInst = hInstance; // 인스턴스 핸들을 전역 변수에 저장합니다.

	m_hWnd = CreateWindow(m_strWindowClass.c_str(), m_strTitle.c_str(), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!m_hWnd)
	{
		return FALSE;
	}

	ShowWindow(m_hWnd, SW_SHOW);
	UpdateWindow(m_hWnd);

	return TRUE;
}


//---------------------------------------------------------------------
// 컨트롤을 숨김
//---------------------------------------------------------------------
CStaticControl* EEngineFrameWork::CreateStatic( LPCWSTR aName, DWORD aStyle, int aX, int aY, int aWidth, int aHeight )
{
	CStaticControl* Temp = new CStaticControl( m_stlBaseControl.size(), aName, aX, aY, aWidth, aHeight, aStyle  );

	m_stlBaseControl.push_back( Temp );

	return Temp;
}

CButtonControl* EEngineFrameWork::CreateButton( LPCWSTR aName, DWORD aStyle, int aX, int aY, int aWidth, int aHeight  )
{
	CButtonControl* Temp = new CButtonControl( m_stlBaseControl.size(), aName, aX, aY, aWidth, aHeight, aStyle  );

	 m_stlBaseControl.push_back( Temp );

	 return Temp;
}

CEditBoxControl* EEngineFrameWork::CreateEditBox( LPCWSTR aName, DWORD aStyle, int aX, int aY, int aWidth, int aHeight  )
{
	CEditBoxControl* Temp = new CEditBoxControl( m_stlBaseControl.size(), aName, aX, aY, aWidth, aHeight, aStyle  );

	m_stlBaseControl.push_back( Temp );

	return Temp;
}

CComboBoxControl* EEngineFrameWork::CreateComboBox( LPCWSTR aName, DWORD aStyle, int aX, int aY, int aWidth, int aHeight  )
{
	CComboBoxControl* Temp = new CComboBoxControl( m_stlBaseControl.size(), aName, aX, aY, aWidth, aHeight, aStyle  );

	m_stlBaseControl.push_back( Temp );

	return Temp;
}

CListBoxControl* EEngineFrameWork::CreateListBox( LPCWSTR aName, DWORD aStyle, int aX, int aY, int aWidth, int aHeight  )
{
	CListBoxControl* Temp = new CListBoxControl( m_stlBaseControl.size(), aName, aX, aY, aWidth, aHeight, aStyle  );

	m_stlBaseControl.push_back( Temp );

	return Temp;
}

CViewControl* EEngineFrameWork::CreateView( LPCWSTR aName, DWORD aStyle, int aX, int aY, int aWidth, int aHeight )
{
	CViewControl* Temp = new CViewControl( m_stlBaseControl.size(), aName, aX, aY, aWidth, aHeight, aStyle  );
	m_stlBaseControl.push_back( Temp );

	return Temp;
}



CBaseControl* EEngineFrameWork::GetControl( int aIdx )
{
	for( int Index = 0 ; Index < (int)m_stlBaseControl.size() ; ++Index )
	{
		if( m_stlBaseControl[Index]->m_nIdx == aIdx )
		{
			return m_stlBaseControl[Index];
		}
	}

	return NULL;
}
