/* This file is part of `mogul'
 * Copyright (c) 1995 David Deaven
 *
 * In the following, "you" and "You" refer to any entity other than 
 * the Microsoft corporation and its subsidiaries.  All rights are
 * reserved by the author in connection with Microsoft and its
 * subsidiaries.  The Microsoft Network is explicity denied the right
 * to copy, reproduce, or transmit this software.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Author: deaven $
 * $Id: rules.c,v 2.0 1996/07/09 20:59:55 deaven Exp $
 */

/* Manipulation of atomic type specification.
 * Copyright (c) 1994 David Deaven
 */
#include <stdio.h>
#include <string.h>
#include "dmd/rgb.h"
#include "dmd/memory.h"
#include "dmd/element.h"
#include "mogul.h"

struct chain {
	struct ruleAttributes rule;
	struct chain *next;
};

struct MogulRulesRec {
	struct chain *first;
	StringPool sp;
};

static struct chain *
default_atom_rule(MogulRules r, char *aname)
{
	struct chain *c = NewPointer(c);
	Element *e = GuessElement(aname);

	c->rule.type = RULE_ATOM;

	c->rule.attr.mode = MA_MINIMAL | MA_FILLED | MA_OUTLINE;
	c->rule.attr.color = ColorFromName(e->color);

	c->rule.u.atom.aname = AddToStringPool(r->sp, aname);
	c->rule.u.atom.fullname = AddToStringPool(r->sp, e->fullname);
	c->rule.u.atom.radius = e->radius;

	return c;
}

static struct chain *
default_bond_rule(MogulRules r, char *anameFrom, char *anameTo)
{
	struct chain *c = NewPointer(c);
	Element *e1 = GuessElement(anameFrom);
	Element *e2 = GuessElement(anameTo);

	c->rule.type = RULE_BOND;

	c->rule.attr.mode = MA_FILLED | MA_OUTLINE;
	c->rule.attr.color = ColorFromName("slate");

	c->rule.u.bond.anameFrom = AddToStringPool(r->sp, anameFrom);
	c->rule.u.bond.anameTo = AddToStringPool(r->sp, anameTo);
	c->rule.u.bond.bondr = e1->bondr + e2->bondr;
	c->rule.u.bond.radius = 0.6 *
	  (e1->radius < e2->radius ? e1->radius : e2->radius);

	return c;
}

MogulRules NewMogulRules(void)
{
	MogulRules r = NewPointer(r);

	r->first = NULL;
	r->sp = NewStringPool();

	return r;
}

void DeleteMogulRules(MogulRules r)
{
	if(r) {
	  struct chain *n, *c = r->first;
	  while(c) {
	    n = c->next;
	    myfree(c);
	    c = n;
	  }
	  DeleteStringPool(r->sp);
	  myfree(r);
	}
}

static struct ruleAttributes *
add_rule(MogulRules r, struct chain *c)
{
	c->next = r->first;
	r->first = c;
	return &(c->rule);
}

/* look up the attributes for an atom.
 */
struct ruleAttributes *
MogulGetAtomRule(MogulRules r, char *aname)
{
	if(r) {
	  struct chain *c = r->first;
	  while(c) {
	    if(c->rule.type == RULE_ATOM &&
	       strcmp(aname, c->rule.u.atom.aname) == 0)
	    {
	      return &(c->rule);
	    }
	    c = c->next;
	  }
	}

	return add_rule(r, default_atom_rule(r, aname));
}

struct ruleAttributes *
MogulGetBondRule(MogulRules r, char *anameFrom, char *anameTo)
{
	if(r) {
	  struct chain *c = r->first;
	  while(c) {
	    if(c->rule.type == RULE_BOND &&
	      ((!strcmp(anameFrom, c->rule.u.bond.anameFrom) &&
	        !strcmp(anameTo, c->rule.u.bond.anameTo)) ||
	       (!strcmp(anameTo, c->rule.u.bond.anameFrom) &&
	        !strcmp(anameFrom, c->rule.u.bond.anameTo))))
	    {
	      return &(c->rule);
	    }
	    c = c->next;
	  }
	}

	return add_rule(r, default_bond_rule(r, anameFrom, anameTo));
}

#ifdef DEBUG
void debugRules(MogulRules r)
{
	if(r) {
	  struct chain *c = r->first;
	  while(c) {
	    switch(c->rule.type) {
	      case RULE_ATOM:
		printf("atom \"%s\" (\"%s\") r=%g",
		  c->rule.u.atom.aname,
		  c->rule.u.atom.fullname,
		  c->rule.u.atom.radius);
		break;

	      case RULE_BOND:
		printf("bond \"%s\"-\"%s\" bondr=%g r=%g",
		  c->rule.u.bond.anameFrom, c->rule.u.bond.anameTo,
		  c->rule.u.bond.bondr, c->rule.u.bond.radius);
		break;
	    }
	  }
	  printf("\n");
	}
}
#endif
