/* Time-stamp: <97/01/17 13:14:54 john> */

/* 
# Purpose: handle the graphics control file (see ../doc/tech/graphics.html)
 */

#include <stdio.h>
#include "attr.h"
#include "style.h"
#include "graphics.h"
#include "save.h"

/* #define debug 1 */

struct entry *all_graphics = NULL;
struct entry *all_colours = NULL;

static int graphics_check_entry(struct entry *new_entry, int entry_count)
{
  char *attr=get_attr(new_entry, "type");
  if (attr == NULL)
    {
      new_entry->type = NULL;
    } else {
      new_entry->type = attr;
    }
  new_entry ->name = NULL;
  return ENTRY_OK;
}

static int colour_check_entry(struct entry *new_entry, int entry_count)
{
  char *attr=get_attr(new_entry, "name");
  if (attr == NULL)
    {
      return ENTRY_BAD;
    } else {
      new_entry->name = attr;
    }
  new_entry->type = NULL;
  return ENTRY_OK;
}

#define load_graphics() \
 { \
    if (all_graphics == NULL) \
     { \
      read_attribute_file(".", "graphics", NULL, \
                          &all_graphics, \
			  NULL, \
			  graphics_check_entry, \
                          1, 0); \
      read_attribute_file(".", "colours", NULL, \
                          &all_colours, \
			  NULL, \
			  colour_check_entry, \
                          1, 0); \
     } \
 }

/* return all the graphics specifications for this sort of object
 */

struct entry *get_graphics(struct entry *object,
			   struct style *style)
{
  char *variety = get_attr(object, "variety");
  char *kind = get_attr(object, "kind");
  char *type = get_attr(object, "type");
  struct entry *g;

#ifdef debug
  printf("<!-- Looking for graphics entry to suit \"%s\" (%s:%s:%s) -->\n",
	 object->name,
	 (type!=NULL) ? type : "<no type>",
	 (kind!=NULL) ? kind : "<no kind>",
	 (variety!=NULL) ? variety : "<no variety>");
#endif

  load_graphics();

  for (g = all_graphics;
       g != NULL;
       g = g->next)
    {
      if (variety)
	{
	  char *v = get_attr(g, "variety");
	  if ((v != NULL) && (strcmp(variety, v) == 0))
	    {
	      return g;
	    }
	}
    }

  for (g = all_graphics;
       g != NULL;
       g = g->next)
    {
      if (kind)
	{
	  char *k = get_attr(g, "kind");
	  if ((k != NULL) && (strcmp(kind, k) == 0))
	    {
	      return g;
	    }
	}

    }

  for (g = all_graphics;
       g != NULL;
       g = g->next)
    {
      if (type)
	{
	  char *ty = get_attr(g, "type");
	  if ((ty != NULL) && (strcmp(type, ty) == 0))
	    {
	      return g;
	    }
	}
    }

  return NULL;
}

/* return a graphic property of the object, or for that sort of object
   if the individual object doesn't specify anything */

char *get_graphic(struct entry *object,
		  char *property,
		  struct style *style)
{
  char *props = get_attr(object, property);

#ifdef debug
  printf("\n\n<!-- lookup of \"%s\" property of object \"%s\" -->\n",
	 property, object->name);
#endif

  if (props != NULL)
    {
#ifdef debug
      printf("<-- \"%s\" specifies its own \"%s\" -->\n\n", object->name, property);
#endif
      return props;
    } else {
      struct entry *class = get_graphics(object, style);

      if (class == NULL)
	{
#ifdef debug
	  printf("<!-- could not find \"%s\" for \"%s\" -->\n",
		 property, object->name);
#endif
	  return NULL;
	} else {
	  return get_attr(class, property);
	}
    }
}

char *get_colour(struct entry *object,
		 char *colourspace,
		 struct style *style)
{
  char *col = get_graphic(object, "colour", style);
#ifdef debug
  printf("\n\n<!-- colour lookup for %s -->\n\n", object->name);
#if 0
  return NULL;
#endif
#endif
  if (col == NULL)
    {
      return NULL;
    } else {
      struct entry *col_entry;

      for (col_entry = all_colours;
	   col_entry != NULL;
	   col_entry = col_entry->next)
	{
	  char *col_ent_name = get_attr(col_entry, "name");
	  if (col_ent_name == NULL) continue;
	  if (strcmp(col_ent_name, col) != 0) continue;
	  return get_attr(col_entry, colourspace);
	}
      return NULL;
    }
}

struct entry *get_graphics_by_type(char *type)
{
  struct entry *g;

  load_graphics();
#if 0
  printf("Getting graphics for type=\"%s\"\n", type);
#endif
  for (g = all_graphics;
       g != NULL;
       g = g->next)
    {
      char *this_type = get_attr(g,"type");
      if ((this_type != NULL) &&
	  (strcmp(this_type, type) == 0))
	{
	  return g;
	}
    }

  return (struct entry*)NULL;
}

char *get_type_generic_attribute(char *type, char *attribute)
{
  struct entry *g = get_graphics_by_type(type);
  if (g == NULL)
    {
      return (char*)NULL;
    }
  return get_attr(g, attribute);
}

/* end of graphics.c */
