#include <mask.h>
#include <printapp.h>
#include <relation.h>
#include <tabutil.h>
#include <utility.h>

#include "ba3.h"
#include "ba3300.h"

class BA3300_application : public TPrintapp
{
  friend bool codice_handler(TMask_field& field, KEY key);  
  
  TMask*  _msk;
  TTable* _tab;        // Tabella dei registri
  TString _cod_reg, _comulc, _ragsoc, _indulc, _civulc, _capulc, _com, _prov, _cofi, _paiv, _tipoa;
  long _codice_ditta, _agg, _codanagr;
  long _num_pag, _pag_ini, _pag_fin; 
  int _cod_anno;
  bool _inizia_dopo;
  TLocalisamfile*   _nditte;
  TLocalisamfile*   _anag;
  TLocalisamfile*   _unloc;
  TLocalisamfile*   _comuni;  

public:
  
  bool menu (MENU_TAG m) { return TPrintapp::menu(m) ; }
  virtual bool user_destroy();
  virtual bool user_create() ;
  bool set_print(int);

  const char* decodifica_reg ();

  virtual void set_page (int, int);
  virtual print_action postprocess_page (int, int);
  
  BA3300_application();
};

print_action BA3300_application::postprocess_page(int, int counter)
{
  if (counter == _num_pag - 1)
  {  
    TString16 dep; dep.format("%04d%-3s", _cod_anno, (const char*)_cod_reg);
    _tab->curr().put("CODTAB", (const char*)dep);
    _tab->read();
    if (_tab->bad()) _tab->curr().zero();
    _agg = _tab->curr().get_long("I2");
    _agg = _agg + _num_pag;
    _tab->curr().put("I2", _agg);
    _tab->rewrite();
    return NEXT_PAGE;
  }    

  return REPEAT_PAGE;
}

void BA3300_application::set_page(int , int counter)
{
  reset_print();

  const TString80 descr(decodifica_reg());
  long contatore = _pag_ini + counter;

  if (counter == 0)
  {
    if (!_inizia_dopo)
    {
      reset_row(8);
      set_row(8, "@65gPag.@70g%ld", contatore);
    }

    reset_row(1);
    reset_row(2);
    reset_row(3);
    reset_row(4);
    reset_row(5);
    reset_row(6);
    reset_row(7);

    set_row (1, "@50g%-50s", (const char*) descr);
    set_row (2, "@50g%-50s", (const char*)_ragsoc);
    set_row (3, "@50g%-35s", (const char*)_indulc);
    set_row (3, "@90g%-10s", (const char*)_civulc);
    set_row (4, "@50g%5s",   (const char*)_capulc);
    set_row (4, "@60g%-50s", (const char*)_com);
    set_row (4, "@115g%5s",  (const char*)_prov);
    set_row (5, "@50g%16s",  (const char*)_cofi);
    set_row (6, "@50g%11s",  (const char*)_paiv);
    
  }
  else if (counter==_num_pag-1)       
  {
    reset_row(1);
    reset_row(2);
    reset_row(3);
    reset_row(4);
    reset_row(5);
    reset_row(6);
    reset_row(7);
    reset_row(8);
    set_row (1, "@1gSocieta'@10g%-50s", (const char*) _ragsoc);
    set_row (1, "@62g%-29s", (const char*) _indulc);
    set_row (1, "@93g%-9s", (const char*) _civulc);
    set_row (1, "@103g%-5s", (const char*) _capulc);
    set_row (1, "@110g%-18s", (const char*) _com);
    set_row (1, "@130g%-3s", (const char*) _prov);
    set_row (2, "@1g%-50s", (const char*) descr);
    set_row (2, "@52g%16s", (const char*) _cofi);
    set_row (2, "@70g%11s", (const char*) _paiv);         
    set_row (4, "@50gN. ........ DI REPERTORIO");
    set_row (5, "@50gIL PRESENTE LIBRO DELLA@75g%50s", (const char*) _ragsoc);
    set_row (6, "@50g%-35s", (const char*) _indulc);
    set_row (6, "@88g%10s", (const char*) _civulc);
    set_row (7, "@50g%5s", (const char*) _capulc);
    set_row (7, "@58g%50s", (const char*) _com);
    set_row (7, "@111g%5s", (const char*) _prov);
    set_row (8, "@120gPag.@125g%ld", contatore);
  }
  else
  { 
    reset_row(1);
    reset_row(2);
    reset_row(3);
    reset_row(4);
    reset_row(5);
    reset_row(6);
    reset_row(7);
    reset_row(8);
    set_row (1, "@1gSocieta'@10g%-50s", (const char*) _ragsoc);
    set_row (1, "@62g%-29s", (const char*) _indulc);
    // set_row (1, "@93g%-9s", (const char*) _civulc);
    // set_row (1, "@103g%-5s", (const char*) _capulc);
    set_row (1, "@110g%-18s", (const char*) _com);
    set_row (1, "@130g%-3s", (const char*) _prov);
    set_row (2, "@1g%-50s", (const char*) descr);
    set_row (2, "@52g%16s", (const char*) _cofi);
    set_row (2, "@70g%11s", (const char*) _paiv);
    set_row (3, "@85gPag.@90g%ld", contatore);         
  }
}

