/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#pragma once
#include "../libs.h"
#include "../engine/engine.h"

/** Implies that the arguments the function will take is variable */
#define SCRIPTER_VARIABLE_ARGUMENT_COUNT DUK_VARARGS

/** Global context defintion of the pointer referring back to the scripter */
#define SCRIPTER_SELF_NAME "__scripter"

/** Type forwarders */
typedef duk_context scriptercontext_t;
typedef duk_ret_t scripterreturn_t;

typedef struct {
  duk_context *context;
  engine_t *engine;
  void *user;
} scripter_t;

typedef scripterreturn_t scriptermethod_t(scriptercontext_t *context);

/**
 * Initialize the scripter engine.
 * 
 * @param scripter Scripter engine to initialize.
 * @param engine Game engine to use.
 */
void scripterInit(scripter_t *scripter, engine_t *engine);

/**
 * Dispose a previously created scripter instance.
 * 
 * @param scripter Scripter to dispose.
 */
void scripterDispose(scripter_t *scripter);

/**
 * Retreive the scripter instance frm a scripter context.
 * 
 * @param ctx Scripter context.
 * @return Pointer to the scripter instance.
 */
scripter_t * scripterFromContext(scriptercontext_t *ctx);

/**
 * Define a method onto the global scripter stack.
 * 
 * @param scripter Scripter to define onto.
 * @param name Name of the method.
 * @param argCount Arguments that the method takes.
 * @param method Pointer to the method to receive the callback.
 */
void scripterDefineMethod(scripter_t *scripter,
  char *name, int32_t argCount, scriptermethod_t *method
);

/**
 * Invoke a method (without arguments) off the global stack.
 * 
 * @param scripter Scripter to invoke frmo
 * @param method Method to invoke.
 * @return True if successful, otherwise false.
 */
bool scripterInvokeMethodSimple(scripter_t *scripter, char *method);
