#include <common/calendar.hpp>
#include <common/string.hpp>
#include <common/comctlex.hpp>

CalendarControl::CalendarControl(GUIWindow &parentWnd,HWND hControlWnd,UINT controlID)
: mParentWnd(parentWnd), Control(hControlWnd,controlID)
{
  mNotifyHandler.setCallback(this,&CalendarControl::notifyHandler);
	parentWnd.insertHandler(VectorHandler::NotifyHandler,&mNotifyHandler);
  CommonControlsEx commonControlsEx;
	commonControlsEx.flags(CommonControlsEx::InitDate);
	commonControlsEx.initCommonControlsEx();
}

CalendarControl::CalendarControl(GUIWindow &parentWnd,UINT controlID,const Rect &initRect,DWORD style,DWORD styleEx)
: mParentWnd(parentWnd)
{
  CommonControlsEx commonControlsEx;
	commonControlsEx.flags(CommonControlsEx::InitDate);
	commonControlsEx.initCommonControlsEx();
  mNotifyHandler.setCallback(this,&CalendarControl::notifyHandler);
	parentWnd.insertHandler(VectorHandler::NotifyHandler,&mNotifyHandler);
  createControl(styleEx,MONTHCAL_CLASS,"",style,initRect,parentWnd,controlID);
}

CalendarControl::~CalendarControl()
{
}

bool CalendarControl::resize(void)
{
  Rect minRect;
  
	if(!isValid())return false;
	getMinReqRect(minRect);
	width(minRect.right());
	height(minRect.bottom());
	return true;
}

CallbackData::ReturnType CalendarControl::notifyHandler(CallbackData &someCallbackData)
{
  if(controlID()!=someCallbackData.wParam())return FALSE;
	NMHDR *pNotifyHeader=(NMHDR*)someCallbackData.lParam();
	switch(pNotifyHeader->code)
	{
	  case MCN_SELCHANGE :
		  {
			  NMSELCHANGE *pSelChange=((NMSELCHANGE*)someCallbackData.lParam());
			  handleSelChange(*pSelChange);
		    mSelChangeHandler.callback(CallbackData(0,(LPARAM)pSelChange));
		  }
		case MCN_GETDAYSTATE :
		  {
			  NMDAYSTATE *pDayState=((NMDAYSTATE*)someCallbackData.lParam());
		    handleDayState(*pDayState);
				mDayStateHandler.callback(CallbackData(0,(LPARAM)pDayState));
			}
		case MCN_SELECT :
		  {
			  NMSELCHANGE *pSelChange=((NMSELCHANGE*)someCallbackData.lParam());
		    handleSelect(*((NMSELCHANGE*)someCallbackData.lParam()));
				mSelectHandler.callback(CallbackData(0,(LPARAM)pSelChange));
			}
		case MCN_FIRST :
		  break;
		case MCN_LAST :
		  break;

	}
  return FALSE;
}

// virtuals

bool CalendarControl::handleSelChange(NMSELCHANGE &nmSelChange)
{
  return false;
}

bool CalendarControl::handleDayState(NMDAYSTATE &nmDayState)
{
  return false;
}

bool CalendarControl::handleSelect(NMSELCHANGE &nmSelChange)
{
  return false;
}


