/*------------------------------------------------------------------------*
  Filename....: manf_attr.c


		 COPYRIGHT (C) 2001.  All Rights Reserved.
		 auto*des*sys, Inc., Columbus, Ohio, USA.
 *------------------------------------------------------------------------*/

#include "manf.h"


/************************************************************************
	Manufacturer attribute uuid callback function
*************************************************************************/
static fzrt_error_td manf_attr_uuid(
	fzrt_UUID_td			uuid		
	)
{
	plugin_stuff.fzrt_base_funcs.fzrt_UUID_copy( MANF_ATTR_ID, uuid );

	return(FZRT_NOERR);
}

/************************************************************************
	Manufacturer attribute name callback function
*************************************************************************/
static fzrt_error_td manf_attr_name(
	char			*str,
	fzrt_int			max_str
	)
{
	char	name[256];
	
	plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_NAME_STR, name);
	strncpy(str,name,max_str);

	return(FZRT_NOERR);
}

/************************************************************************
	Manufacturer attribute size callback function
*************************************************************************/
static fzrt_error_td manf_attr_info(
	fzrt_int			*data_size,
	fzrt_int			*level_flags,
	fzrt_int			*flags
	)
{
	*data_size = sizeof(manf_attr_data_td);
	
	*level_flags = 0;
	FZ_SETBIT(*level_flags,FZ_ATTR_LEVEL_OBJT);
	
	*flags = 0;
	FZ_SETBIT(*flags,FZ_ATTR_FLAGS_SHOW_QUERY_ALWAYS);

	return(FZRT_NOERR);
}


/************************************************************************
	Manufacturer attribute iost callback function
*************************************************************************/
static fzrt_error_td	manf_attr_iost (
	fzrt_int					windex,	
	fz_iost_ptr				iost, 	
	fz_iost_dir_td_enum		dir,		
	fzpl_vers_td * const version, 	
	fzrt_unsigned_int 			size,		
	void					*data	
	)
{
	manf_attr_data_td	*manf_attr;
	fzrt_error_td		rv;
	
	manf_attr = (manf_attr_data_td*) data;

	if ( dir == FZ_IOST_WRITE ) *version = 0;
	
	if((rv = plugin_stuff.iost_funcs.fz_iost_string(iost, &manf_attr->manufacturer, 1))		== FZRT_NOERR &&
	   (rv = plugin_stuff.iost_funcs.fz_iost_string(iost, &manf_attr->product, 1)) 			== FZRT_NOERR &&
	   (rv = plugin_stuff.iost_funcs.fz_iost_int(iost, &manf_attr->cost_type, 1))			== FZRT_NOERR &&
	   (rv = plugin_stuff.iost_funcs.fz_iost_double(iost, &manf_attr->cost_per_unit, 1))	== FZRT_NOERR &&
	   (rv = plugin_stuff.iost_funcs.fz_iost_double(iost, &manf_attr->cost_per_volume, 1))	== FZRT_NOERR )
	{	
		rv = plugin_stuff.iost_funcs.fz_iost_double(iost, &manf_attr->cost_per_surface, 1);
	}
	

	return(rv);
}

/************************************************************************
	Manufacturer attribute defaults callback function
*************************************************************************/
static fzrt_error_td	manf_attr_deflt (	
	fzrt_int					windex,
	void					*data
	)
{
	manf_attr_data_td	*manf_attr;
	fzrt_error_td		rv = FZRT_NOERR;
	
	manf_attr = (manf_attr_data_td*) data;
	
	plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DEFAULT_MANF_STR, manf_attr->manufacturer);
	plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DEFAULT_PROD_STR, manf_attr->product);
	manf_attr->cost_type = 0;
	manf_attr->cost_per_unit	= 1495.0;
	manf_attr->cost_per_volume 	= 1.0;
	manf_attr->cost_per_surface = 1.0;

	
	return(rv);
}

