# include "dvoshell.h"

/* XXX note : for RegionName or RegionList, we need to free the skylist
   elements, but not in the case of radius selection - this implies 
   information carried back up */

static char *CATDIR  = NULL;
static SkyTable *sky = NULL;

int SetCATDIR (char *path, int verbose) {

  char *newpath;
  char catdir_config[256];
  char gscfile[256];
  char skyfile[256];
  int  skydepth;

  /* find CATDIR in config system */
  if (path == NULL) {
    if (VarConfig ("CATDIR", "%s", catdir_config) == NULL) return (FALSE);
    newpath = catdir_config;
  } else {
    newpath = path;
  }

  if (CATDIR != NULL) free (CATDIR);
  CATDIR = strcreate (newpath);

  if (VarConfig ("GSCFILE",  "%s", gscfile) == NULL) gscfile[0] = 0;
  if (VarConfig ("SKYFILE",  "%s", skyfile) == NULL) skyfile[0] = 0;
  if (VarConfig ("SKYDEPTH", "%d", &skydepth) == NULL) skydepth = 2;

  if (verbose) {
      gprint (GP_ERR, "CATDIR %s\n", CATDIR);
      gprint (GP_ERR, "GSCFILE %s\n", gscfile);
      gprint (GP_ERR, "SKYFILE %s\n", skyfile);
      gprint (GP_ERR, "SKYDEPTH %d\n", skydepth);
  }

  /* load the SkyTable at this point */
  /* set the image path as well */

  if (sky != NULL) SkyTableFree (sky);
  sky = SkyTableLoadOptimal (CATDIR, skyfile, gscfile, skydepth, verbose);
  if (sky == NULL) return FALSE;

  SkyTableSetFilenames (sky, CATDIR, "cpt");

  return (TRUE);
}

char *GetCATDIR () {
  if (CATDIR == NULL) {
    SetCATDIR (NULL, FALSE);
  }
  return (CATDIR);
}

SkyTable *GetSkyTable () {
  if (sky == NULL) {
    SetCATDIR (NULL, FALSE);
  }
  return (sky);
}

void FreeSkyRegionSelection (SkyRegionSelection *selection) {

  if (selection == NULL) return;
  if (selection[0].name != NULL) free (selection[0].name);
  if (selection[0].list != NULL) free (selection[0].list);
  free (selection);
}

SkyRegionSelection *SetRegionSelection (int *argc, char **argv) {
  
  int N;
  SkyRegionSelection *selection;

  ALLOCATE (selection, SkyRegionSelection, 1);
  selection[0].name = NULL;
  selection[0].list = NULL;
  selection[0].useDisplay = FALSE;
  selection[0].useSkyregion = FALSE;

  /* check for Region selection (named dvo catalog file) */
  if ((N = get_argument (*argc, argv, "-cpt"))) {
    remove_argument (N, argc, argv);
    selection[0].name = strcreate (argv[N]);
    remove_argument (N, argc, argv);
    return selection;
  }    

  /* check for Region list (file containing dvo catalog file list)*/
  if ((N = get_argument (*argc, argv, "-cptlist"))) {
    remove_argument (N, argc, argv);
    selection[0].list = strcreate (argv[N]);
    remove_argument (N, argc, argv);
    return selection;
  } 

  /* check for Region selection from display */
  if ((N = get_argument (*argc, argv, "-region"))) {
    remove_argument (N, argc, argv);
    selection[0].useDisplay = TRUE;
    return selection;
  }    

  /* check for Region selection from display */
  if ((N = get_argument (*argc, argv, "-skyregion"))) {
    if (N + 5 >= *argc) {
      gprint (GP_ERR, "USAGE: -skyregion (RA) (RA) (DEC) (DEC)\n");
      FreeSkyRegionSelection (selection);
      return NULL;
    }
    remove_argument (N, argc, argv);
    selection[0].useSkyregion = TRUE;
    set_skyregion (atof(argv[N]), atof(argv[N+1]), atof(argv[N+2]), atof(argv[N+3]));
    remove_argument (N, argc, argv);
    remove_argument (N, argc, argv);
    remove_argument (N, argc, argv);
    remove_argument (N, argc, argv);
    return selection;
  }    

  /* default to pre-defined sky region */
  selection[0].useSkyregion = TRUE;
  return selection;
}

/* given possible options (by name, by list, by graph region), select SkyRegions */
SkyList *SelectRegions (SkyRegionSelection *selection) {

  SkyList *skylist;

  /* determine region-file names */
  if (selection->name != NULL) {
    char filename[256];

    ALLOCATE (skylist, SkyList, 1);
    ALLOCATE (skylist[0].regions, SkyRegion *, 1);
    ALLOCATE (skylist[0].regions[0], SkyRegion, 1);
    ALLOCATE (skylist[0].filename, char *, 1);
    skylist[0].ownElements = TRUE; // free these elements when freeing the list
    
    strcpy (skylist[0].regions[0][0].name, selection->name);
    sprintf (filename, "%s/%s.cpt", CATDIR, selection->name);
    skylist[0].filename[0] = strcreate (filename);
    return (skylist);
  } 

  if (selection->list != NULL) {
    skylist = SkyListLoadFile (selection->list);
    return (skylist);
  }

  if (selection->useDisplay) {
    double Radius;
    Graphdata graphsky;

    if (!GetGraphData (&graphsky, NULL, NULL)) {
      gprint (GP_ERR, "region display not available\n");
      return (NULL);
    }

    Radius = MAX (fabs(graphsky.xmax), fabs(graphsky.ymax));
    skylist = SkyListByRadius (sky, -1, graphsky.coords.crval1, graphsky.coords.crval2, Radius);
    return (skylist);
  }

  if (selection->useSkyregion) {
    double Rmin, Rmax, Dmin, Dmax;

    get_skyregion (&Rmin, &Rmax, &Dmin, &Dmax);
    skylist = SkyListByBounds (sky, -1, Rmin, Rmax, Dmin, Dmax);
    return (skylist);
  }    

  return NULL;
}

/* returns a list of region files names from file */
SkyList *SkyListLoadFile (char *filename) {
  
  FILE *f;
  int NREGIONS, Nregions;
  SkyList *skylist;

  ALLOCATE (skylist, SkyList, 1);

  f = fopen (filename, "r");
  if (f == NULL) {
    gprint (GP_ERR, "ERROR: can't find region list file %s\n", filename);
    skylist[0].Nregions = 0;
    skylist[0].regions = NULL;
    return (skylist);
  }
  
  Nregions = 0;
  NREGIONS = 50;
  ALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS);
  ALLOCATE (skylist[0].filename, char *, NREGIONS);
  skylist[0].ownElements = TRUE; // free these elements when freeing the list

  while (fscanf (f, "%s", filename) != EOF) {
    ALLOCATE (skylist[0].regions[Nregions], SkyRegion, 1);
    strcpy (skylist[0].regions[Nregions][0].name, filename);
    sprintf (filename, "%s/%s.cpt", CATDIR, skylist[0].regions[Nregions][0].name);
    skylist[0].filename[Nregions] = strcreate (filename);
    Nregions ++;
    CHECK_REALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS, Nregions, 50);
  }
  skylist[0].Nregions = Nregions;
  return (skylist);
}

