#include <mask.h>
#include <printapp.h>
#include <tabutil.h>  
#include <progind.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;
  TString80 _descr; 
  TProgind *_p;
  long _codice_ditta, _agg;
  long _num_pag, _pag_ini, _pag_fin; 
  int _cod_anno;
  bool _inizia_dopo;
  TLocalisamfile*   _nditte;
  TLocalisamfile*   _anag;
  TLocalisamfile*   _unloc;
  TLocalisamfile*   _comuni;  

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

  const char* decodifica_reg ();

  virtual void set_page (int, int);
  virtual print_action postprocess_page (int, int);
  virtual bool preprocess_print(int, int);
  virtual print_action postprocess_print(int, int);
  
public:
  BA3300_application() : _p(NULL) {}
  ~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->good())
    {
      //      _agg = _tab->curr().get_long("I2");
      //      _agg = _agg + _num_pag;
      _tab->put("I2", _pag_fin);
      _tab->rewrite();
    }
    return NEXT_PAGE;
  }    

  return REPEAT_PAGE;
}

bool BA3300_application::preprocess_print(int, int)
{
  _p = new TProgind(_pag_fin - _pag_ini +1, "Preparazione stampa", FALSE, TRUE, 60);
  return TRUE;
}

print_action BA3300_application::postprocess_print(int, int)
{
  if (_p != NULL) 
  {
    delete _p;
    _p = NULL;
  }           
  return NEXT_PAGE;
}

void BA3300_application::set_page(int , int counter)
{
  reset_print();        
  _p->addstatus(1);

  long contatore = _pag_ini + counter;

  if (_inizia_dopo) contatore--;
  reset_row(1);
  reset_row(2);
  reset_row(3);
  reset_row(4);
  reset_row(5);
  reset_row(6);
  reset_row(7);
  reset_row(8);
  reset_row(9);
  if (counter == 0)
  {
    if (!_inizia_dopo)
      set_row(3, "@120gPag. %ld", contatore);
    int pos = printer().formlen() / 2 - 3;
    set_row (pos++, "@36g%-50s", (const char*) _descr);
    set_row (pos++, "@36g%-50s", (const char*)_ragsoc);
    TString s(_indulc);
    
    s << " " << _civulc ;
    set_row (pos++, "@36g%-52s", (const char*) s);
    s = _capulc; 
    s << " " << _com << " " << _prov;
    set_row (pos++, "@36g%-62s",   (const char*) s);
    set_row (pos++, "@36gCODICE FISCALE %-16s",  (const char*)_cofi);
    set_row (pos, "@36gPARTITA I.V.A. %-11s",  (const char*)_paiv);
    
  }
  else if (counter == _num_pag - 1)       
  {
    set_row (1, "@1g%-50s", (const char*) _ragsoc);
    TString s(_indulc);
    
    s << " " << _civulc << " " << _capulc << " " << _com << " " << _prov;
    set_row (1, "@62g%-65s", (const char*) s);
    set_row (2, "@1g%-50s", (const char*) _descr);
    set_row (2, "@52gCODICE FISCALE %-16s", (const char*) _cofi);
    set_row (2, "@85gPARTITA I.V.A. %-11s", (const char*) _paiv);         
    set_row (2, "@120gPag. %ld", contatore);         
    set_row (25, "@50gN. ........ DI REPERTORIO");
    set_row (27, "@50gIL PRESENTE LIBRO DELLA@75g%-50s", (const char*) _ragsoc);

    s = _indulc;
    s << " " << _civulc;
    set_row (29, "@50g%-52s", (const char*) s);
    s = _capulc; 
    s << " " << _com << " " << _prov;
    set_row (31, "@50g%-62s", (const char*) s);
    set_row (33, "@50gCONSTA DI %ld PAGINE UTILI, BOLLATE E NUMERATE", _num_pag - (_inizia_dopo ? 1 : 0));
    set_row (35, "@50gE VIENE OGGI VIDIMATO, il .........");
  }
  else
  { 
    set_row (1, "@1g%-50s", (const char*) _ragsoc);
    TString s(_indulc);
    
    s << " " << _civulc << " " << _capulc << " " << _com << " " << _prov;
    set_row (1, "@52g%-68s", (const char*) s);
    set_row (2, "@1g%-50s", (const char*) _descr);
    set_row (2, "@52gCODICE FISCALE %-16s", (const char*) _cofi);
    set_row (2, "@85gPARTITA I.V.A. %-11s", (const char*) _paiv);
    set_row (2, "@120gPag. %ld", contatore);  
  }
}

void BA3300_application::on_firm_change()

{
  TPrintapp::on_firm_change();

  _msk->reset();
  _nditte->setkey(1);
  _codice_ditta = get_firm();
  _nditte->curr().zero();
  _nditte->curr().put(N_CODDITTA,_codice_ditta);
  _nditte->read();                 
  if (_nditte->bad()) 
    return;
  
  _msk->set(F_CODDITTA, _codice_ditta);
  _ragsoc="SOCIETA' ";
  _ragsoc << _nditte->curr().get(N_RAGSOC);
  const TString16 tipoa =_nditte->curr().get(N_TIPOA);
  const long codanagr =atol(_nditte->curr().get(N_CODANAGR));
  _msk->set(F_RAGSOC,_ragsoc);
  
  _unloc->setkey(1);
  _unloc->curr().zero();
  _unloc->curr().put(N_CODDITTA, _codice_ditta);
  _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);
}       

bool BA3300_application::set_print(int)
{
  //on_firm_change();
  if (_msk->run() == K_ENTER) 
  {
    printer().formlen(_msk->get_int(F_LUNG));
    _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;
    _ragsoc = _msk->get(F_RAGSOC);
    _indulc= _msk->get(F_INDIRIZZO);
    _civulc= _msk->get(F_CIV);
    _capulc= _msk->get(F_CAP);
    _comulc= _msk->get(F_CODCOM);
    _com= _msk->get(F_COM);
    _prov= _msk->get(F_PROV);
    _paiv = _msk->get(F_PAIV);
    _cofi = _msk->get(F_COFI);
    _descr = decodifica_reg();
    if (_inizia_dopo) _num_pag++; 
    enable_print_menu();      
    return TRUE;
  }

  return FALSE;
}

static bool codice_handler (TMask_field& field, KEY key)
{ 
  if (key == K_TAB && field.focusdirty())  
  { 
    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, 0L);
    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;   
  if (_p != NULL) delete _p;
  return TRUE;
}

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