/************************************************************************
	Manufacturer attribute fuim callback function
*************************************************************************/
static fzrt_error_td	manf_attr_iface_tmpl (	
	fzrt_int					windex,
	fz_fuim_tmpl_ptr		fuim_tmpl,
	fzrt_ptr				data
	)
{
	manf_attr_data_td	*manf_attr;
	short				g1,g2,align[3];
	char				str[256];
	fzrt_error_td		rv = FZRT_NOERR;
	
	manf_attr = (manf_attr_data_td*) data;
	
	plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_NAME_STR, str);
	if((rv = plugin_stuff.fuim_funcs.fz_fuim_tmpl_init(fuim_tmpl,str, 0, MANF_ATTR_ID, 0)) == FZRT_NOERR)
	{
		plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_MANUFACTURER_STR, str);
		plugin_stuff.fuim_funcs.fz_fuim_new_text_static_edit(fuim_tmpl,-1,FZ_FUIM_NONE,str,FZ_FUIM_NONE,FZ_FUIM_FLAG_NONE, NULL, NULL,&g1);			
		plugin_stuff.fuim_funcs.fz_fuim_item_range_string(fuim_tmpl, g1, manf_attr->manufacturer, 128, 128, FZ_FUIM_FLAG_NONE);
		align[0] = g1;
		
		plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_PRODUCT_STR, str);
		plugin_stuff.fuim_funcs.fz_fuim_new_text_static_edit(fuim_tmpl,-1,FZ_FUIM_NONE,str,FZ_FUIM_NONE,FZ_FUIM_FLAG_NONE, NULL, NULL,&g1);			
		plugin_stuff.fuim_funcs.fz_fuim_item_range_string(fuim_tmpl, g1, manf_attr->product, 128, 128, FZ_FUIM_FLAG_NONE);
		align[1] = g1;
		
		plugin_stuff.fuim_funcs.fz_fuim_item_align(fuim_tmpl, FZ_FUIM_ALIGN_VLFT | FZ_FUIM_ALIGN_MAX, 2, align);

		plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_UNIT_STR, str);
		g1 = plugin_stuff.fuim_funcs.fz_fuim_new_radio(fuim_tmpl,-1,FZ_FUIM_NONE,FZ_FUIM_FLAG_HORZ | FZ_FUIM_FLAG_GFLT,str, NULL, NULL);			
		plugin_stuff.fuim_funcs.fz_fuim_item_unary_int(fuim_tmpl, g1, &manf_attr->cost_type ,0);
		
			g2 = plugin_stuff.fuim_funcs.fz_fuim_new_text_edit(fuim_tmpl,g1,FZ_FUIM_NONE,FZ_FUIM_FLAG_NONE, NULL, NULL);			
			plugin_stuff.fuim_funcs.fz_fuim_item_range_double(fuim_tmpl, g2, &manf_attr->cost_per_unit, 0, 0, FZ_FUIM_FORMAT_FLOAT_DEFAULT, FZ_FUIM_RANGE_MIN | FZ_FUIM_RANGE_MIN_INCL);
			align[0] = g2;
			
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DOLLAR_STR, str);
			g2 = plugin_stuff.fuim_funcs.fz_fuim_new_text_static(fuim_tmpl,g1,FZ_FUIM_NONE, FZ_FUIM_FLAG_NONE,str, NULL, NULL);			

		plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_VOLUME_STR, str);
		g1 = plugin_stuff.fuim_funcs.fz_fuim_new_radio(fuim_tmpl,-1,FZ_FUIM_NONE,FZ_FUIM_FLAG_HORZ | FZ_FUIM_FLAG_GFLT,str, NULL, NULL);			
		plugin_stuff.fuim_funcs.fz_fuim_item_unary_int(fuim_tmpl, g1, &manf_attr->cost_type ,1);
		
			g2 = plugin_stuff.fuim_funcs.fz_fuim_new_text_edit(fuim_tmpl,g1,FZ_FUIM_NONE,FZ_FUIM_FLAG_NONE, NULL, NULL);			
			plugin_stuff.fuim_funcs.fz_fuim_item_range_double(fuim_tmpl, g2, &manf_attr->cost_per_volume, 0, 0, FZ_FUIM_FORMAT_FLOAT_DEFAULT, FZ_FUIM_RANGE_MIN | FZ_FUIM_RANGE_MIN_INCL);
			align[1] = g2;
		
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DOLLAR_STR, str);
			g2 = plugin_stuff.fuim_funcs.fz_fuim_new_text_static(fuim_tmpl,g1,FZ_FUIM_NONE, FZ_FUIM_FLAG_NONE,str, NULL, NULL);			

		plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_SURFACE_STR, str);
		g1 = plugin_stuff.fuim_funcs.fz_fuim_new_radio(fuim_tmpl,-1,FZ_FUIM_NONE,FZ_FUIM_FLAG_HORZ | FZ_FUIM_FLAG_GFLT,str, NULL, NULL);			
		plugin_stuff.fuim_funcs.fz_fuim_item_unary_int(fuim_tmpl, g1, &manf_attr->cost_type ,2);
		
			g2 = plugin_stuff.fuim_funcs.fz_fuim_new_text_edit(fuim_tmpl,g1,FZ_FUIM_NONE,FZ_FUIM_FLAG_NONE, NULL, NULL);			
			plugin_stuff.fuim_funcs.fz_fuim_item_range_double(fuim_tmpl, g2, &manf_attr->cost_per_surface, 0, 0, FZ_FUIM_FORMAT_FLOAT_DEFAULT, FZ_FUIM_RANGE_MIN | FZ_FUIM_RANGE_MIN_INCL);
			align[2] = g2;
		
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DOLLAR_STR, str);
			g2 = plugin_stuff.fuim_funcs.fz_fuim_new_text_static(fuim_tmpl,g1,FZ_FUIM_NONE, FZ_FUIM_FLAG_NONE,str, NULL, NULL);			
	
	
		plugin_stuff.fuim_funcs.fz_fuim_item_align(fuim_tmpl, FZ_FUIM_ALIGN_VLFT | FZ_FUIM_ALIGN_MAX, 3, align);
	}

	return(rv);
}


