# include "Ximage.h"

# define DrawLine(X,Y,DX,DY) (XDrawLine (graphic->display, graphic->window, graphic->gc, (int)(X), (int)(Y), (int)(X+DX), (int)(Y+DY)))
  
int DrawFrame (KapaGraphWidget *graph) {
  
  int i, j, Nticks, P;
  double fx, fy, dfx, dfy, lweight;
  Graphic *graphic;
  TickMarkData *ticks;
  int color;

  graphic = GetGraphic();

  /* each axis is drawn independently, but ticks and labels are placed according to perpendicular distance. */
  for (i = 0; i < 4; i++) {
    fx  = graph[0].axis[i].fx;
    fy  = graph[0].axis[i].fy;
    dfx = graph[0].axis[i].dfx;
    dfy = graph[0].axis[i].dfy;
    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);

    lweight = MAX (0, MIN (10, graph[0].axis[i].lweight));
    color = MAX (0, MIN (15, graph[0].axis[i].color));

    XSetLineAttributes (graphic->display, graphic->gc, lweight, LineSolid, CapNotLast, JoinMiter);
    XSetForeground (graphic->display, graphic->gc, graphic->color[color]);
    DrawRotTextInit (graphic->display, graphic->window, graphic->gc, graphic->color[color], graphic->back);

    if (graph[0].axis[i].isaxis) {
      DrawLine (fx, fy, dfx, dfy);
    }
    
    if (graph[0].axis[i].areticks) {
      ticks = CreateAxisTicks (&graph[0].axis[i], &Nticks);
      for (j = 0; j < Nticks; j++) {
	DrawTick (graphic, &graph[0].axis[i], P, &ticks[j], i);
      }
      FREE (ticks);
    }
  }
  return (TRUE);
}

void DrawTick (Graphic *graphic, Axis *axis, int P, TickMarkData *tick, int naxis) {
  
  double x, y, dx, dy;
  int pos, dir, fontsize;
  double size, n, pad;
  char string[64], *fontname;

  double fx  = axis->fx;
  double fy  = axis->fy;
  double dfx = axis->dfx;
  double dfy = axis->dfy;

  double min = axis->min;
  double max = axis->max;

  double value = tick->value;

  pos = size = 0;

  if (tick->IsMajor) { 
    size = MAX (0.02, 7.0 / P); 
  } else {
    size = MAX (0.01, 4.0 / P); 
  }
  
  n = P / hypot(dfx, dfy);
  x = fx + (value-min)*dfx/(max - min);
  y = fy + (value-min)*dfy/(max - min);

  if ((naxis == 0) || (naxis == 2)) {
    x = MAX(x, fx);
  }
  //   y = MAX(y, fy);
  // }

  dir = ((naxis == 0) || (naxis == 1)) ? -1 : +1;
  dx = dir*size*dfy*n;	
  dy = dir*size*dfx*n;
  
  DrawLine (x, y, dx, dy);

  if (tick->IsLabel) {
    int xt, yt;

    fontname = GetRotFont (&fontsize);
    
    pad = !isnan(axis->ticktextPad) ? axis->ticktextPad*fontsize : fontsize + 4.0;
    // pad = !isnan(axis->ticktextPad) ? axis->ticktextPad*fontsize : 0.8*fontsize + 1.0;
    
    /* temporarily assume rectilinear axes */
    if (naxis == 0) { dx = 0; dy = +1.0*pad; pos = 7; }
    if (naxis == 2) { dx = 0; dy = -1.0*pad; pos = 1; }

    if (naxis == 1) { dy = 0; dx = -0.5*pad; pos = 3; }
    if (naxis == 3) { dy = 0; dx = +0.5*pad; pos = 5; }

    xt = fx + (value-min)*dfx/(max - min) + dx;
    yt = fy + (value-min)*dfy/(max - min) + dy;
    if (fabs(value/(max-min)) < 0.001) { value = 0.0; }
    sprintf (string, "%4g", value);
    DrawRotText (xt, yt, string, pos, 0.0);
  }
}

# define MIN_RANGE 1e-10

