/*************************************************************************

	form*Z by auto*des*sys, Inc.
	Copyright (c) 1989-2004 auto*des*sys, Inc.  All rights reserved.

	File:			UUID_GEN.c
	Author:			auto*des*sys, Inc.
	Created on:		02/23/04
	Description:	UUID_GEN project utility plugin.
					Changes color of all objects in scene to plain-colored
					objects in the spectrum of a UUID_GEN according to
					their locations.

*************************************************************************/

/* this plugin's header */

#include "uuid_gen.h"

/* required for plugin's source file implementing plugin entry function */

#include "fz_plugin_glue_api.h"

/* function called by formZ to fill a utility's function set */
/* with pointers to UUID_GEN's own callback functions. */
static fzrt_error_td uuid_gen_fill_fset(const fzpl_fset_def_ptr fset_def, fzpl_fset_td* const fset);
static fzrt_error_td uuid_gen_fuim_tmpl(fz_fuim_tmpl_ptr fuim_tmpl);
short uuid_gen_cust_func( fz_fuim_tmpl_ptr  fuim_tmpl, short indx ,fz_fuim_actn_cust_enum  action , fz_type_td *action_data , void *func_data ,void ** prvt_data);

#define  UUID_GEN_FUIM_TITLE	3
#define UUID_GENERATE_STR		4
#define	UUID_GEN_TRADITIONAL_STRING 5
#define UUID_GEN_FORMZ_STRING 6

enum uuid_gen_item_enum {
	UUID_GEN_GENERATE_BUTTON = 0,
		UUID_GEN_TRADITIONAL_EDIT,
		UUID_GEN_FORMZ_EDIT,
	UUID_GEN_ID_MAX
};
enum uuid_gen_stack_enum {
	UUID_GEN_FUIM_STACK_TRADITIONAL = 0,
	UUID_GEN_FUIM_STACK_FORMZ	,
	UUID_GEN_STACK_MAX
};
/************************************************************************
						UUID_GEN Data Structures
*************************************************************************/


/************************************************************************
							UUID_GEN Callbacks
*************************************************************************/

/************************************************************************
	uuid_gen_uuid
		Tells formZ what this plugin's uuid is.
*************************************************************************/
static fzrt_error_td uuid_gen_uuid(fzrt_UUID_td uuid)
/************************************************************************/
{
	_base_funcs.fzrt_UUID_copy(UUID_GEN_PLUGIN_UUID, uuid);
	return(FZRT_NOERR);
}

/************************************************************************
	uuid_gen_name
		Tells formZ what the display name/title of this plugin is.
*************************************************************************/
static fzrt_error_td uuid_gen_name(char* name, fzrt_int max_len)
/************************************************************************/
{
	strncpy(name, _short_name, max_len);
	return(FZRT_NOERR);
}



/************************************************************************
	item callback for the uuid generate dialog
*************************************************************************/
static short uuid_gen_fuim_item_func(fz_fuim_tmpl_ptr fuim_tmpl, fzrt_int action, short item_id, void *func_data, fz_fuim_type_td *rv_data)
{
	short						rv = FALSE;
	fzrt_UUID_td				newuuid;
	fzrt_error_td				err = FZRT_NOERR;
	char						str[256];
	
	switch (action)
	{
		case FZ_FUIM_ACTN_HIT :
			switch ( item_id )
			{
				case UUID_GEN_GENERATE_BUTTON :
					err = _base_funcs.fzrt_UUID_create(newuuid);
					if (err == FZRT_NOERR) {
						err = _base_funcs.fzrt_UUID_to_string(newuuid, TRUE, str, sizeof(str));
						if (err == FZRT_NOERR) _fuim_funcs.fz_fuim_stack_put(fuim_tmpl, UUID_GEN_FUIM_STACK_TRADITIONAL, sizeof(str),&str);
						err = _base_funcs.fzrt_UUID_to_string(newuuid, FALSE, str, sizeof(str));
						if (err == FZRT_NOERR) _fuim_funcs.fz_fuim_stack_put(fuim_tmpl, UUID_GEN_FUIM_STACK_FORMZ, sizeof(str),&str);
					}
					rv = TRUE;
				break;
			}
		break;
	}
	
	return (rv);
}

