// Includo stdio senno' dice che ridefinisco FILE
#include <stdio.h>
#include <fstream.h>

#include <applicat.h>              
#include <files.h>
#include <utility.h>
#include <expr.h>
#include <isam.h>
#include <mask.h>
#include <progind.h>
#include <sheet.h>
#include <msksheet.h>
#include <urldefid.h>
#include <validate.h>

#include "ba1100.h"

TMask*       TRec_sheet::_mask = NULL;
HIDDEN       TToken_string s(256);


void TDir_sheet::add ()

{
  _dir->get(LF_DIR, _lock, _nordir, _sysdirop);
  const int nitems = (int)_dir->eod() + 1;

  _dir->eod() = nitems;   
  if (_dir->eox() < _dir->eod())
    _dir->eox() += 10;
  _dir->put(LF_DIR, _nordir, _sysdirop);  
  TDir d;
  
  d.zero();
  d.put(nitems, _nordir, _sysdirop);
  _items = nitems;
  
  build_page(first() + 1);
  update();
}

TDir_sheet::TDir_sheet(const char* title, byte buttons, const char* colonne)
:TSheet(-1,-1, 0, 0, title, colonne, buttons)
{
  _dir = new TDir;
  _rec = new TTrec;
  
  maximize();
  rebuild();
}

TDir_sheet::~TDir_sheet()
{
  delete _dir;
  delete _rec;
}


bool TDir_sheet::on_key(KEY key)
{                                        
  if (key > K_CTRL)
    switch (key-K_CTRL)
    {
    case 'A':
      key = K_F8; break;
    case 'C':
      key = K_F7; break;
    case 'E':
      key = K_DEL; break;
    case 'I':
      key = K_F6; break;
    case K_F4:
      key = K_ESC; break;
    default:
      break;
    }
  return TSheet::on_key(key);
}

void TDir_sheet::page_build(long first, byte rows)

{
  TToken_string l(128);

  for (byte i = 0; i < rows; i++)
  {
    const int n = int(i+first+1);
    _dir->get (n,_nolock,_nordir,_sysdirop);

    l = format("%3d", n); // Numero progressivo del file
    l.add(_dir->name());
    const TRecnotype eod = _dir->eod();
    l.add(eod);
    const TRecnotype eox = _dir->eox();
    l.add(eox);
    const word len = _dir->len();
    l.add(format("%u", len));
    l.add(_dir->des());
    l.add(_dir->expr());
    l.add(_dir->flags());
    set_row(l, i);
  }
}


TRec_sheet::TRec_sheet(int logicnum, const char * tab)
: _descr(NULL), _tab(tab)

{
  _dir = new TDir;
  _rec = new TTrec;
  _rec_old = new TTrec;
  _mask = new TMask("ba1100d");

  _dir->get(logicnum, _lock, _nordir, _sysdirop);
  _rec->get(logicnum);
  if (_dir->len() == 0)
   _rec->zero();
  *_rec_old = *_rec;
  _tab.lower();
  if (fexist(DESCDIR))
  {
    if (logicnum >= LF_TABGEN && logicnum <= LF_TAB && _tab.not_empty())
      _descfname.format("%s/d%s.des", DESCDIR, (const char *) _tab);
    else
      _descfname.format("%s/d%d.des", DESCDIR, _dir->num());
    FILE * fd = NULL;

    if (!fexist(_descfname) && (fd = fopen(_descfname, "w")) == NULL)
      error_box("Impossibile creare il file delle descrizioni");
    else
    {
      if (fd != NULL)
        fclose(fd);
      _descr = new TConfig(_descfname, DESCPAR);
    }
  }
}

TRec_sheet::~TRec_sheet()
{
  _dir->get(_dir->num(), _unlock, _nordir, _sysdirop);
  delete _dir;
  delete _rec;
  delete _rec_old;
  delete _mask;
  _mask = NULL;
  if (_descr)
    delete _descr;
}

bool TRec_sheet::check_key_expr(int key, const char * key_expr)

{
  TExpression expr("", _strexpr);
  return expr.set(key_expr, _strexpr);
}

HIDDEN bool len_handler(TMask_field& f, KEY key)

