/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2011 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/

#include "scolEmbeddedWebNavigatorLoad.h"
#include "scolEmbeddedWebNavigator.h"
#include "embeddedWebNavigatorMessageInformations.h"
#include "embeddedWebNavigatorErrorInformations.h"

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnLoadStart, WEB_NAVIGATOR_LOAD_ON_START_CB)
int FireWebNavigatorReflexiveOnLoadStart(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnLoadStart\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::MessageInformations* message = (Scol::EmbeddedWebNavigator::MessageInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_LOAD_ON_START_CB))
  {
    Mpushstrbloc(m, (char*)(message->content.c_str()));
    res = OBJcallreflex(m, 1);
  }

  delete(message);
  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnLoadEnd, WEB_NAVIGATOR_LOAD_ON_END_CB)
int FireWebNavigatorReflexiveOnLoadEnd(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnLoadEnd\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::MessageInformations* message = (Scol::EmbeddedWebNavigator::MessageInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_LOAD_ON_END_CB))
  {
    Mpushstrbloc(m, (char*)(message->content.c_str()));
    Mpushstrbloc(m, (char*)(message->optionalContent.c_str()));
    res = OBJcallreflex(m, 2);
  }

  delete(message);
  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnLoadError, WEB_NAVIGATOR_LOAD_ON_ERROR_CB)
int FireWebNavigatorReflexiveOnLoadError(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnLoadError\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::ErrorInformations* message = (Scol::EmbeddedWebNavigator::ErrorInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_LOAD_ON_ERROR_CB))
  {
    Mpushstrbloc(m, (char*)(message->frameName.c_str()));
    Mpushstrbloc(m, (char*)(message->failedUrl.c_str()));
    MMpush(m, ITOM(message->errorCode));
    Mpushstrbloc(m, (char*)(message->errorText.c_str()));
    res = OBJcallreflex(m, 4);
  }

  delete(message);
  return res;
}

// Nb of Scol functions or types
#define WebNavigatorLoadScolFunctionsNb 3

/**
 *	Scol function names
 */
char* WebNavigatorLoadScolFunctionsNames[WebNavigatorLoadScolFunctionsNb] =
{
  "WebNavigatorCbOnLoadStart",
  "WebNavigatorCbOnLoadEnd",
  "WebNavigatorCbOnLoadError"
};

/**
 *	Pointers to C functions that manipulate the VM for each scol function previously defined
 */
int (*WebNavigatorLoadScolFunctionsPtr[WebNavigatorLoadScolFunctionsNb])(mmachine m)=
{
  WebNavigatorCbOnLoadStart,
  WebNavigatorCbOnLoadEnd,
  WebNavigatorCbOnLoadError
};

/**
 *	Nb of arguments of each scol function
 */
int WebNavigatorLoadScolFunctionsNbArgs[WebNavigatorLoadScolFunctionsNb]=
{
  3,                                          // WebNavigatorCbOnLoadStart
  3,                                          // WebNavigatorCbOnLoadEnd
  3                                           // WebNavigatorCbOnLoadError
};

/**
 *	Prototypes of the scol functions
 */
char* WebNavigatorLoadScolFunctionsType[WebNavigatorLoadScolFunctionsNb]=
{
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S] u1 u0] ObjWebNavigator",       // WebNavigatorCbOnLoadStart
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S S] u1 u0] ObjWebNavigator",     // WebNavigatorCbOnLoadEnd
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S S I S] u1 u0] ObjWebNavigator"  // WebNavigatorCbOnLoadError
};

int LoadWebNavigatorLoad(mmachine m)
{
  return PKhardpak(m, "WebNavigatorLoad", WebNavigatorLoadScolFunctionsNb, WebNavigatorLoadScolFunctionsNames, WebNavigatorLoadScolFunctionsPtr, WebNavigatorLoadScolFunctionsNbArgs, WebNavigatorLoadScolFunctionsType);
}
