#import <objc/HashTable.h>
#import "MiscList.h"
#import <stdio.h>
#import <string.h>
#import <stdlib.h>
#import <ctype.h>
#import "structures.h"
#import "Htag.h"
#import "TeXviewSwitchBoard.h"

extern TeXviewSwitchBoard *myTeXviewSwitchBoard ;
extern Boolean      prescan;
extern shalfword    hh, vv;
extern integer      pagenum;
extern int          lasthh, lastvv;
extern int          last_pagenum;
extern float        screendpi ;
extern int          debugon ;
int                 do_we_have_data = 0;
//id                  htags;
//id                  htpages;
MiscList	  *visited_tags; // History list of tags visited
// int									visit_index;  // Index of current tag

void
ht_get_ready_for_new_file()
{
  id                  htags;
  id                  htpages;
    if (do_we_have_data) {
  htags = [myTeXviewSwitchBoard getCurrentHtags] ; 
  htpages = [myTeXviewSwitchBoard getCurrentHtpages] ;
/*clear data*/
	[htags freeObjects];
	[htags free];
	[htpages freeObjects];
	[htpages free];
	[visited_tags freeObjects];
	[visited_tags free];
    }
    htags = [HashTable alloc];
    [htags initKeyDesc:"*" valueDesc:"@" capacity:10];
    htpages = [HashTable alloc];
    [htpages initKeyDesc:"i" valueDesc:"@" capacity:5];
		visited_tags = [[MiscList alloc] initCount:25];
	//	visit_index = 0;
}

/* Taking in to account screendpi, we can have multiple tags */
/* The key in the label is the name concatenated with the dpi */
void
ht_add_tag(char *pt ,int isexternal) /* Add an href */
{
    char               *ptexp;
    id                  htags;

    ptexp = (char *)malloc(strlen(pt) + 7);
    sprintf(ptexp, "%sMD%.0f", pt, screendpi);
#ifdef DEBUG
  if (debugon > 2 ) {
    printf("tag's id is %s\n", ptexp) ;
   }
#endif
    htags = [myTeXviewSwitchBoard getCurrentHtags] ;

    if ([htags isKey:ptexp]) {
	 /*   if (!prescan) { */
	      [(id)[htags valueForKey:ptexp] 
              setParams:prescan:hh:vv:pagenum:lasthh:
                          lastvv:last_pagenum:screendpi];
#ifdef DEBUG
  if (debugon > 2 ) {
	       printf("Just updated a tag %s with parameters:\n \
         prescan=%i,hh=%i,vv=%i,pagenum=%i,lasthh=%i,lastvv=%i,last_pagenum=%i\
             ,screendpi=%.1f\n",ptexp,prescan,hh,vv,pagenum,
                           lasthh,lastvv,last_pagenum, screendpi);
  }
#endif
          free(ptexp) ;
          return ;
       /*  }  end of prescan */
    } else { /* tag isn't found */
	     id                  newtag;
	     char               *tagcpy;

    /*Make new tag object and insert it in*/
  	newtag = [[Htag new] init];
  	[newtag setParams:prescan :hh :vv :pagenum :lasthh :lastvv :last_pagenum
                    :screendpi];
	  [newtag setMyName:ptexp];[newtag setIsTarget:1];
	  tagcpy = (char *)malloc(strlen(ptexp) + 1);
	  strcpy(tagcpy, ptexp);
	  [htags insertKey:tagcpy value:newtag];
#ifdef DEBUG
  if (debugon > 2 ) {
     printf("Just inserted a tag %s with parameters:\n \
     prescan=%i,hh=%i,vv=%i,pagenum=%i,lasthh=%i,lastvv=%i,last_pagenum=%i, \
     screendpi=%.1f\n",
      ptexp,prescan,hh,vv,pagenum,lasthh,lastvv,last_pagenum, screendpi);
  }
#endif 
     free (ptexp) ;
     return ;
    }
}

id                  page;
extern int          last_thing;
void
ht_register_page()
{
    id                  htpages;
    htpages = [myTeXviewSwitchBoard getCurrentHtpages] ;
    last_thing = 0;
    if (![htpages isKey:(void *)pagenum]) {

/*Let us add a page */
	page = [HashTable alloc];
	[page initKeyDesc:"*" valueDesc:"@" capacity:3];
	[htpages insertKey:(void *)pagenum value:page];
    }
}
void
ht_add_pt(char *pt ,int isexternal) /* Add an href */
{
    char               *real_name;
    char               *ptexp;
    id                  obj;
    int                 counter = 0;
    id                  htpages;

    real_name = (char *)malloc(strlen(pt) + 1);
    strcpy(real_name, pt);
    ptexp = (char *)malloc(strlen(pt) + 7);
    strcpy(ptexp, pt);
    htpages = [myTeXviewSwitchBoard getCurrentHtpages] ;
    page = [htpages valueForKey:(void *)last_pagenum];
    if ([page isKey:ptexp]) {
	if (prescan) {                 /* prescan, don't care about it */
	    free(real_name);
	    free(ptexp);
	    return;
	}
	obj = (id)[page valueForKey:ptexp];
	while (([obj getScreenDpi] != screendpi) || ([obj getiHH] != lasthh) || 
          ([obj getiVV] != lastvv)) {
	    counter++;
	    sprintf(ptexp, "%sDL%d", real_name, counter); 
	    obj = (id)[page valueForKey:ptexp];
	    if (!obj)
		break;
	    if (([obj getiHH] == 0) && ([obj getiVV] == 0)) {
		break; }
	}
	if (obj) {
	    if (([obj getiHH] == 0) && ([obj getiVV] == 0)) {
		[obj setParams:prescan :hh :vv :pagenum :lasthh :lastvv :last_pagenum 
                  :screendpi];
     }
	} else {
	    id                  newpt;

/*Make new tag object and insert it in*/
	    newpt = [[Htag new] init];
	    [newpt setParams:prescan :hh :vv :pagenum :lasthh :lastvv :last_pagenum
                       : screendpi];
	    [newpt setMyName:real_name];[newpt setIsExternal:isexternal];
	    [page insertKey:ptexp value:newpt];
	    free(real_name);
	    return;
	}
    } else
{
	    id                  newpt;

/*Make new tag object and insert it in*/
	    newpt = [[Htag new] init];
	    [newpt setParams:prescan :hh :vv :pagenum :lasthh :lastvv :last_pagenum
         :screendpi];
	    [newpt setMyName:real_name];[newpt setIsExternal:isexternal];
	    [page insertKey:ptexp value:newpt];
	    free(real_name);
	    return;
	}
    free(real_name);
    free(ptexp);
}