/************************************************************************
	custom fuim uuid item function
************************************************************************/
short uuid_gen_cust_func( fz_fuim_tmpl_ptr  fuim_tmpl, short indx ,fz_fuim_actn_cust_enum  action , fz_type_td *action_data , void *func_data ,void ** prvt_data )  
{
	short			rv = FALSE;
	short			text_height;

	switch (action)
	{
		case FZ_FUIM_ACTN_CUST_INIT:
			break;
		case FZ_FUIM_ACTN_CUST_GET_HEIGHT_MIN :
			case FZ_FUIM_ACTN_CUST_GET_HEIGHT:
			text_height = 1;
			_type_funcs.fz_type_set_short(&text_height,action_data);
			rv = TRUE;
			break;
			case FZ_FUIM_ACTN_CUST_GET_WIDTH_MIN:
			case FZ_FUIM_ACTN_CUST_GET_WIDTH:
			text_height = 300;
			_type_funcs.fz_type_set_short(&text_height,action_data);
			rv = TRUE;
			break;
		case FZ_FUIM_ACTN_CUST_REDRAW:
			
		case FZ_FUIM_ACTN_CUST_FINIT :
			break;
			
		default:
			break;
	}
	return (rv);
}

/************************************************************************
	KDA 04/19/04: New Function 
************************************************************************/

static fzrt_error_td uuid_gen_fuim_tmpl(fz_fuim_tmpl_ptr fuim_tmpl)
{
	fzrt_error_td				err = FZRT_NOERR;
	short						tindx, g1,g2,g3;
	char						str[256];
	char						*edit_str;
	fzrt_UUID_td				newuuid;
	

	_base_funcs.fzrt_fzr_get_string(_resource_ref, 1, UUID_GEN_FUIM_TITLE, str);
	if((tindx = _fuim_funcs.fz_fuim_tmpl_init(fuim_tmpl, str, 0, UUID_GEN_FUIM_ID, 0)) > -1)
	{
		_fuim_funcs.fz_fuim_tmpl_set_std_titles(fuim_tmpl , NULL , "" , "");  

		g1 = _fuim_funcs.fz_fuim_new_group(fuim_tmpl, -1, FZ_FUIM_NONE, FZ_FUIM_FLAG_GFLT, NULL);
			g2 = _fuim_funcs.fz_fuim_new_group(fuim_tmpl, g1, FZ_FUIM_NONE, FZ_FUIM_FLAG_GFLT , NULL);

				_base_funcs.fzrt_fzr_get_string(_resource_ref, 1, UUID_GEN_TRADITIONAL_STRING, str);
				
				_fuim_funcs.fz_fuim_new_text_static(fuim_tmpl, g2, FZ_FUIM_NONE, FZ_FUIM_FLAG_NONE,str,   NULL, NULL);
				g3 = _fuim_funcs.fz_fuim_new_text_edit(fuim_tmpl, g2, UUID_GEN_TRADITIONAL_EDIT, FZ_FUIM_FLAG_NONE, NULL, NULL);
				_fuim_funcs.fz_fuim_item_set_dimns(fuim_tmpl, g3, 250, 0);
				err = _base_funcs.fzrt_UUID_create(newuuid);
				if (err == FZRT_NOERR) {
					err = _base_funcs.fzrt_UUID_to_string(newuuid, TRUE, str, sizeof(str));
				}
				else strcpy(str,"");
				_fuim_funcs.fz_fuim_stack_put(fuim_tmpl, UUID_GEN_FUIM_STACK_TRADITIONAL, sizeof(str),str);
				_fuim_funcs.fz_fuim_stack_get_ptr(fuim_tmpl,UUID_GEN_FUIM_STACK_TRADITIONAL, (void **)&edit_str);
				_fuim_funcs.fz_fuim_item_range_string(fuim_tmpl, g3, edit_str, 256, 256, FZ_FUIM_FLAG_NONE );

				_base_funcs.fzrt_fzr_get_string(_resource_ref, 1, UUID_GEN_FORMZ_STRING, str);
				_fuim_funcs.fz_fuim_new_text_static(fuim_tmpl, g2, FZ_FUIM_NONE, FZ_FUIM_FLAG_NONE,str,   NULL, NULL);
				g3 = _fuim_funcs.fz_fuim_new_text_edit(fuim_tmpl, g2,  UUID_GEN_FORMZ_EDIT, FZ_FUIM_FLAG_NONE, NULL, NULL);
				_fuim_funcs.fz_fuim_item_set_dimns(fuim_tmpl, g3, 380, 0);
				
				if (err == FZRT_NOERR) {
					err = _base_funcs.fzrt_UUID_to_string(newuuid, FALSE, str, sizeof(str));
				}
				else strcpy(str,"");
				_fuim_funcs.fz_fuim_stack_put(fuim_tmpl, UUID_GEN_FUIM_STACK_FORMZ, sizeof(str),str);
				_fuim_funcs.fz_fuim_stack_get_ptr(fuim_tmpl,UUID_GEN_FUIM_STACK_FORMZ, (void **)&edit_str);
				_fuim_funcs.fz_fuim_item_range_string(fuim_tmpl, g3, edit_str, 256, 256, FZ_FUIM_FLAG_NONE );


			_base_funcs.fzrt_fzr_get_string(_resource_ref, 1, UUID_GENERATE_STR, str);
			g3 = _fuim_funcs.fz_fuim_new_button(fuim_tmpl,g1,UUID_GEN_GENERATE_BUTTON, FZ_FUIM_FLAG_NONE,str,uuid_gen_fuim_item_func, NULL);

	}
	
	return (tindx);
}

