#include <stddef.h>
#include <assert.h>
#include <string.h>

#include "sysdep.h"
#include "verb.h"
#include "darray.h"

#include "actionP.h"

AlAction AlAction_create(verb, args)
     AlVerb verb;
     Darray args;
{
  AlAction_rep *temp = create();
  unsigned int i, num_args;
  Darray temp_darray;
  char *temp_str;

  assert(verb != NULL && args != NULL);

  num_args = Darray_len(args);
  assert(num_args >= AlVerb_min_args(verb) && num_args <= AlVerb_max_args(verb));

  temp->verb = verb;
  temp_darray = Darray_create();
  Darray_hint(temp_darray, (unsigned int)0, num_args);
  for (i=0; i < num_args; ++i) {
    char *cur_arg = Darray_get(args, i);
    temp_str = (char *)Memory_allocate(strlen(cur_arg)+1);
    strcpy(temp_str, cur_arg);
    Darray_addh(temp_darray, temp_str);
  }
  temp->args = temp_darray;
  return raise(temp);
}

AlVerb AlAction_verb(action)
     AlAction action;
{
  assert(action != NULL);
  return lower(action)->verb;
}

Darray AlAction_args(action)
     AlAction action;
{
  assert(action != NULL);
  return lower(action)->args;
}

NORET AlAction_destroy(action)
     AlAction action;
{
  unsigned int i, num_args;
  Darray the_darray;

  assert(action != NULL);
  the_darray = lower(action)->args;
  num_args = Darray_len(the_darray);
  for (i=0; i < num_args; ++i)
    Memory_free(Darray_get(the_darray, i));
  Darray_destroy(the_darray);
  destroy(lower(action));
}

AlAction AlAction_copy(action)
     AlAction action;
{
  assert(action != NULL);
  return AlAction_create(AlAction_verb(action), AlAction_args(action));
}

