/* Swfdec
 * Copyright (C) 2007 Pekka Lampila <pekka.lampila@iki.fi>
 *
 * This library 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.1 of the License, or (at your option) any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include "swfdec_as_internal.h"
#include "swfdec_as_native_function.h"
#include "swfdec_as_strings.h"
#include "swfdec_debug.h"
#include "swfdec_player_internal.h"
#include "swfdec_player_scripting.h"
#include "swfdec_xml.h"
#include "swfdec_sandbox.h"

SWFDEC_AS_NATIVE (14, 0, swfdec_external_interface_addCallback)
void
swfdec_external_interface_addCallback (SwfdecAsContext *cx,
    SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
    SwfdecAsValue *ret)
{
  //SwfdecPlayerPrivate *priv = SWFDEC_PLAYER (cx)->priv;
  SwfdecAsObject *obj=0;
  SwfdecAsObject *fun=0;
  const char *name=0;
  SwfdecPlayerScripting *scripting = SWFDEC_PLAYER (cx)->priv->scripting;
  SwfdecPlayerScriptingClass *klass;

  SWFDEC_AS_VALUE_SET_BOOLEAN (ret, FALSE);
  SWFDEC_AS_CHECK (0, NULL, "soo", &name, &obj, &fun);

  /* FIXME: do we allow setting if scripting is unsupported? */
  SWFDEC_FIXME("Arg number %d, Sandbox %p",argc,swfdec_sandbox_get (SWFDEC_PLAYER (cx)));
  if (!SWFDEC_IS_AS_FUNCTION (fun->relay))
  {
    SWFDEC_WARNING("Parameter is not a function");
    return;
  }

  if (scripting == NULL)
  {
    SWFDEC_WARNING("Player has no scripting support");
    return;
  }
  klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
  if (klass->addCallback) {
    klass->addCallback (scripting, name, obj, fun);
  }
  //g_hash_table_insert(priv->scripting_callbacks,fun,obj);
  SWFDEC_AS_VALUE_SET_BOOLEAN (ret, TRUE);
}

SWFDEC_AS_NATIVE (14, 1, swfdec_external_interface_call)
void
swfdec_external_interface_call (SwfdecAsContext *cx,
    SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
    SwfdecAsValue *ret)
{
  SwfdecPlayerScripting *scripting =  SWFDEC_PLAYER (cx)->priv->scripting;
  SwfdecPlayerScriptingClass *klass;
  const char *s;
  
  SWFDEC_AS_VALUE_SET_NULL (ret);
  SWFDEC_AS_CHECK (0, NULL, "s", &s);
  
  if (scripting == NULL)
    return;
  klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
  if (klass->call) {
    klass->call (scripting, s, argc-1,argv+1,ret);
  }
}

SWFDEC_AS_NATIVE (14, 100, swfdec_external_interface_get_available)
void
swfdec_external_interface_get_available (SwfdecAsContext *cx,
    SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
    SwfdecAsValue *ret)
{
  SWFDEC_AS_VALUE_SET_BOOLEAN (ret, SWFDEC_PLAYER (cx)->priv->scripting != NULL);
}

SWFDEC_AS_NATIVE (14, 101, swfdec_external_interface_set_available)
void
swfdec_external_interface_set_available (SwfdecAsContext *cx,
    SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
    SwfdecAsValue *ret)
{
  /* read-only property */
}
