// ScolPlugin.cpp : implémentation de CScolPlugin
#include "stdafx.h"
#include "ScolPlugin.h"

extern char DefDownloadURL[];
extern long DefDownloadSize;

// CScolPlugin
CScolPlugin::CScolPlugin()
{
	m_bWindowOnly = TRUE;

	//m_bInPlaceActive=FALSE;
	//m_bUIActive=TRUE;

  // Init Member vars
  m_bstrForceInstall="false";
  m_bstrDownloadURL=DefDownloadURL;
  m_bstrScolVersionNeeded="";
  m_longDownloadSize=DefDownloadSize;
  
  //sprintf(mOnScolEnd,"%s", "EndScolMachine");
  //sprintf(mOnJSMessage,"%s", "MsgFromScol");

  m_Text=SCOL_PRODUCT_NAME;
}

HRESULT CScolPlugin::OnDraw(ATL_DRAWINFO& di)
{
  // subclass window so we can intercept window messages and
  // do our drawing to it	
  SetWindowLongPtr(GWLP_WNDPROC, (LONG)PluginWinProc);  
  
  // associate window with our CPlugin object so we can access 
  // it in the window procedure
  SetWindowLongPtr(GWLP_USERDATA, (LONG)this);
	return S_OK;
}

STDMETHODIMP CScolPlugin::LaunchMachine(BSTR *CmdLine, long CmdShow, long MemSize, long *result)
{
	USES_CONVERSION;
	char * cmdl;

	cmdl=(char *)OLE2A(*CmdLine);

	// Modify Download URL
	if (m_bstrDownloadURL.Length()>0)
		set_DownloadURL(OLE2A(m_bstrDownloadURL));

	// Modify Download size
	if (m_longDownloadSize>0)
		set_DownloadSize(m_longDownloadSize);

	// Modify Version Needed
	if (m_bstrScolVersionNeeded.Length()>0)
		set_ScolVersionNeeded(OLE2A(m_bstrScolVersionNeeded));

	// Force Install if Needed
	char * charbool;
	charbool=(char *)OLE2A(m_bstrForceInstall);
  if (!stricmp("true", charbool) || !stricmp("1", charbool) || !stricmp("yes", charbool))
		ForceInstall();
  
  *result = LaunchScolMachine(cmdl, CmdShow, MemSize, m_hWndCD);

	InPlaceActivate(OLEIVERB_UIACTIVATE);

	return S_OK;
}

STDMETHODIMP CScolPlugin::SendScolMessage(BSTR *msg, long *result)
{
	char * MsgToScol;
	USES_CONVERSION;
	MsgToScol=(char *)OLE2A(*msg);

		*result=SendMessageToScol(MsgToScol);
	return S_OK;
}

///////////////////////////////////////////////////////////////////////
// Property Accessors for ScolVersionNeeded Property
STDMETHODIMP CScolPlugin::get_ScolVersionNeeded(BSTR *pVal)
{
	*pVal = m_bstrScolVersionNeeded.Copy();
	return S_OK;
}

STDMETHODIMP CScolPlugin::put_ScolVersionNeeded(BSTR newVal)
{
	m_bstrScolVersionNeeded=newVal;
	return S_OK;
}

///////////////////////////////////////////////////////////////////////
// Property Accessors for DownloadURL Property
STDMETHODIMP CScolPlugin::get_DownloadURL(BSTR *pVal)
{
	*pVal = m_bstrDownloadURL.Copy();
	return S_OK;
}

STDMETHODIMP CScolPlugin::put_DownloadURL(BSTR newVal)
{
	USES_CONVERSION;
	char * NewDownloadURL;

	m_bstrDownloadURL=newVal;

  NewDownloadURL=OLE2A(m_bstrDownloadURL);
	set_DownloadURL(NewDownloadURL);

	return S_OK;
}

/**********************************************************************************/
/* This section is here to provide a mapping C Function -> Object member function */

/**********************************************************************************/