/************************************************************************
	uuid_gen_main
		Executes upon running utility.
*************************************************************************/
static fzrt_error_td uuid_gen_main(void)
/************************************************************************/
{

	fzrt_error_td err = FZRT_NOERR;
	

		_fuim_funcs.fz_fuim_dlog_drive(uuid_gen_fuim_tmpl);

	return(err);
}



/************************************************************************
							Plugin Functions
*************************************************************************/

/************************************************************************
	uuid_gen_acquire_fsets
		Acquires formZ function sets.
*************************************************************************/
static fzrt_error_td uuid_gen_acquire_fsets(const fzpl_fset_glue_fset* const fsf)
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;

	err = fsf->fzpl_fset_acquire ( FZ_FZRT_BASE_FSET_TYPE, FZ_FZRT_BASE_FSET_VERSION, FZRT_UUID_NULL,
								 FZPL_TYPE_STRING(fz_fzrt_base_fset), sizeof(_base_funcs),
								 (fzpl_fset_td *)&_base_funcs );

	if(err == FZRT_NOERR)
	{
		err = fsf->fzpl_fset_acquire ( FZ_FUIM_FSET_TYPE, FZ_FUIM_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_fuim_fset), sizeof(_fuim_funcs),
									 (fzpl_fset_td *)&_fuim_funcs );
	}

	

	if(err == FZRT_NOERR)
	{
		err = fsf->fzpl_fset_acquire ( FZ_TYPE_FSET_TYPE,
								  FZ_TYPE_FSET_VERSION,
								  FZRT_UUID_NULL,
								  FZPL_TYPE_STRING(fz_type_fset),
								  sizeof(_type_funcs),
								  (fzpl_fset_td *)&_type_funcs );
	}

	if(err == FZRT_NOERR)
	{
		err = fsf->fzpl_fset_acquire ( FZ_FZRT_MEM_FSET_TYPE,
								  FZ_FZRT_MEM_FSET_VERSION,
								  FZRT_UUID_NULL,
								  FZPL_TYPE_STRING(fz_fzrt_mem_fset),
								  sizeof(_mem_funcs),
								  (fzpl_fset_td *)&_mem_funcs );
	}
	


	if(err == FZRT_NOERR)
	{
		err = fsf->fzpl_fset_acquire(	FZ_FZRT_FILE_FSET_TYPE,
										FZ_FZRT_FILE_FSET_VERSION,
										FZRT_UUID_NULL,
										FZPL_TYPE_STRING(fz_fzrt_file_fset),
										sizeof(_file_funcs),
										(fzpl_fset_td *)&_file_funcs );
	}
	
	return err;
}

