#include "assert.h"   /* must be first for use eith hc2 */
#include "ruleeditorP.h"
#include "ru.h"
#include "memory.h"
#include "text.h"
#include "help.h"
#include "useful.h"

#define RULEEDITOR_NOTES "ruleeditor help notes"
#define RULEEDITOR_EXAMPLES "ruleeditor help examples"
#define RULEEDITOR_USAGE "ruleeditor usage"

NORET ruleeditor_UpdateTitle PROTOTYPE((int,reid));
char* ruleeditor_GetTitle PROTOTYPE((int));
NORET ruleeditor_Quit PROTOTYPE((reid,VOIDP));
NORET ruleeditor_Save PROTOTYPE((reid,AlRule,VOIDP));
NORET ruleeditor_Cleanup PROTOTYPE((int));
NORET ruleeditor_FullUsage();
NORET ruleeditor_Usage();


main(argc,argv)
int argc;
char** argv; 
{
  int i;
  XtAppContext AppCont;
  Display *dis;
  extern void XtMainLoop();
  
  Al_init();

  if (AlHelp_user_requested_help(argc, argv) == Bool_TRUE) {
    AlHelp_non_standard_usage("ruleeditor ruleset [-rule] rulenum");
    ruleeditor_FullUsage();
    ruleeditor_Cleanup(0);
  }

    /* initialize the toolkit, create an application context, and open a */
    /* display */
  XtToolkitInitialize();
  AppCont=XtCreateApplicationContext();
  dis=XtOpenDisplay(AppCont,NULL,"ruleeditor","Argus",
		    NULL,0,&argc,argv); 

  AlUI_Initialize(NULL);
  mt_Init(&argc,argv);
  rs_Init(dis,&argc,argv);
  ru_Init(&argc,argv);
  re_OpenEditor(dis,
		Rule_GR,
		ruleeditor_Save,
		ruleeditor_Quit,
		&argc,
		argv,
		ruleeditor_GetTitle(EditOrCreate_GR),
		NULL,
		NULL,
		NULL,
		NULL);
  XtAppMainLoop(AppCont);
}

/*======================================================================*/

/*--------------------------------*/
NORET ruleeditor_Usage()
{
  Al_warning(AlText_get(RE_TEXT_FILE,RULEEDITOR_USAGE));
}

/*--------------------------------------------------*/
NORET ruleeditor_FullUsage()
{
  char *notes[2], *examples[2];
  notes[0]=AlText_get(RE_TEXT_FILE,RULEEDITOR_NOTES); notes[1]=(char*)NULL;
  examples[0]=AlText_get(RE_TEXT_FILE,RULEEDITOR_EXAMPLES); 
  examples[1]=(char*)NULL;
  AlHelp_info("ruleeditor",
	      ALH_force | ALH_eliminate, ALH_none,
	      ALH_after | ALH_before | ALH_rule, ALH_none,
	      ALH_std_msgtype , ALH_ruleset | ALH_server,
	      notes,
	      examples,
	      "ruleeditor -help");
}

/*--------------------------------------------------*/
NORET ruleeditor_Cleanup(x)
     int x;
{
  Al_cleanup();
  exit(x);
}


/*--------------------------------------------------*/
NORET ruleeditor_Save(rei,Rule,UData)
     reid rei;
     AlRule Rule;
     VOIDP UData;
{
  switch (EditOrCreate_GR) {
  case RU_EDIT:
    AlRule_mark_changed(Rule);
    AlRuleSet_put_rule_at_index(RuleSet_GR,Rule,RuleNumber_GR-1);
    AlRuleSet_remove_rule(RuleSet_GR,RuleNumber_GR);
    break;
  case RU_CREATE:
    AlRuleSet_add_rule_to_end(RuleSet_GR,Rule);
    EditOrCreate_GR=RU_EDIT;
    RuleNumber_GR=AlRuleSet_number_of_rules(RuleSet_GR);
    break;
  case RU_IA:
    if (AlRuleSet_number_of_rules(RuleSet_GR)==RuleNumber_GR)
      AlRuleSet_add_rule_to_end(RuleSet_GR,Rule);
    else 
      AlRuleSet_put_rule_at_index(RuleSet_GR,Rule,RuleNumber_GR);
    EditOrCreate_GR=RU_EDIT;
    RuleNumber_GR++;
    break;
  case RU_IB:
    AlRuleSet_put_rule_at_index(RuleSet_GR,Rule,RuleNumber_GR-1);
    EditOrCreate_GR=RU_EDIT;
    break;
  default:
    printf("parsing error in ed.c ^^^10\n");
    break;
  }

  ruleeditor_UpdateTitle(EditOrCreate_GR,rei);
  
  if (AsClient_GR!=NULL)
    AlA1C_write_rules(RuleSet_GR,AsClient_GR); 
  else
    AlRSreg_save(RuleSetName_GR);
}


/*--------------------------------------------------*/
NORET ruleeditor_Quit(rei,UD)
reid rei;
VOIDP UD;
{
  re_CloseEditor(rei);
  ruleeditor_Cleanup(0);
}


/*--------------------------------------------------*/
char* ruleeditor_GetTitle(d)
     int d;
{
  char* Doing;

  Doing=(char *)Memory_allocate(120);
  switch (d) {
  case RU_EDIT:
    sprintf(Doing,"EDITING:  rule %d of rule set '%s'",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  case RU_CREATE:
    sprintf(Doing,"CREATING: rule %d of rule set '%s'",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  case RU_IA:
    sprintf(Doing,"CREATING: rule %d of rule set '%s'",
	    RuleNumber_GR+1,
	    RuleSetName_GR);
    break;
  case RU_IB:
    sprintf(Doing,"CREATING: rule %d of rule set '%s'",
	    RuleNumber_GR-1,
	    RuleSetName_GR);
    break;
  default:
    printf("bad parsing in d1.c ^^^10\n");
    break;
  }
  return(Doing);
}


/*--------------------------------------------------*/
NORET ruleeditor_UpdateTitle(d,rei)
     int d;
     reid rei;
{
  char* Doing;

  Doing=ruleeditor_GetTitle(d);
  re_UpdateTitle(rei,Doing);
  Memory_free(Doing);
}