BA3300_application::BA3300_application()  
{
}

bool BA3300_application::set_print(int)
{
  KEY   tasto;
  TString16 app;

  _nditte->setkey(1);
  _codice_ditta = get_firm();
  _nditte->curr().zero();
  _nditte->curr().put(N_CODDITTA,_codice_ditta);
  _nditte->read();
  app=_nditte->curr().get(N_CODDITTA);
  _msk->set(F_CODDITTA,app);
  _ragsoc=_nditte->curr().get(N_RAGSOC);
  _tipoa =_nditte->curr().get(N_TIPOA);
  _codanagr =atol(_nditte->curr().get(N_CODANAGR));
  _msk->set(F_RAGSOC,_ragsoc);
  
  _unloc->setkey(1);
  _unloc->curr().zero();
  _unloc->curr().put(N_CODDITTA,app);
  _unloc->curr().put(N_CODULC,"1");
  _unloc->read();
  _indulc=_unloc->curr().get(N_INDULC);
  _msk->set(F_INDIRIZZO,_indulc);
  _civulc=_unloc->curr().get(N_CIVULC);
  _msk->set(F_CIV,_civulc);
  _capulc=_unloc->curr().get(N_CAPULC);
  _msk->set(F_CAP,_capulc);
  _comulc=_unloc->curr().get(N_COMULC);
  _msk->set(F_CODCOM,_comulc);
  
  _comuni->setkey(1);
  _comuni->curr().zero();
  _comuni->curr().put(N_CODCOM,_comulc);
  _comuni->read();
  _com=_comuni->curr().get(N_COM);
  _msk->set(F_COM,_com);
  _prov=_comuni->curr().get(N_PROV);
  _msk->set(F_PROV,_prov);
  
  _anag->setkey(1);  
  _anag->curr().zero();
  _anag->curr().put(N_TIPOA,_tipoa);
  _anag->curr().put(N_CODANAGR,_codanagr);
  _anag->read();
  _cofi=_anag->curr().get(N_COFI);
  _msk->set(F_COFI,_cofi);
  _paiv=_anag->curr().get(N_PAIV);
  _msk->set(F_PAIV,_paiv);

  _msk->set(F_LUNG,66);
  
  tasto = _msk->run();

  if (tasto == K_ENTER) 
  {
    _cod_reg = _msk->get(F_CODICE);
    _cod_anno = _msk->get_int(F_ANNO);
    _pag_ini = _msk->get_long(F_NPAGINI);
    _pag_fin = _msk->get_long(F_NPAGFI);
    _inizia_dopo = _msk->get_bool(F_PRIMO);   
    _num_pag = (_pag_fin - _pag_ini) + 1; 
    enable_print_menu();
    return TRUE;
  }

  return FALSE;
}

static bool codice_handler (TMask_field& field, KEY key)
{ 
  if (key == K_TAB)  
  { 
    const TEdit_field& e = (const TEdit_field&)field;
    const TLocalisamfile& t = e.browse()->cursor()->file();
    const long num = t.get_long("I2");
    field.mask().set(F_NPAGINI, num+1);
    field.mask().set(F_NPAGFI, 0L);
  }
  return TRUE;
}

const char* BA3300_application::decodifica_reg ()
{
  TString16 dep; dep.format("%04d%-3s", _cod_anno, (const char*)_cod_reg);

  _tab->curr().put("CODTAB", (const char*)dep);
  _tab->read();
  if (_tab->bad()) _tab->curr().zero();
  return _tab->curr().get ("S0");
}

bool BA3300_application::user_create()
{
  enable_print_menu();

  _nditte = new TLocalisamfile (LF_NDITTE);
  _unloc = new TLocalisamfile (LF_UNLOC);
  _comuni = new TLocalisamfile (LF_COMUNI);
  _anag = new TLocalisamfile (LF_ANAG);
  _tab = new TTable(TAB_REG);

  _msk = new TMask ("ba3300a") ;
  _msk->set_handler(F_CODICE, codice_handler);
  
  add_cursor (NULL);
  set_auto_ff (TRUE);
  return TRUE;
}

bool BA3300_application::user_destroy() // releasev e arrmask
{
  delete  _msk;
  delete  _nditte;
  delete  _unloc;
  delete  _comuni;
  delete  _anag;
  delete  _tab;
  return TRUE;
}

int ba3300(int argc, char* argv[])
{
  BA3300_application a;
  a.run(argc, argv, "Stampa fogli libro bollato");
  return 0;
}