/************************************************************************
	uuid_gen_release_fsets
		Releases all function sets used from formZ.
*************************************************************************/
static void uuid_gen_release_fsets(const fzpl_fset_glue_fset* const fsf)
/************************************************************************/
{
	fsf->fzpl_fset_release( (fzpl_fset_td*)&_file_funcs );
	fsf->fzpl_fset_release( (fzpl_fset_td*)&_mem_funcs );
	fsf->fzpl_fset_release( (fzpl_fset_td*)&_type_funcs );
	fsf->fzpl_fset_release( (fzpl_fset_td*)&_fuim_funcs );
	fsf->fzpl_fset_release( (fzpl_fset_td*)&_base_funcs );
}


/************************************************************************
	uuid_gen_check_config
		A first pass check to see if the plugin can be loaded.  It does
		the following:
			Acquires formZ function sets.
			Checks licensing.
			Informs formZ if this plugin is dependent on
				function sets from other plugins.
			Opens localization resource file and loads strings.
*************************************************************************/
static fzrt_error_td uuid_gen_check_config(const fzpl_fset_glue_fset* const fsf, const fzpl_host_config_td* const hostConfig)
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;
	/* resource file variables */
	fzrt_floc_ptr	floc;
	char			buffer[STRING_SIZE];

	_fsf = fsf;

	/*  Make sure required formZ defined function sets are available */
	/*  Release the function sets at unload. */

	err = uuid_gen_acquire_fsets(fsf);

	/* IF NEEDED: Check License */

	/* IF NEEDED: Tell the plugin manager what plugins this plugins are dependent on  */

	/* get localized strings */

	if (err == FZRT_NOERR)
	{
		/* initialize file locator structure */
		err = _file_funcs.fzrt_file_floc_init(&floc);
		if (err == FZRT_NOERR)
		{
			/* get the location of this plugin */
			err = _fsf->fzpl_plugin_file_get_floc(floc);

			if (err == FZRT_NOERR)
			{
				/* open the resource file eg. UUID_GEN.ENU.fzr */
				err = _base_funcs.fzrt_fzr_open(floc, "UUID_GEN", &_resource_ref);

				if (err == FZRT_NOERR)
				{
					/* get the localized resource strings for this plugin */
					_base_funcs.fzrt_fzr_get_string(_resource_ref, 1, 1, buffer);
					strcpy(_name, buffer);
					_base_funcs.fzrt_fzr_get_string(_resource_ref, 1, 2, buffer);
					strcpy(_short_name, buffer);
					
					/* close the resource file */
					//err = _base_funcs.fzrt_fzr_close(resource_ref);	/* closed in finit function */
				}
				/* throw away floc */
				_file_funcs.fzrt_file_floc_finit(&floc);
			}
		}
	}
	
	/* Tell the plugin manager how many plugins and what types we'll be loading */

	if (err != FZRT_NOERR)
	{
		uuid_gen_release_fsets(fsf);
	}

	return err;
}

