
#include "game.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 game_parameters_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,

	// 	file_tag tutorial_map;
	_4byte,
	
	// file_tag first_map;
	_4byte,
	
	// file_tag default_netmap;
	_4byte,

	// short unused[10];
	sizeof(short) * 10,
	
	_end_bs_array
};

game_parameters_definition default_game =
{
	-1,
	-1,
	-1,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

// ==============================  EDIT WINDOW  ========================================
void edit_game( tag_entry* e )
{
	TgameEditForm* form = new TgameEditForm( e );
	form->AttachWindow( 161 );
}

void defaultnew_game( tag_entry *e )
{
	game_parameters_definition def;
	byte_swap_move_be( "game", &def, &default_game, sizeof(game_parameters_definition), 1, game_parameters_definition_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_game( tag_entry *e )
{
	game_parameters_definition def;
	read_game_parameters_definition( &def, e );
	add_tag_dependency( 'mesh', def.training_map );
	add_tag_dependency( 'mesh', def.first_map );
	add_tag_dependency( 'mesh', def.default_netmap );
}

// ==============================  READ TAG DATA  ========================================
bool read_game_parameters_definition( game_parameters_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "game", e, def, SIZEOF_STRUCT_GAME_PARAMETERS_DEFINITION, 1, game_parameters_definition_byte_swapping_data );
}


// ==============================  SAVE TAG DATA  ========================================
bool save_game_parameters_definition( game_parameters_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "game", e, def, SIZEOF_STRUCT_GAME_PARAMETERS_DEFINITION, 1, game_parameters_definition_byte_swapping_data );
}


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


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

	return save_game_parameters_definition( def, e );
}


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


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

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

}


// ==============================  INPUT HANDLING  ========================================
long TgameEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif

	tag_entry *e;
	tag new_id;

	switch( arg )
	{
		case 100:
		{
			// Choose a tag
			new_id = pick_tag_of_type( 'mesh', GetTag( 100 ), fEntry  );
			if( new_id != 0 )
				SetTag( 100, 'mesh', new_id );
			
		}
		break; 
		
		case 101:
		{
			// Open tag window
			e = get_entry( 'mesh', GetTag( 100 ) );
			if( e )
				TRevengeApp::OpenItem( e );
				
		break;
		}
		
		case 102:
		{
			// Choose a tag
			new_id = pick_tag_of_type( 'mesh', GetTag( 102 ), fEntry  );
			if( new_id != 0 )
				SetTag( 102, 'mesh', new_id );
			
		}
		break; 

		case 103:
		{
			// Open tag window
			e = get_entry( 'mesh', GetTag( 102 ) );
			if( e )
				TRevengeApp::OpenItem( e );
				
		break;
		}
		
		case 104:
		{
			// Choose a tag
			new_id = pick_tag_of_type( 'mesh', GetTag( 104 ), fEntry  );
			if( new_id != 0 )
				SetTag( 104, 'mesh', new_id );
			
		}
		break; 

		case 105:
		{
			// Open tag window
			e = get_entry( 'mesh', GetTag( 104 ) );
			if( e )
				TRevengeApp::OpenItem( e );
				
		break;
		}
		
		default:
			return -1;
	}
	
	return 0;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TgameEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "game", fEntry, &def, SIZEOF_STRUCT_GAME_PARAMETERS_DEFINITION, 1, game_parameters_definition_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TgameEditForm::SetValues()
{
	SetTag( 100, 'mesh', def.training_map );
	SetTag( 102, 'mesh', def.first_map );
	SetTag( 104, 'mesh', def.default_netmap );
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TgameEditForm::GetValues()
{
	def.training_map = GetTag( 100 );
	def.first_map = GetTag( 102 );
	def.default_netmap = GetTag( 104 );
}


