/* object OBJID_AUDIO */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "obj-audio.h"

unsigned int objnew_AUDIO(void *);

static void f_free(void *, void *);
static int f_data(void *, struct VG_Object *, void *);


/* export-function to create a new object-instance of OBJID_AUDIO
 * @param vgame   private structure of the game
 * @return  instance-ID or 0 = error
 */
unsigned int
objnew_AUDIO(void *vgame)
{
  struct s_game *sgame = (struct s_game *)vgame;
  struct VG_Object *objp;
  struct sobj_audio *objvars;

  if (sgame == NULL) { return 0; }

  /* create private struct */

  objvars = calloc(1, sizeof(*objvars));
  if (objvars == NULL) { return 0; }

  /* load audios */
  objvars->a_portal = vg4->audio->load("files/audio/portal.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_autsch = vg4->audio->load("files/audio/autsch.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_heart = vg4->audio->load("files/audio/heart.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_hunterdead = vg4->audio->load("files/audio/hunterdead.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_gold = vg4->audio->load("files/audio/gold.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_pill = vg4->audio->load("files/audio/pill.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_keyhole = vg4->audio->load("files/audio/keyhole.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_key = vg4->audio->load("files/audio/key.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_bombs = vg4->audio->load("files/audio/bombs.wav", 100, VG_AUDIO_VOLUME_SOUND);
  objvars->a_playerdead = vg4->audio->load("files/audio/playerdead.wav", 100, VG_AUDIO_VOLUME_SOUND);

  /* create object-instance */
  objp = vg4->object->create(OBJID_AUDIO, 0, 0, 1, objvars);
  objp->f_free = f_free;
  objp->f_data = f_data;

  return objp->instanceid;
}


/* free private struct of object-instance, called from vg4->object->destroy() */
static void
f_free(void *vgame, void *opriv)
{
  struct sobj_audio *objvars = (struct sobj_audio *)opriv;

  (void)vgame;

  /* free private struct */
  vg4->audio->unload(objvars->a_portal);
  vg4->audio->unload(objvars->a_autsch);
  vg4->audio->unload(objvars->a_heart);
  vg4->audio->unload(objvars->a_hunterdead);
  vg4->audio->unload(objvars->a_gold);
  vg4->audio->unload(objvars->a_pill);
  vg4->audio->unload(objvars->a_keyhole);
  vg4->audio->unload(objvars->a_key);
  vg4->audio->unload(objvars->a_bombs);
  vg4->audio->unload(objvars->a_playerdead);
  free(objvars);
}


/* get/set data from/for object-instance, called from vg4->object->call_data() */
static int
f_data(void *vgame, struct VG_Object *objp, void *vptr)
{
  struct s_game *sgame = (struct s_game *)vgame;
  struct sobj_audio *objvars = (struct sobj_audio *)objp->opriv;
  VG_BOOL *do_start = (VG_BOOL *)vptr;

  (void)objvars;

  if (sgame == NULL) { return 0; }

  if (do_start == NULL) {
    vg4->audio->unload(sgame->a_music);
    sgame->a_music = 0;
  } else if (*do_start) {
    char buf[128];
    snprintf(buf, sizeof(buf), "files/audio/music/%d.wav", sgame->svdt.dnew.gamelevel + 1);
    sgame->a_music = vg4->audio->load(buf, 40, VG_AUDIO_VOLUME_MUSIC);
    vg4->audio->play(sgame->a_music, VG_TRUE, VG_TRUE);
  } else {
    vg4->audio->stop(sgame->a_music, VG_TRUE);
    while (vg4->audio->is_playing(sgame->a_music, NULL)) { vg4->misc->wait_time(100); }
    vg4->audio->unload(sgame->a_music);
    sgame->a_music = 0;
  }

  return 1;
}
