/*
-----------------------------------------------------------------------------
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 "scolEmbeddedWebNavigatorDisplay.h"
#include "scolEmbeddedWebNavigator.h"
#include "embeddedWebNavigatorMessageInformations.h"
#include "embeddedWebNavigatorConsoleInformations.h"

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnNavStateChange, WEB_NAVIGATOR_DISPLAY_ON_NAV_STATE_CHANGE_CB)
int FireWebNavigatorReflexiveOnNavStateChange(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnNavStateChange\n");
#endif

  int res = 0;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, wParam, WEB_NAVIGATOR_DISPLAY_ON_NAV_STATE_CHANGE_CB))
    res = OBJcallreflex(m, 0);

  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnAddressChange, WEB_NAVIGATOR_DISPLAY_ON_ADDRESS_CHANGE_CB)
int FireWebNavigatorReflexiveOnAddressChange(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnAddressChange\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::MessageInformations* message = (Scol::EmbeddedWebNavigator::MessageInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_DISPLAY_ON_ADDRESS_CHANGE_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(OnTitleChange, WEB_NAVIGATOR_DISPLAY_ON_TITLE_CHANGE_CB)
int FireWebNavigatorReflexiveOnTitleChange(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnTitleChange\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::MessageInformations* message = (Scol::EmbeddedWebNavigator::MessageInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_DISPLAY_ON_TITLE_CHANGE_CB))
  {
    Mpushstrbloc(m, (char*)(message->content.c_str()));
    res = OBJcallreflex(m, 1);
  }

  delete(message);
  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnTooltip, WEB_NAVIGATOR_DISPLAY_ON_TOOLTIP_CB)
int FireWebNavigatorReflexiveOnTooltip(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnTooltip\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::MessageInformations* message = (Scol::EmbeddedWebNavigator::MessageInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_DISPLAY_ON_TOOLTIP_CB))
  {
    Mpushstrbloc(m, (char*)(message->content.c_str()));
    res = OBJcallreflex(m, 1);
  }

  delete(message);
  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnStatusMessage, WEB_NAVIGATOR_DISPLAY_ON_STATUS_MESSAGE_CB)
int FireWebNavigatorReflexiveOnStatusMessage(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnStatusMessage\n");
#endif

  int res = 0;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, wParam, WEB_NAVIGATOR_DISPLAY_ON_STATUS_MESSAGE_CB))
    res = OBJcallreflex(m, 0);

  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnConsoleMessage, WEB_NAVIGATOR_DISPLAY_ON_CONSOLE_MESSAGE_CB)
int FireWebNavigatorReflexiveOnConsoleMessage(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnConsoleMessage\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::ConsoleInformations* message = (Scol::EmbeddedWebNavigator::ConsoleInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)message->parentWebNavigator, WEB_NAVIGATOR_DISPLAY_ON_CONSOLE_MESSAGE_CB))
  {
    Mpushstrbloc(m, (char*)(message->content.c_str()));
    Mpushstrbloc(m, (char*)(message->source.c_str()));
    MMpush(m, ITOM(message->line));
    res = OBJcallreflex(m, 3);
  }

  delete(message);
  return res;
}

// Nb of Scol functions or types
#define WebNavigatorDisplayScolFunctionsNb 4 //6

/**
 *	Scol function names
 */
char* WebNavigatorDisplayScolFunctionsNames[WebNavigatorDisplayScolFunctionsNb] =
{
  //"WebNavigatorCbOnNavStateChange",
  "WebNavigatorCbOnAddressChange",
  "WebNavigatorCbOnTitleChange",
  "WebNavigatorCbOnTooltip",
  //"WebNavigatorCbOnStatusMessage",
  "WebNavigatorCbOnConsoleMessage"
};

/**
 *	Pointers to C functions that manipulate the VM for each scol function previously defined
 */
int (*WebNavigatorDisplayScolFunctionsPtr[WebNavigatorDisplayScolFunctionsNb])(mmachine m)=
{
  //WebNavigatorCbOnNavStateChange,
  WebNavigatorCbOnAddressChange,
  WebNavigatorCbOnTitleChange,
  WebNavigatorCbOnTooltip,
  //WebNavigatorCbOnStatusMessage,
  WebNavigatorCbOnConsoleMessage
};

/**
 *	Nb of arguments of each scol function
 */
int WebNavigatorDisplayScolFunctionsNbArgs[WebNavigatorDisplayScolFunctionsNb]=
{
  //3,                                          // WebNavigatorCbOnNavStateChange
  3,                                          // WebNavigatorCbOnAddressChange
  3,                                          // WebNavigatorCbOnTitleChange
  3,                                          // WebNavigatorCbOnTooltip
  //3,                                          // WebNavigatorCbOnStatusMessage
  3                                           // WebNavigatorCbOnConsoleMessage
};

/**
 *	Prototypes of the scol functions
 */
char* WebNavigatorDisplayScolFunctionsType[WebNavigatorDisplayScolFunctionsNb]=
{
  //"fun [ObjWebNavigator fun [ObjWebNavigator u0 I I] u1 u0] ObjWebNavigator",    // WebNavigatorCbOnNavStateChange
  
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S S] u1 u0] ObjWebNavigator",   // WebNavigatorCbOnAddressChange
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S] u1 u0] ObjWebNavigator",     // WebNavigatorCbOnTitleChange
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S] u1 u0] ObjWebNavigator",     // WebNavigatorCbOnTooltip
  
  //"fun [ObjWebNavigator fun [ObjWebNavigator u0] u1 u0] ObjWebNavigator",       // WebNavigatorCbOnStatusMessage
  
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 S S I] u1 u0] ObjWebNavigator"  // WebNavigatorCbOnConsoleMessage
};

int LoadWebNavigatorDisplay(mmachine m)
{
  return PKhardpak(m, "WebNavigatorDisplay", WebNavigatorDisplayScolFunctionsNb, WebNavigatorDisplayScolFunctionsNames, WebNavigatorDisplayScolFunctionsPtr, WebNavigatorDisplayScolFunctionsNbArgs, WebNavigatorDisplayScolFunctionsType);
}