/************************************************************************
	Get field count callback
*************************************************************************/
static fzrt_error_td	 manf_attr_get_field_count (
	fzrt_int		*num_fields
	)
{
	*num_fields = 4;
	return(FZRT_NOERR);
}

/************************************************************************
	Get field info callback
*************************************************************************/
static fzrt_error_td	 manf_attr_get_field_info (
	fzrt_int						field_indx,		
	char						*name,			
	fzrt_int						max_name_len,
	fz_attr_field_type_enum		*field_type,
	fz_fuim_format_float_enum	*unit_fmt_flt,
	fz_fuim_format_int_enum		*unit_fmt_int,
	fz_type_td					*def_value,		
	fz_type_td					*min_value,		
	fz_type_td					*max_value,		
	fzrt_UUID_td				vlist_uuid,		
	fzrt_int						*flags			
												
	)
{
	fz_string_td	str,str2;
	double			dval;
	
	*flags = 0;
	switch(field_indx)
	{
		case 0 :
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_MANUFACTURER_STR, str);
			*field_type = FZ_ATTR_FIELD_TYPE_STNG;
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DEFAULT_MANF_STR, str2);
			plugin_stuff.type_funcs.fz_type_set_string(str2,def_value);
		break;
		
		case 1 :
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_PRODUCT_STR, str);
			*field_type = FZ_ATTR_FIELD_TYPE_STNG;
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_DEFAULT_PROD_STR, str2);
			plugin_stuff.type_funcs.fz_type_set_string(str2,def_value);
		break;
		
		case 2 :
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_TYPE_STR, str);
			*field_type = FZ_ATTR_FIELD_TYPE_STNG;
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_UNIT_STR, str2);
			plugin_stuff.type_funcs.fz_type_set_string(str2,def_value);
		break;
		
		case 3 :
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_STR, str);
			*field_type = FZ_ATTR_FIELD_TYPE_CRCY;
			
			FZ_SETBIT(*flags,FZ_FUIM_RANGE_MIN_BIT);
			dval = 1495.0;	plugin_stuff.type_funcs.fz_type_set_double(&dval,def_value);
			dval = 0.0;		plugin_stuff.type_funcs.fz_type_set_double(&dval,min_value);
			dval = 0.0;		plugin_stuff.type_funcs.fz_type_set_double(&dval,max_value);
		break;
	}
	
	strncpy(name,str,max_name_len);
	
	return(FZRT_NOERR);
}

