/* 
   * Copyright (C) 1997, Alexander Pukhov 
   * Copyright (C) 2001-2003, CompHEP Collaboration
   *------------------------------------------------------
   * $Id: showgrph2.c,v 1.4 2003/06/25 18:46:42 kryukov Exp $
   *
   * $Log: showgrph2.c,v $
   * Revision 1.4  2003/06/25 18:46:42  kryukov
   * *** empty log message ***
   *
   * Revision 1.3  2003/04/22 09:01:20  kryukov
   * Apply indent to improve readability of files
   *
   * Revision 1.2  2003/04/20 17:55:02  kryukov
   * Clean up warnings during make
   *
 */
#include <math.h>
#include "chep_crt/include/chep_crt.h"
#include "service2/include/os.h"
#include "service2/include/tptcmac.h"
#include "service2/include/syst2.h"
#include "service2/include/s_files.h"

#include "symb/include/physics.h"
#include "symb/include/r_code.h"
#include "symb/include/drawdiag.h"
#include "symb/include/showgrph2.h"
#include "symb/include/draw_ampl.h"
#include "symb/include/process.h"
#include "symb/include/process_core.h"
#include "symb/include/diaprins.h"
/*======================================================================*/
  /*
     nn   - first FilePos of subproces;
     nm-1 - last  FilePos of subproces;
   */

static char stc[80];
static long nn, nm;
static int pictureX;
static int pictureY;
static int xn, ynu;

static FILE *diagrFile;
static int diagSize;
static char *mark;
static int squared;

static void 
wrttext (int n, int x, int y)
{
  int ypos = y + ynu - 2;

  tg_settextjustify (LeftText, BottomText);
  switch (mark[n - 1])
    {
    case -1:
      scrcolor (Red, White);
      tg_outtextxy (x + 48, ypos, "DEL");
      break;
    case -2:
      scrcolor (Red, White);
      tg_outtextxy (x + 48, ypos, "Out of memory");
      break;
    case 1:
      scrcolor (Blue, White);
      tg_outtextxy (x + 48, ypos, "CALC");
      break;
    case 2:
      scrcolor (Blue, White);
      tg_outtextxy (x + 48, ypos, "ZERO ");
    }
  scrcolor (Black, White);
}


static void 
new_picture (int n, int x, int y)
{
  char buff[MAX (sizeof (csdiagram), sizeof (adiagram))];
  vampl amplDiagram;

  wrttext (n, x, y);
  n += (nn - 1);
  fseek (diagrFile, n * diagSize, SEEK_SET);
  fread (buff, diagSize, 1, diagrFile);

  mkverts ((particleNumType *) buff, &amplDiagram);

  drawAmpitudeDiagram (&amplDiagram, x, y);

/*   writeAmDiagram( &amplDiagram , 0,' ',stdout); */
}

/* tex tex tex tex tex tex tex tex tex tex */

static void 
texinvoke (char *pname)
{
  char buff[MAX (sizeof (csdiagram), sizeof (adiagram))];
  char dd[30];
  int n_, dn_;
  int flag;

  char f_name[STRSIZ];

  if (squared)
    strcpy (dd, "csd_");
  else
    strcpy (dd, "fd_");
  nextFileName (f_name, dd, ".tex");
  strcat (f_name, ".tex");
  texStart (f_name, scat ("diagrams for process %s", pname), "tiny");

  texxscale = 1;
  texyscale = 1;

  setPictureScale (squared, &pictureX, &pictureY);

  texxshift = 0;
  texymax1 = pictureY;


  dn_ = 0;

  for (n_ = nn; n_ < nm; n_++)
    {
      flag = (mark[n_ - nn] != -1);
      if (flag)
	{
	  fseek (diagrFile, n_ * diagSize, SEEK_SET);
	  fread (buff, diagSize, 1, diagrFile);
	  dn_++;
	  f_printf (out_tex, "{} \\qquad\\allowbreak\n");
	  f_printf (out_tex, "%%  diagram # %d\n", (n_ - (int) nn) + 1);
	  f_printf (out_tex, "\\begin{picture}(%d,%d)(0,0)\n", pictureX, pictureY);
	  picture (squared, buff, 0, 0);
	  f_printf (out_tex, "\\Text(%d,0)[b] {diagr.%d}\n", pictureX / 2, dn_);
	  f_printf (out_tex, "\\end{picture} \\ \n");
	}
    }

  texFinish ();
  setPictureScale (squared, &pictureX, &pictureY);
  messanykey (20, 14, scat ("LaTeX output is written in file\n %s", f_name));

}


/* end tex */

