
#include "soli.h"

#ifndef PandaWave_RESOURCES
#include "RevengeResources.h"
#endif

#include "RevengeApp.h"
#include <XMessages.h>
#include "tagpick.h"
#include <assert.h>
#include <XPopup.h>
#include <XWindow.h>


// ==============================  BYTE SWAPPING DATA  ========================================
static byte_swap_code sound_list_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,
	
	// short number_of_sounds;
	_2byte,
	
	// short unused[7];
	7*sizeof(short),
	
	// struct sound_list_entry sounds[MAXIMUM_SOUNDS_PER_SOUND_LIST_DEFINITION];
	
		_begin_bs_array, MAXIMUM_SOUNDS_PER_SOUND_LIST_DEFINITION,
		
		// file_tag tag;
		_4byte,
		
		// short unused[1];
		// short index;
		2*sizeof(short),
		
		_end_bs_array,
	
	_end_bs_array
};

sound_list_definition default_soli =
{
	0,
	0, 0, 0, 0, 0, 0, 0,
	// sound list entries
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0,
	-1, 0, 0
};

// ==============================  EDIT WINDOW  ========================================
void edit_soli( tag_entry* e )
{
	TsoliEditForm* form = new TsoliEditForm( e );
	form->AttachWindow( 1022 );
}

void defaultnew_soli( tag_entry *e )
{
	sound_list_definition def;
	byte_swap_move_be( "soli", &def, &default_soli, sizeof(sound_list_definition), 1, sound_list_definition_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_soli( tag_entry *e )
{
	sound_list_definition def;
	read_sound_list_definition( &def, e );
	for (int i = 0; i < def.number_of_sounds; ++i)
	{
		add_tag_dependency( 'soun', def.sounds[i].tag );
	}
}

// ==============================  READ TAG DATA  ========================================
bool read_sound_list_definition( sound_list_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "soli", e, def, SIZEOF_STRUCT_SOUND_LIST_DEFINITION, 1, sound_list_definition_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_sound_list_definition( sound_list_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "soli", e, def, SIZEOF_STRUCT_SOUND_LIST_DEFINITION, 1, sound_list_definition_byte_swapping_data);
}


// ==============================  READ TAG DATA (WITH ID) ========================================
bool read_sound_list_definition( sound_list_definition* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'soli', id );
	if( 0 == e ) {
		return FALSE;
	}
	
	return read_sound_list_definition( def, e );	
}


// ==============================  SAVE TAG DATA (WITH ID) ========================================
bool save_sound_list_definition( sound_list_definition* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'soli', id );
	if( 0 == e ) {
		return FALSE;
	}

	return save_sound_list_definition( def, e );
}


// ==============================  DISPATCH  ========================================
BEGINDISPATCH(TsoliEditForm,TTagEditForm)
	DISPATCHTABLE(KXGControlClick,TsoliEditForm::DoControlClick)
ENDDISPATCH


TsoliEditForm::TsoliEditForm( struct tag_entry *e ) : TTagEditForm( e )
{
}

// ==============================  INITIALIZE FORM =======================================
void TsoliEditForm::DoFormInitialize()
{
	TTagEditForm::DoFormInitialize();

}


// ==============================  INPUT HANDLING  ========================================
long TsoliEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif
/*
	tag_entry* e;
	tag new_id;
	
	switch( arg )
	{
		case 10001:
			// Choose a tag
			new_id = pick_tag_of_type('soun', GetTag( 10001 ));
			if(new_id != 0) {
				SetTag( 10001, 'soun', new_id );
			}
		break; 
		
		case 10002:
			// Open tag window
			e = get_entry('soun', GetTag( 10001 ) );
			if(e) {
				TRevengeApp::OpenItem( e );
			}
		break;
		
		default:
			return -1;
	}
*/	
	return -1;
}

		
// ========================================  SAVE TO TAG  ==================================================
void TsoliEditForm::CommitToTag()
{
	GetValues();
	save_sound_list_definition( &def, fEntry );
}


// ========================================  RELOAD TAG  ==================================================
void TsoliEditForm::ReloadTag()
{
	read_sound_list_definition( &def, fEntry );
	SetValues();
}


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TsoliEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "soli", fEntry, &def, SIZEOF_STRUCT_SOUND_LIST_DEFINITION, 1, sound_list_definition_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TsoliEditForm::SetValues()
{
/*
	SetTag( 10001, 'soun', def.sound_tags[0] );
	if(def.flags == _ambient_sound_background_sound) {
		SetFlag( 10003, FALSE );
	} else {
		SetFlag( 10003, TRUE );
	}
		
	SetShortWorldDistance( 10005, def.inner_radius );
	SetShortWorldDistance( 10006, def.outer_radius );
	SetFloat( 10007, def.period_lower_bound / (float)TICKS_PER_SECOND, 3 );
	SetFloat( 10008, (def.period_lower_bound + def.period_delta) / (float)TICKS_PER_SECOND, 3 );
	SetShortWorldDistance( 10009, def.random_sound_radius );
*/
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TsoliEditForm::GetValues()
{
/*
	def.sound_tags[0] = GetTag( 10001 );
	if(GetFlag( 10003 )) {
		def.flags = _ambient_sound_random_sound;
	} else {
		def.flags = _ambient_sound_background_sound;
	}
	
	def.inner_radius = GetShortWorldDistance( 10005 );
	def.outer_radius = GetShortWorldDistance( 10006 );
	def.period_lower_bound = GetFloat( 10007 ) * (float)TICKS_PER_SECOND;
	def.period_delta = (GetFloat( 10008 ) * (float)TICKS_PER_SECOND) - (float)def.period_lower_bound;
	def.random_sound_radius = GetShortWorldDistance( 10009 );
*/
}