int CScolPlugin::SendEndEventToJS()
{
  Fire_onScolEnd();
	//::MessageBox(NULL,"Scol End Message Received","DEBUG",NULL);
	return 0;
}

int CScolPlugin::SendMessageToJS(char *JSMessage)
{
  USES_CONVERSION;
  BSTR bstrJSMessage;
  bstrJSMessage = A2BSTR(JSMessage);
  Fire_onMessage(&bstrJSMessage);
  //::MessageBox(NULL,JSMessage,"VC:Message from Scol to JS",NULL);
  return 0;
}

STDMETHODIMP CScolPlugin::get_ForceInstall(BSTR *pVal)
{
	// TODO: Add your implementation code here
	*pVal=m_bstrForceInstall.Copy();
	return S_OK;
}

STDMETHODIMP CScolPlugin::put_ForceInstall(BSTR newVal)
{
	// TODO: Add your implementation code here
	m_bstrForceInstall=newVal;
	return S_OK;
}

STDMETHODIMP CScolPlugin::get_DownloadSize(long *pVal)
{
	// TODO: Add your implementation code here

	*pVal = m_longDownloadSize;
	return S_OK;
}

STDMETHODIMP CScolPlugin::put_DownloadSize(long newVal)
{
	// TODO: Add your implementation code here
  
	m_longDownloadSize = newVal;
  set_DownloadSize(newVal);
 
	return S_OK;
}

static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	CScolPlugin * inst = (CScolPlugin *)GetWindowLong(hWnd, GWL_USERDATA);
	{
		switch( msg )
		{
      case WM_SIZE:
      {
        HWND hWndChild;
        RECT fatherRect;

        hWndChild = GetWindow(hWnd, GW_CHILD);
        if (hWndChild != NULL)
        {
          GetClientRect(hWnd, &fatherRect);
          MoveWindow(hWndChild, 0, 0, fatherRect.right, fatherRect.bottom, true);
        }
        break;
      }
			case WM_PAINT:
			{
				PAINTSTRUCT paintStruct;
				HDC hdc = BeginPaint(hWnd, &paintStruct);
			  
        if(paintStruct.fErase)
          FillRect(hdc, &paintStruct.rcPaint, 
                   (HBRUSH) GetStockObject(WHITE_BRUSH)); 
        
				if(inst->m_Text)
				{
					RECT rcWnd;
					GetWindowRect(hWnd, &rcWnd);
					/*SelectObject(paintStruct.hdc, GetStockObject(WHITE_BRUSH));
          Rectangle(paintStruct.hdc,
                      paintStruct.rcPaint.left,
                      paintStruct.rcPaint.top,
                      paintStruct.rcPaint.right,
                      paintStruct.rcPaint.bottom);
          */
					SetTextAlign(hdc, TA_CENTER);
					TextOut(hdc, (rcWnd.right-rcWnd.left)/2, (rcWnd.bottom-rcWnd.top)/2, inst->m_Text, strlen(inst->m_Text));
				}
        
				EndPaint( hWnd, &paintStruct );
        break;
			}

      case WM_COPYDATA:
      {
        PCOPYDATASTRUCT cpd;
        cpd = (PCOPYDATASTRUCT)lParam;
        switch( cpd->dwData )
        {
          case WM_SCOLSETWPTR:
          {
            inst->set_ScolHWindow(*((HWND *) cpd->lpData));
            // show scol console
            //ShowWindow(inst->get_ScolHWindow(), SW_SHOW);
            break;
          }

          case WM_SCOLAXMESS:
          {
            char * myaxdata = (char *) cpd->lpData;
            /*
            char script[1024];
	          
	          if (inst->mOnJSMessage[0] == NULL)
		          sprintf(script, "%s", myaxdata);
	          else
		          sprintf(script, "%s('%s');", inst->mOnJSMessage, myaxdata);
            */
            inst->SendMessageToJS(myaxdata);
            break;
          }
        }
        break;
      }

			case WM_SCOLEND:
			{
        inst->ScolClosed();
        inst->SendEndEventToJS();
				break;
			}
		}
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}
