# include <kapa_internal.h>

int KiiResize (int fd, int Nx, int Ny) {

  KiiSendCommand (fd, 4, "RSIZ");
  KiiSendMessage (fd, "%d %d", Nx, Ny); 
  return (TRUE);
}

int KiiCenter (int fd, double x, double y, int zoom) {

  KiiSendCommand (fd, 4, "CENT");
  KiiSendMessage (fd, "%8.3f %8.3f %8d ", x, y, zoom);
  return (TRUE);
}

int KapaBox (int fd, Graphdata *graphdata) {

  KiiSendCommand (fd, 4, "DBOX");

  KiiSendMessage (fd, "%12.6g %12.6g %12.6g %12.6g", 
		    graphdata[0].xmin, graphdata[0].xmax, graphdata[0].ymin, graphdata[0].ymax);

  KiiSendMessage (fd, "%s %s %s", graphdata[0].axis, graphdata[0].labels, graphdata[0].ticks);
  return (TRUE);
}

int KapaClear (int fd, int ClearSection) {

  if (ClearSection) {
    KiiSendCommand (fd, 4, "ERAS");
  } else {
    KiiSendCommand (fd, 4, "ERSS");
  }
  return (TRUE);
}

int KapaInitGraph (Graphdata *graphdata) {

  graphdata[0].xmin = graphdata[0].ymin = 0.0;
  graphdata[0].xmax = graphdata[0].ymax = 1.0;
  graphdata[0].style = graphdata[0].ptype = 0;
  graphdata[0].ltype = graphdata[0].color = 0;
  graphdata[0].etype = graphdata[0].ebar = 0;
  graphdata[0].lweight = graphdata[0].size = 1.0;
    
  graphdata[0].coords.pc1_1 = graphdata[0].coords.pc2_2 = 1.0;
  graphdata[0].coords.pc1_2 = graphdata[0].coords.pc2_1 = 0.0;
  strcpy (graphdata[0].coords.ctype, "RA---LIN");
  graphdata[0].coords.crval1 = 0.0;
  graphdata[0].coords.crval2 = 0.0;
  graphdata[0].coords.crpix1 = 0.0;
  graphdata[0].coords.crpix2 = 0.0;
  graphdata[0].coords.cdelt1 = graphdata[0].coords.cdelt2 = 1.0;
  graphdata[0].flipeast = TRUE;
  graphdata[0].flipnorth = FALSE;
  strcpy (graphdata[0].axis, "2222");
  strcpy (graphdata[0].ticks, "2222");
  strcpy (graphdata[0].labels, "2222");

  return (TRUE);
}

int KapaPrepPlot (int fd, int Npts, Graphdata *graphmode) {

  /* tell kapa to look for the incoming image */
  KiiSendCommand (fd, 4, "PLOT"); 
  
  /* send Xgraph the plot details */
  KiiSendMessage (fd, "%8d %8d %d %d %d %d %d %f %f", 
		  Npts, graphmode[0].style, 
		  graphmode[0].ptype, graphmode[0].ltype, 
		  graphmode[0].etype, graphmode[0].ebar, graphmode[0].color, 
		  graphmode[0].lweight, graphmode[0].size);
  KiiSendMessage (fd, "%g %g %g %g", 
		  graphmode[0].xmin, graphmode[0].xmax, 
		  graphmode[0].ymin, graphmode[0].ymax);
  return (TRUE);
}

int KapaPlotVector (int fd, int Npts, float *values) {

  int Nbytes;

  Nbytes = Npts * sizeof (float);
  write (fd, values, Nbytes);
  return (TRUE);
}

int KapaSetFont (int fd, char *name, int size) {

  KiiSendCommand (fd, 4, "FONT");
  KiiSendCommand (fd, 16, "%s", name);
  KiiSendCommand (fd, 16, "%d", size);
  return (TRUE);
}

int KapaSendLabel (int fd, char *string, int mode) {
  
  KiiSendCommand (fd, 4, "LABL");
  KiiSendMessage (fd, "%6d", mode);
  KiiSendData (fd, string, strlen(string));
  return (TRUE);
}

int KapaSendTextline (int fd, char *string, float x, float y, float angle) {
  
  KiiSendCommand (fd, 4, "PTXT");
  KiiSendMessage (fd, "%f %f %f", x, y, angle);
  KiiSendData (fd, string, strlen(string));
  return (TRUE);
}

int KapaSetLimits (int fd, Graphdata *graphmode) {

  KiiSendCommand (fd, 4, "SLIM");
  KiiSendMessage (fd, "%g %g %g %g ", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
  return (TRUE);
}

int KapaSetSection (int fd, KapaSection *section) {

  KiiSendCommand (fd, 4, "DSEC");
  KiiSendMessage (fd, "%s %6.3f %6.3f %6.3f %6.3f", 
		  section[0].name, 
		  section[0].x,
		  section[0].y,
		  section[0].dx,
		  section[0].dy);
  return (TRUE);
}

int KapaSelectSection (int fd, char *name) {

  KiiSendCommand (fd, 4, "SSEC");
  KiiSendMessage (fd, "%s", name);
  return (TRUE);
}

int KapaGetSection (int fd, char *name) {

  KiiSendCommand (fd, 4, "LSEC");
  KiiSendMessage (fd, "%s", name);
  return (TRUE);
}