/************************************************************************
	uuid_gen_init
		Registers plugins and their associated function sets.
*************************************************************************/
static fzrt_error_td uuid_gen_init(const fzpl_fset_glue_fset* const fsf, const fzpl_host_config_td* const hostConfig)
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;

	/* TODO : Get Localized plugin name form rsrc file */
	err = fsf->fzpl_plugin_register( UUID_GEN_PLUGIN_UUID,
									 _name,
									 UUID_GEN_PLUGIN_VERSION,
									 UUID_GEN_PLUGIN_VENDOR,
									 UUID_GEN_PLUGIN_URL,
									 FZ_UTIL_SYST_EXTS_TYPE,
									 FZ_UTIL_SYST_EXTS_VERSION,
									 NULL /*error string function*/,
									 0,
									 NULL,
									 &_uuid_gen_plugin_runtime_id);
	if (err == FZRT_NOERR)
	{
		err = fsf->fzpl_plugin_add_fset( _uuid_gen_plugin_runtime_id,
										 FZ_UTIL_CBAK_SYST_FSET_TYPE,
										 FZ_UTIL_CBAK_SYST_FSET_VERSION,
										 FZ_UTIL_CBAK_SYST_FSET_NAME,
										 FZPL_TYPE_STRING(fz_util_cbak_syst_fset),
										 sizeof(fz_util_cbak_syst_fset),
										 uuid_gen_fill_fset,
										 FALSE);
	}

	if (err != FZRT_NOERR)
	{
		uuid_gen_release_fsets(fsf);
	}

	return err;
}

/************************************************************************
	uuid_gen_finit
		Unregisters plugins.  Releases function sets.
*************************************************************************/
static fzrt_error_td uuid_gen_finit(const fzpl_fset_glue_fset* const fsf)
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;

	err = fsf->fzpl_plugin_unregister(_uuid_gen_plugin_runtime_id);

	err = _base_funcs.fzrt_fzr_close(_resource_ref);
	uuid_gen_release_fsets(fsf);

	return err;
}


/************************************************************************
						Utility Plugin Fill Function
*************************************************************************/

/************************************************************************
	uuid_gen_fill_fset
		Fills the fset structure with the callback functions for a
		project utility plugin.
*************************************************************************/
static fzrt_error_td uuid_gen_fill_fset(const fzpl_fset_def_ptr fset_def, fzpl_fset_td* const fset)
/************************************************************************/
{
	fzrt_error_td			  err = FZRT_NOERR;
	fz_util_cbak_syst_fset* install_struct;

	if ( fset_def && fset )
	{
		err = _fsf->fzpl_fset_def_check ( fset_def, FZ_UTIL_CBAK_SYST_FSET_VERSION, FZPL_TYPE_STRING(fz_util_cbak_syst_fset), sizeof ( fz_util_cbak_proj_fset ), FZPL_VERSION_OP_NEWER );

		if (err == FZRT_NOERR)
		{
			install_struct = (fz_util_cbak_syst_fset*)fset;

			install_struct->fz_util_cbak_syst_uuid	= uuid_gen_uuid;
			install_struct->fz_util_cbak_syst_name	= uuid_gen_name;
			install_struct->fz_util_cbak_syst_main	= uuid_gen_main;
		}
	}
	else
		return (_base_funcs.fzrt_error_set(FZRT_BAD_PARAM_ERROR, FZRT_ERROR_SEVERITY_ERROR, FZRT_ERROR_CONTEXT_FZRT, 0));

	return err;
}

/************************************************************************
						Required Plugin Functions
*************************************************************************/

/************************************************************************
	fz_plugin_entry
		The main entry point for a formZ plugin.
*************************************************************************/
FZPL_PLUGIN_FUNC(fzrt_error_td) fz_plugin_entry(const fzpl_fset_glue_fset* const fsf, fzpl_command_td cmd, const fzpl_host_config_td* const hostConfig)
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;

	
	switch ( cmd )
	{
		case FZPL_PLUGIN_CHECK:
			err = uuid_gen_check_config ( fsf, hostConfig );
		break;
		
		case FZPL_PLUGIN_INITIALIZE:
			err = uuid_gen_init ( fsf, hostConfig );
		break;
		
		case FZPL_PLUGIN_EXIT:
			err = uuid_gen_finit ( fsf );
		break;
					
	}

	return ( err );
}
