/*
* Copyright (C) 2001-2008, CompHEP Collaboration
* Copyright (C) 2000, Alexander Pukhov 
* ------------------------------------------------------
* $Id: param.c,v 1.5 2003/11/08 19:46:05 kryukov Exp $
*
* $Log: param.c,v $
* Revision 1.5  2003/11/08 19:46:05  kryukov
* To bring into according the usage of messanykey and warnanykey functions.
*
* Revision 1.4  2003/06/25 18:46:41  kryukov
* *** empty log message ***
*
* Revision 1.3  2003/04/22 09:01:19  kryukov
* Apply indent to improve readability of files
*
* Revision 1.2  2003/04/20 17:55:02  kryukov
* Clean up warnings during make
*/
#include <stdio.h>

#include "service2/include/chep_limits.h"
#include "chep_crt/include/chep_crt.h"
#include "out_ext.h"

#include "param.h"

/* ************************************************* */
/* Physics model parameters menu                     */
/* ************************************************* */

static void 
param_menu (int sqtrS_on, int vars_on, int func_on, char **strmen)
{
  int k;
  double val;
  char name[10];

  int pos;
  int npos = 0;
/* **  menu loop */


  if (vars_on)
    npos += nvar_;
  if (func_on)
    npos += nfunc_;

  if (npos == 0)
    {
      *strmen = NULL;
      return;
    }
  *strmen = malloc (24 * (npos + 1) + 2);
  (*strmen)[0] = '\030';

  pos = 1;
  for (k = !sqtrS_on; k <= nvar_ + nfunc_; ++k)
    {
      if (k == 0 || (vars_on && k <= nvar_) || (func_on && k > nvar_))
        {
          char c = ' ';
          vinf_ (k, name, &val);
          if (k > nvar_ && vars_on)
            c = '*';
          sprintf ((*strmen) + pos, "%c%7s= %-14.5g", c, name, val);
          pos += 24;
        }
    }
  (*strmen)[pos] = 0;
}


void 
selectParam (int *position, int x, int y, int sqtrS_on, int vars_on, int func_on,
             char *mess, void **pscrPtr)
{
  char *strmen;
  void *pscr;

  if (pscrPtr)
    pscr = *pscrPtr;
  else
    pscr = NULL;

  param_menu (sqtrS_on, vars_on, func_on, &strmen);
  if (strmen)
    {
      menu1 (x, y, mess, strmen, "", &pscr, position);
      free (strmen);

      if (*position == 0)
        {
          if (pscrPtr)
            *pscrPtr = NULL;
          *position = -1;
          return;
        }
      if (pscrPtr)
        *pscrPtr = pscr;
      else
        put_text (&pscr);
      if (sqtrS_on)
        (*position)--;
      if (!vars_on)
        *position += nvar_;
    }
  else
    *position = -1;
}

int change_parameter (int x, int y) {
  double val;
  char name[20];
  int first = 1;
  int returnCode = 0;
  void *pscr = NULL;
  int m;

  do {
    if (!first)
      warnanykey (15, 15, " Wrong parameters\n Can not evaluate constraints");
    for (m = 0; m >= 0;) {
      selectParam (&m, x, y, 0, 1, 0, "Change parameter", &pscr);
      if (m >= 0) {
        vinf_ (m, name, &val);
        strcat (name, " = ");
        if (correctDouble (x, y + 4, name, &val, 1)) {
          asgn_ (m, val);
          returnCode = 1;
        }
      }
    }
    first = 0;
  } while (calcFunc ());
  return returnCode;
}
