
#include "core.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>

#include <XDraw.h>
#include <XImageControls.h>
#include <XScroll.h>


// ==============================  BYTE SWAPPING DATA  ========================================
static byte_swap_code collection_reference_byte_swapping_data[]=
{
	_begin_bs_array, 1,
	
	// file_tag collection_tag;
	_4byte,

	// short number_of_permutations;
	// short_fixed tint_fraction;
	_2byte,
	_2byte,

	// struct rgb_color colors[MAXIMUM_PERMUTATIONS_PER_COLLECTION_REFERENCE][MAXIMUM_HUE_CHANGES_PER_COLLECTION];
	
		_begin_bs_array, MAXIMUM_PERMUTATIONS_PER_COLLECTION_REFERENCE,
		
			_begin_bs_array, MAXIMUM_HUE_CHANGES_PER_COLLECTION,
			
			// word red, green, blue;
			// word flags;
			_2byte, _2byte, _2byte,
			_2byte,
			
			_end_bs_array,
		
		_end_bs_array,

	// struct rgb_color tint_color;
	_2byte, _2byte, _2byte, _2byte,

	// short unused[18];
	18*sizeof(short),

	// short collection_index;
	sizeof(short),
	
	// short color_table_indexes[MAXIMUM_PERMUTATIONS_PER_COLLECTION_REFERNECE];
	MAXIMUM_PERMUTATIONS_PER_COLLECTION_REFERENCE*sizeof(short),
	
	_end_bs_array
};


// ==============================  EDIT WINDOW  ========================================
void edit_core( tag_entry* e )
{
	TcoreEditForm* form = new TcoreEditForm( e );
	form->AttachWindow( 1002 );
}


void dependency_core( tag_entry *e )
{
	collection_reference def;
	read_collection_reference( &def, e );
	add_tag_dependency( '.256', def.collection_tag );
}


// ==============================  READ TAG DATA  ========================================
bool read_collection_reference( collection_reference* def, tag_entry* e )
{
	return load_tag_data_into_struct( "core", e, def, SIZEOF_STRUCT_COLLECTION_REFERENCE, 1, collection_reference_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_collection_reference( collection_reference* def, tag_entry* e )
{
	return save_tag_data_from_struct( "core", e, def, SIZEOF_STRUCT_COLLECTION_REFERENCE, 1, collection_reference_byte_swapping_data);
}


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


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

	return save_collection_reference( def, e );
}


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


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

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

}


// ==============================  INPUT HANDLING  ========================================
long TcoreEditForm::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 TcoreEditForm::CommitToTag()
{
	GetValues();
	save_collection_reference( &def, fEntry );
}


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TcoreEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "core", fEntry, &def, SIZEOF_STRUCT_COLLECTION_REFERENCE, 1, collection_reference_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TcoreEditForm::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 TcoreEditForm::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 );
*/
}

