
#include "amso.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 ambient_sound_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,
	
	// unsigned long flags;
	_4byte,
	// short_world_distance inner_radius, outer_radius; // of both sounds
	_2byte, _2byte,
	// short period_lower_bound, period_delta; // of random sound
	_2byte, _2byte,
	// file_tag sound_tags[MAXIMUM_NUMBER_OF_AMBIENT_SOUND_DEFINITION_SOUNDS];
	_begin_bs_array, MAXIMUM_NUMBER_OF_AMBIENT_SOUND_DEFINITION_SOUNDS, _4byte, _end_bs_array,
	// short_world_distance random_sound_radius;
	_2byte,
	// short unused[5];
	5 * sizeof(short),
	// short sound_indexes[MAXIMUM_NUMBER_OF_AMBIENT_SOUND_DEFINITION_SOUNDS];
	MAXIMUM_NUMBER_OF_AMBIENT_SOUND_DEFINITION_SOUNDS * sizeof(short),
	// short phase, period;
	2 * sizeof(short),
	
	_end_bs_array
};

ambient_sound_definition default_amso =
{
	0,
	0, 0,
	0, 0,
	-1, -1, -1, -1, -1, -1,
	0,
	0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0
};

// ==============================  EDIT WINDOW  ========================================
void edit_amso( tag_entry* e )
{
	TamsoEditForm* form = new TamsoEditForm( e );
	form->AttachWindow( 1000 );
}

void defaultnew_amso( tag_entry *e )
{
	ambient_sound_definition def;
	byte_swap_move_be( "amso", &def, &default_amso, sizeof(ambient_sound_definition), 1, ambient_sound_definition_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_amso( tag_entry *e )
{
	ambient_sound_definition def;
	read_ambient_sound_definition( &def, e );
	for (int i = 0; i < NUMBER_OF_AMBIENT_SOUND_DEFINITION_SOUNDS; ++i)
	{
		add_tag_dependency( 'soun', def.sound_tags[i] );
	}
}

// ==============================  READ TAG DATA  ========================================
bool read_ambient_sound_definition( ambient_sound_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "amso", e, def, SIZEOF_STRUCT_AMBIENT_SOUND_DEFINITION, 1, ambient_sound_definition_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_ambient_sound_definition( ambient_sound_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "amso", e, def, SIZEOF_STRUCT_AMBIENT_SOUND_DEFINITION, 1, ambient_sound_definition_byte_swapping_data);
}


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


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

	return save_ambient_sound_definition( def, e );
}


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


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

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

}


// ==============================  INPUT HANDLING  ========================================
long TamsoEditForm::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 ), fEntry );
			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 0;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TamsoEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "amso", fEntry, &def, SIZEOF_STRUCT_AMBIENT_SOUND_DEFINITION, 1, ambient_sound_definition_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TamsoEditForm::SetValues()
{
	if( def.sound_tags[0] != -1 )
	{
		SetTag( 10001, 'soun', def.sound_tags[0] );
		SetFlag( 10003, FALSE );
	}
	else
	{
		SetTag( 10001, 'soun', def.sound_tags[1] );
		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 TamsoEditForm::GetValues()
{
	if( !GetFlag( 10003 ) )
	{
		def.sound_tags[0] = GetTag( 10001 );
		def.sound_tags[1] = -1;
	}
	else
	{
		def.sound_tags[0] = -1;
		def.sound_tags[1] = GetTag( 10001 );
	}
	
	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 ) - GetFloat( 10007 )) * (float)TICKS_PER_SECOND;
	def.random_sound_radius = GetShortWorldDistance( 10009 );
}