/************************************************************************
	Get field data callback
*************************************************************************/
static fzrt_error_td	 manf_attr_get_field_data (
	fzrt_int 					windex, 	
	fz_objt_ptr				obj, 			
	void					*data,			
	fzrt_int					field_indx,		
	fz_type_td				*value				
	)
{
	fz_string_td		str;
	double				dval;
	manf_attr_data_td	*manf_attr;
	
	
	manf_attr = (manf_attr_data_td*) data;
	
	switch(field_indx)
	{
		case 0 :
			plugin_stuff.type_funcs.fz_type_set_string(manf_attr->manufacturer,value);
		break;
		
		case 1 :
			plugin_stuff.type_funcs.fz_type_set_string(manf_attr->product,value);
		break;
		
		case 2 :
			switch(manf_attr->cost_type)
			{
				case 0 :
					plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_UNIT_STR, str);
				break;
				
				case 1 :
					plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_VOLUME_STR, str);
				break;
				
				case 2 :
					plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.rsrc_ref, MANF_STRINGS, MANF_ATTR_COST_PER_SURFACE_STR, str);
				break;
			}
			plugin_stuff.type_funcs.fz_type_set_string(str,value);
		break;
		
		case 3 :
			switch(manf_attr->cost_type)
			{	case 0 : dval = manf_attr->cost_per_unit; 		break;
				case 1 : dval = manf_attr->cost_per_volume;		break;
				case 2 : dval = manf_attr->cost_per_surface;	break;
			}
			plugin_stuff.type_funcs.fz_type_set_double(&dval,value);
		break;
	
	}
	
	return(FZRT_NOERR);
}

/************************************************************************
	Fills the fset structure with the attribute callback functions.
*************************************************************************/
fzrt_error_td manf_attr_get_callback_fset ( const fzpl_fset_def_ptr fset_def, fzpl_fset_td * const fset )
{
	fzrt_error_td		err = FZRT_NOERR;
	fz_attr_cbak_fset	*attr_cbak_fset;

	err = plugin_stuff.fsf->fzpl_fset_def_check ( 	fset_def, 
										FZ_ATTR_CBAK_FSET_VERSION, 
										FZPL_TYPE_STRING(fz_attr_cbak_fset), 
										sizeof ( fz_attr_cbak_fset ), 
										FZPL_VERSION_OP_NEWER );

	if ( err == FZRT_NOERR )
	{
		attr_cbak_fset = (fz_attr_cbak_fset *)fset;

		attr_cbak_fset->fz_attr_cbak_name				= manf_attr_name;
		attr_cbak_fset->fz_attr_cbak_uuid				= manf_attr_uuid;
		attr_cbak_fset->fz_attr_cbak_info				= manf_attr_info;
		attr_cbak_fset->fz_attr_cbak_io				= manf_attr_iost;
		attr_cbak_fset->fz_attr_cbak_deflt				= manf_attr_deflt;
		attr_cbak_fset->fz_attr_cbak_iface_tmpl			= manf_attr_iface_tmpl;
	
		attr_cbak_fset->fz_attr_cbak_get_field_count	= manf_attr_get_field_count;
		attr_cbak_fset->fz_attr_cbak_get_field_info		= manf_attr_get_field_info;
		attr_cbak_fset->fz_attr_cbak_get_field_data		= manf_attr_get_field_data;
	}

	return err;
}

