//This code is copyright Andrew Story (Kaslai/Aslai) All rights reserved.
//This and derivative works can be distributed as seen fit,
//so long as this notice remains unchanged and present

#include "engine/kau.h"
#include "engine/types.h"

std::vector<KAU*> kaus;
std::map<const char*, int, map_char> kau_map;

void KAU::play( )
{
    chip->play_queue( data );
}

void KAU::play( int vol )
{
    chip->play_queue( data );
    chip->volume_queue( data, vol );
}

void KAU::volume( int vol )
{
    chip->volume_queue( data, vol );
}

void KAU::stop()
{
    chip->stop_queue( data );
}

void KAU::pause()
{
    chip->pause_queue( data );
}

KAU::KAU( char* fname, Context* prnt, Chiptunes* chi, char* ID )
{
    QUEUE a = chi->load_kau_pt( fname );
    data = a;
    parent = prnt;
    chip = chi;
    id = (char*) malloc( strlen( ID ) + 1 );
    strcpy( id, ID );
}

KAU::KAU( void* f, int len, Context* prnt, Chiptunes* chi, char* ID )
{
    QUEUE a = chi->load_kau_pt_mem( f, len );
    data = a;
    parent = prnt;
    chip = chi;
    id = (char*) malloc( strlen( ID ) + 1 );
    strcpy( id, ID );
}

KAU::~KAU()
{
    if( id != 0 )
        free( id );
}


KAU* kau_add( char* fname, Context* prnt, Chiptunes* chi, char* ID )
{
    KAU* a = new KAU( fname, prnt, chi, ID );
    int pos = kaus.size();
    kaus.push_back( a );
    kau_map[a->id] = pos;
    a->pos = pos;
    return a;
}

KAU* kau_add( void* f, int len, Context* prnt, Chiptunes* chi, char* ID )
{
    KAU* a = new KAU( f, len, prnt, chi, ID );
    int pos = kaus.size();
    kaus.push_back( a );
    kau_map[a->id] = pos;
    a->pos = pos;
    return a;
}

void kau_rem( KAU* kau )
{
    delete kaus[kau->pos];
    kaus[kau->pos] = 0;
}

KAU* kau_get_by_id( char* ID )
{
    return kaus[kau_map[ID]];
}