static int 
comList (int n, char key)
{
  int l;

  switch (key)
    {

    case 'L':			/* LaTex output for all undel. diagrs. */
      texinvoke (stc);
      return 0 /*nothing to redraw */ ;

    case 'D':			/* Del -   Delete all subprocess  */
      for (l = 0; l < nm - nn; l++)
	{
	  if (mark[l] == 0 || mark[l] == -2)
	    mark[l] = -1;
	}
      return 2 /* redrawScreen */ ;

    case 'R':			/*  Ins -  Restore all deleted  */
      for (l = 0; l < nm - nn; l++)
	{
	  if (mark[l] == -1)
	    mark[l] = 0;
	}
      return 2 /* redrawScreen */ ;

    case 'O':			/*  Space  Bar   del/ins proceess  */
      if (mark[n - 1] == 0 || mark[n - 1] == -2)
	mark[n - 1] = -1;
      else if (mark[n - 1] == -1)
	mark[n - 1] = 0;
      return 1;

    case 'G':
      {
	FILE *txt;
	makeghostdiagr (nn + n - 1, "view.tmp");
	txt = fopen ("view.tmp", "r");
	showtext (1, 1, 80, 1, "Ghosts", txt);
	fclose (txt);
	unlink ("view.tmp");
	return 0;
      }
    }
  fprintf (stderr, "***** comList: End reach. No int val. ?!\n");
  return 0;
}


int 
showgraphs2 (char upravl)
{
  int ndel, nrest, ncalc;

  int u;
  int onlyview;
  int i;
  int delMarkPos;
  int ntot;
  char help[80];
  char comLine[80];

  onlyview = (upravl < 0);
  if (onlyview)
    upravl = -upravl;

  squared = (upravl > 1);

  if (squared)
    setPictureScale (squared, &xn, &ynu);
  else
    {
      int numin = getnin ();
      int numout = getnout ();
      xn = amplitudeFrameX (numin, numout);
      ynu = amplitudeFrameY (numin, numout);
    }


  {
    adiagram varc;
    csdiagram ars;

    if (squared)
      {
	menuq = fopen (MENUQ_NAME, "r+b");
	diagrFile = fopen (DIAGRQ_NAME, "r+b");
	diagSize = sizeof (csdiagram);
	delMarkPos = (char *) &(ars.status) - (char *) &ars;
      }
    else
      {
	menup = fopen (MENUP_NAME, "r+b");
	diagrFile = fopen (DIAGRP_NAME, "r+b");
	diagSize = sizeof (adiagram);
	delMarkPos = (char *) &(varc.delMark) - (char *) &varc;
      }
  }

/*  nn   -  Position of first diagrams in subproces  */
  if (1 == upravl) {
    rd_menu (menup, upravl, nsub, stc, &ndel, &ncalc, &nrest, &nn);
  } else {
    rd_menu (menuq, upravl, nsub, stc, &ndel, &ncalc, &nrest, &nn);
  }
  ntot = ndel + ncalc + nrest;
  nm = nn + ntot;

  if (squared)
    {
      strcpy (help, "s_diag_s");
      strcpy (comLine, "Delete,On/off,Restore,Latex,Ghosts");
    }
  else if (onlyview)
    {
      strcpy (help, "s_diag_v");
      strcpy (comLine, "Latex");
    }
  else
    {
      strcpy (help, "s_diag_e");
      strcpy (comLine, "Delete,On/off,Restore,Latex");
    }

  mark = malloc (ntot);
  for (i = 0; i < ntot; i++)
    mark[i] = 0;


  for (i = nn; i < nm; i++)
    {
      fseek (diagrFile, i * diagSize + delMarkPos, SEEK_SET);
      fread (mark + i - nn, 1, 1, diagrFile);
    }

  pictures (ntot, xn, ynu, new_picture, comLine, comList, help);

  ndel = 0;
  ncalc = 0;
  for (i = nn; i < nm; i++)
    {
      fseek (diagrFile, i * diagSize + delMarkPos, SEEK_SET);
      fwrite (mark + i - nn, 1, 1, diagrFile);
      if (mark[i - nn] >= 1)
	ncalc++;
      else if (mark[i - nn] == -1)
	ndel++;
    }

  nrest = ntot - ndel - ncalc;
  if (1 == upravl) {
    wrt_menu (menup, upravl, nsub, stc, ndel, ncalc, nrest, nn);
  } else {
    wrt_menu (menuq, upravl, nsub, stc, ndel, ncalc, nrest, nn);
  }

  if (squared)
    fclose (menuq);
  else
    fclose (menup);
  free (mark);
  fclose (diagrFile);
  return u;
}
