#include <config.h>
#include <printer.h>
#include <relapp.h>
#include <tabutil.h>

#include "batbreg.h" 
#include "tabreg.h" 
#include "ba5200a.h"

#define REG_JOURNAL 5                

class TRegister_app : public TRelation_application
{
  TMask     *_msk;
  TRelation *_rel; 
  long       _oldditta;
  int        _oldanno;
  bool       _exist_journal;
  bool       _stampa_intest;

protected:  // TRelation_application
  virtual TMask* get_mask(int) { return _msk;}
  virtual TRelation* get_relation() const { return _rel;}
  virtual bool changing_mask(int) { return FALSE; }
  virtual bool user_create() ;
  virtual bool user_destroy();
  virtual void init_insert_mode(TMask& m) ;
  virtual void init_modify_mode(TMask& m);
  virtual int rewrite(const TMask& m);

  static bool codsp_handler(TMask_field& f, KEY k);

public:
  bool exist_journal() {return _exist_journal;}
  
  TRegister_app() : _exist_journal(FALSE), _stampa_intest(FALSE) {}
  virtual ~TRegister_app() {}
};

HIDDEN inline TRegister_app& app() { return (TRegister_app&) main_app();}

void TRegister_app::init_insert_mode(TMask& m) 
{
  const long ditta = get_firm();
  const int anno = atoi(m.get(F_ANNO));

  m.set(F_STAMPA_INTESTAZIONE, _stampa_intest ? "X" : "");

  if (ditta != _oldditta || anno != _oldanno)
  {
    _oldditta = ditta;
    _oldanno = anno;
    
    TLocalisamfile tabreg(LF_TABREG);
    
    tabreg.zero();
    tabreg.put(DITTA, ditta);
    tabreg.put(ANNO, anno);

    TRectype to(tabreg.curr());
    _exist_journal = FALSE;

    for (tabreg.read(_isgteq); !_exist_journal && tabreg.good() && tabreg.curr() <= to; tabreg.next())
      _exist_journal = (tabreg.get_long(TIPO) == REG_JOURNAL);
  }  
}

void TRegister_app::init_modify_mode(TMask& m) 
{             
  TRelation_application::init_modify_mode(m);
  TString16 config;
  config.format("REG%05ld", m.get_long(F_CODDITTA));
  config << m.get(F_CODICE);
  TConfig ini(CONFIG_STAMPE, config);              
  const int what = ini.get_int("Type", NULL, -1, -1);
  m.set(F_CONFIG, what >= 0 ? "X" : "");
  m.enable(F_CONFIG, what >= 0);
}  

HIDDEN bool tiporeg_handler(TMask_field& f, KEY k)
{
  if ((k == K_TAB || k == K_ENTER) && app().exist_journal() &&
      (atoi(f.get()) == REG_JOURNAL)
      )
    return f.error_box("Non e' possibile avere due registri giornale nello stesso anno");
  return TRUE;
}

HIDDEN bool printer_handler(TMask_field& f, KEY k)
{        
  if (k == K_SPACE)
  {                  
    TMask& m = f.mask();
    if (!m.query_mode())
    {
      TString16 config;
      config.format("REG%05ld", m.get_long(F_CODDITTA));
      config << m.get(F_CODICE);
  
      if (config.len() > 8)                   // REG+ANNO+CODICE di tre caratteri
      {
        TPrinter& p = printer();
        p.set_printtype(normprinter);           // Force configuration update
        p.read_configuration(config);
        if (p.set())
        {
          m.enable(F_CONFIG);
          m.set(F_CONFIG, "X");
        }  
        p.read_configuration();
      }   
      else 
        return f.error_box("Nessun registro selezionato"); 
    }    
  }                                    
  return TRUE;  
}

bool TRegister_app::user_create()
{
  _msk = new TMask("ba5200a");
  _rel = new TRelation(LF_TABREG);
  
  _msk->set_handler(F_TIPO, tiporeg_handler);
  _msk->set_handler(F_PRINTER, printer_handler);
  _msk->set_handler(F_CODSPEC, codsp_handler);
    
  TConfig st(CONFIG_STUDIO, "cg");
  _stampa_intest = st.get_bool("StiReg");
  return TRUE;
}

bool TRegister_app::user_destroy()
{
  delete _msk ;
  delete _rel ;
  return TRUE ;
}             

int TRegister_app::rewrite(const TMask& m)
{
  if (!m.get_bool(F_CONFIG))
  {
    TString16 config;
    config.format("REG%05ld", m.get_long(F_CODDITTA));
    config << m.get(F_CODICE);
    TConfig ini(CONFIG_STAMPE, config);
    const int what = ini.get_int("Type", NULL, -1, -1);
    if (what >= 0)
      ini.set("Type", -1);
  }
  
  return TRelation_application::rewrite(m);
}

bool TRegister_app::codsp_handler(TMask_field& f, KEY k)
{                       
  TMask & m = f.mask();
  if (f.to_check(k) || !m.is_running())
  { 
    TString16 codsp(f.get());
    const int len = codsp.len();
    if (len == 1 || len == 3)
    {   
      codsp.insert(" ");    
      f.set(codsp);
    }
/*    TDate d(31, 12, m.get_int(F_ANNO));
    
    m.set(F_DTF, d.string());      
    CheckType c = f.check_type();
    f.check_type(CHECK_NORMAL);
    const bool ok = f.check();
    f.check_type(c);
    return ok;      
*/
  }          
  return TRUE;
}

int ba5200(int argc, char* argv[])
{
  TRegister_app a ;
  a.run(argc, argv, "Gestione registri");
  return 0;
}