void AxisTickScale (Axis *axis, double *major, double *minor) {

  double range, lrange, factor, mantis, fmantis, power;

  range = axis[0].max - axis[0].min;
  if (fabs(range) < MIN_RANGE) {
    range = (range < 0) ? -MIN_RANGE : +MIN_RANGE;
  }

  lrange = log10(MAX(fabs(range), MIN_RANGE));
  mantis = modf (lrange, &factor);
  if (mantis < 0.0) {
    mantis += 1.0;
    factor -= 1.0;
  }
  
  power = MAX(pow(10.0, factor), MIN_RANGE);
  fmantis = pow(10.0, mantis);
  *major = MAX(0.5 * power, MIN_RANGE);
  *minor = MAX(0.1 * power, MIN_RANGE);
  
  if ((fmantis >= 1.0) && (fmantis <  1.999)) {
    *major = 0.5 * power;
    *minor = 0.1 * power;
    if (axis[0].areticks == 1) {
      *major = 1.0 * power;
      *minor = 0.2 * power;
    }	  
  }
  if ((fmantis >= 1.999) && (fmantis <  3.999)) {
    *major = 1.0 * power;
    *minor = 0.2 * power;
    if (axis[0].areticks == 1) {
      *major = 2.0 * power;
      *minor = 0.5 * power;
    }	  
  }
  if ((fmantis >= 3.999) && (fmantis <  5.999)) {
    *major = 1.0 * power;
    *minor = 0.5 * power;
    if (axis[0].areticks == 1) {
      *major = 2.0 * power;
      *minor = 0.5 * power;
    }	  
  }
  if ((fmantis >= 5.999) && (fmantis <   7.999)) {
    *major = 2.0 * power;
    *minor = 0.5 * power;
    if (axis[0].areticks == 1) {
      *major = 2.5 * power;
      *minor = 0.5 * power;
    }	  
  }
  if ((fmantis >= 7.999) && (fmantis <  10.000)) {
    *major = 2.5 * power;
    *minor = 0.5 * power;
    if (axis[0].areticks == 1) {
      *major = 5.0 * power;
      *minor = 1.0 * power;
    }	  
  }
}

TickMarkData *CreateAxisTicks (Axis *axis, int *nticks) {

  TickMarkData *ticks;
  double range, major, minor, first, value, dPixels, overshoot;
  int i, NTICKS, nPixels, done, ifirst;

  *nticks = 0;

  if (isnan(axis->min) || isinf(axis->min)) return NULL;
  if (isnan(axis->max) || isinf(axis->max)) return NULL;
  
  range = axis->max - axis->min;

  // not sure why I skip out here instead of using the MIN_RANGE value
  if (fabs(range) < MIN_RANGE) return NULL;
  
  if (fabs(range) < MIN_RANGE) {
    range = (range < 0) ? -MIN_RANGE : +MIN_RANGE;
  }

  // length of the axis in pixels
  nPixels = hypot(axis->dfx, axis->dfy);
  dPixels = nPixels / range; // axis pixel-scale

  AxisTickScale (axis, &major, &minor);

  // be a little generous 
  NTICKS = (int)(fabs(range / minor)) + 2;
  ALLOCATE (ticks, TickMarkData, NTICKS);

  // value of the tick mark
  ifirst = (axis->min < 0.0) ? (axis->min/minor - 0.05) : (axis->min/minor + 0.05);
  first = minor*ifirst;
  
  // allow undershoot by 1 pixel
  overshoot = (axis->min - first) * dPixels;
  // if (overshoot > 0.5) {
  //   if (range > 0) {
  //     first += minor;
  //   } else {
  //     first -= minor;
  //   }
  // }

  // loop to find the ticks
  done = FALSE;
  value = first; 
  for (i = 0; !done && (i < NTICKS); i++) {
    ticks[i].IsMajor = FALSE;
    ticks[i].IsMajor |= (fabs((int)(value/major) - (value/major)) < 0.5*(minor/major));
    ticks[i].IsMajor |= (fabs((int)((value + 0.5*minor)/major) - (value/major)) < 0.5*(minor/major));
    ticks[i].IsMajor |= (fabs((int)((value - 0.5*minor)/major) - (value/major)) < 0.5*(minor/major));
    ticks[i].IsLabel = (ticks[i].IsMajor && axis->islabel);
    ticks[i].value = value;
    if (range > 0) 
      value += minor;
    else 
      value -= minor;
    
    done |= (range > 0) && (value > axis->max);
    done |= (range < 0) && (value < axis->max);
  }

  *nticks = i;
  return (ticks);
}