{
  const int len = atoi(f.get());

  if (len < 0) return FALSE;

  const int typef = atoi(f.mask().get(FLD_TIPO));

  switch (typef)
  {
  case _alfafld: 
    return len <= 254;
  case _intfld: 
    return len <= 5;
  case _longfld: 
    return len <= 10;
  case _realfld:  
    return len <= 18;
  case _wordfld:  
    return len <= 5;
  case _intzerofld:  
    return len <= 5;
  case _longzerofld:  
    return len <= 10;
  default: 
    return TRUE;
  }
  return TRUE;
}


bool TRec_sheet::key_notify(TSheet_field& f, int r, KEY k)
{
  if (k == K_INS)
  {                   
    const int items = f.items();
    f.disable_cell(0, 1); 
    if (f.items() >= 8) return FALSE;
  }    
  return TRUE;
}

void TRec_sheet::save()

{
  if ((*_rec == *_rec_old && !_descr) ||
      !yesnocancel_box("Salvare le modifiche")) return;
  TSystemisamfile f(_rec->num());
  
  if (_descr)
  {
    TSheet_field& f1 = (TSheet_field&) _mask->field(F_FIELDS);
    const int nfields = f1.items();

    for (int i = 0; i < nfields; i++)
      _descr->set(_rec->rec()->Fd[i].Name, f1.row(i).get(-2));
    delete _descr;
    _descr = new TConfig(_descfname, DESCPAR);
  }
  f.update(*_rec);
  *_rec_old = *_rec;
}


void TRec_sheet::edit()

{
  TSheet_field& f1 = (TSheet_field&) _mask->field(F_FIELDS);
  TSheet_field& f2 = (TSheet_field&) _mask->field(F_KEYS);

  _mask->set (F_NUM, _dir->num());
  _mask->set (F_DES, _dir->des());
  f1.sheet_mask().field(FLD_LEN).set_handler(len_handler);
  f1.set_append(FALSE);
  int nfields = _rec->fields();

  f1.enable_column(FLD_DES - 101, _descr != NULL);
  for (int i = 0; i < nfields; i++)
  {
    f1.row(i) = _rec->fielddef(i);
    if (_descr)
      f1.row(i).add(_descr->get(_rec->rec()->Fd[i].Name));
    else
      f1.row(i).add("");
  }
  f2.set_notify(key_notify);
  f2.set_append(FALSE);
  int nkeys = _rec->keys();
  for (i = 0; i < nkeys; i++) f2.row(i) = _rec->keydef(i);
  f2.disable_cell(0, 1);
  while (TRUE)
  {
    switch (_mask->run())
    {
    case K_SAVE:
    {
      nfields = f1.items();   
      int nf = 0;
      for (i = 0; i < nfields; i++)
      {
        TToken_string s(f1.row(i));
        
        s.destroy(-2);     
        if (s.items() > 0)
          _rec->update_fielddef(nf++, s);
      }
      _rec->set_fields(nf);
      _rec->rehash();
      nkeys = f2.items();
      _rec->set_keys(nkeys);
      for (i = 0; i < nkeys; i++)
      {
        TToken_string& s = f2.row(i);
        _rec->update_keydef(i, s);
      }
      save();
    }
  case K_ESC:
    return;
  case K_ENTER:
    main_app().dispatch_e_menu(M_FILE_PRINT);
    break;
  case K_F6:
  {
    TMask m("ba1100f");
    TFilename nout(_dir->name());

    nout.strip("$%");
    nout.ext("trr");
    m.set(F_NOMEF, nout);  
    if (m.run() == K_ENTER)
    {
      TString80 nf(m.get(F_NOMEF));
      
      if (nf.not_empty())
      {
        ofstream out((const char*) nf);
        
        out << *_rec;
      }
    }
  }
    break;
  case K_F7:
  {
    TMask m("ba1100f");
    TFilename nout(_dir->name());

    nout.strip("$%");
    nout.ext("trr");
    m.set(F_NOMEF, nout);  
    if (m.run() == K_ENTER)
    {
      TString80 nf(m.get(F_NOMEF));
      
      if (nf.not_empty())
      {
        ifstream in((const char*) nf);
        
        in >> *_rec;
        nfields = _rec->fields();
        f1.reset();
        for (i = 0; i < nfields; i++) f1.row(i) = _rec->fielddef(i);
        nkeys = _rec->keys();
        f2.reset();
        for (i = 0; i < nkeys; i++) f2.row(i) = _rec->keydef(i);
        f2.disable_cell(0, 1);
      }
    }
  }
    break;
  default: break;
  }
  }
}

