#include <relapp.h>
#include <stdtypes.h>
#include <tabutil.h>
#include <execp.h>
#include <utility.h>

#include "ba3.h"
#include "ba3100.h"
#include "batbreg.h"

#define FPC "FPC"
#define REG_JOURNAL 5

class BA3100_application : public TRelation_application
{
  TMask* _msk;
  TRelation* _rel;
  TString _tabname;
  long _oldditta;
  int  _oldanno;
  bool _exist_journal;

  bool user_create() ;                         
  bool user_destroy() ;                        
  virtual bool protected_record(TRectype& rec);
  virtual TMask* get_mask( int mode) { return _msk;}
  virtual bool changing_mask( int mode) { return FALSE;}
  virtual TRelation* get_relation() const { return _rel;}
  virtual void  init_query_mode(TMask& m);
  virtual void  init_insert_mode(TMask& m);
  virtual void  init_modify_mode(TMask& m);

public:
  BA3100_application() : _oldditta(-1), _oldanno(-1), _exist_journal(FALSE) , _msk(NULL), _rel(NULL) {}
  virtual ~BA3100_application() {}
  bool exist_journal() {return _exist_journal;}
  void print();
};

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

void BA3100_application::print()
{
#if XVT_OS == XVT_OS_WIN
  TExternal_app stampa(format("ba3a -1 %s", (const char *) _tabname));
#else
  TExternal_app stampa(format("ba3 -1 %s", (const char *) _tabname));
#endif
  stampa.run();
}

void BA3100_application::init_query_mode(TMask& m) 

{
  m.send_key(K_SHIFT + K_CTRL + 'e', -GR_MODIFY_PROTECTED);
  m.send_key(K_SHIFT + K_CTRL + 'e', -GR_RECORD_PROTECTED);
}

void BA3100_application::init_insert_mode(TMask& m) 

{
  if (_tabname != "REG") return;

  long ditta = get_firm();
  int anno = atoi(m.get(F_ANNO));

  if (ditta != _oldditta || anno != _oldanno)
  {
    _oldditta = ditta;
    _oldanno = anno;

    TTable reg(_tabname);
    
    reg.zero();
    reg.put("CODTAB", m.get(F_ANNO));

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

    for (reg.read(_isgteq); !_exist_journal && reg.good() && reg.curr() <= to; reg.next())
      _exist_journal = (reg.get_long("I0") == REG_JOURNAL);
  }
}

void BA3100_application::init_modify_mode(TMask& m) 

{
  m.send_key(K_SHIFT + K_CTRL + 'd', -GR_MODIFY_PROTECTED);
  const bool enable = !(_rel->curr().get_bool(FPC));

  m.send_key(K_SHIFT + K_CTRL + 'd' + enable, -GR_RECORD_PROTECTED);
}

bool BA3100_application::protected_record(TRectype& rec) 
{ 
  bool prot = rec.get_bool(FPC);
  if (!prot)
  {
    if (_tabname == "%IVD")      // Impedisce la cancellazione di una classe se ha sottoclassi
    {                                
      const TRecnotype pos = _rel->lfile().recno();
      const TString cod(rec.get("CODTAB"));
      const int err = _rel->lfile().next();
      if (err == NOERR)
      {
        TString next(_rel->lfile().get("CODTAB")); next.cut(cod.len());
        prot = cod == next;
      }  
      _rel->lfile().readat(pos);
    }
  }
  return prot;
}


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 error_box("Non e' possibile avere due registri giornale nello stesso anno");
  return TRUE;
}


bool BA3100_application::user_create()
{
  if (argc() < 3) return FALSE;
  _tabname = argv(2);

  if (_tabname.empty()) return FALSE;
  _tabname.upper();
  TString16 m, t(_tabname);

  if (t[0] == '%')
    t.ltrim(1);

  m << "BATB" << t;
  _msk = new TMask(m) ;
  for (int i = 0; i < _msk->fields(); i++)
    if (_msk->fld(i).in_group(GR_SEARCH))
    {
      set_search_field(_msk->fld(i).dlg());
      break;
    }
  _rel = new TRelation(_tabname);
  set_title(_msk->get_caption());
  if (_tabname == "REG") _msk->set_handler(F_TIPO, tiporeg_handler);
  return TRUE;
}

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

int ba3100(int argc, char* argv[])
{
  BA3100_application a ;
  a.run(argc, argv, "Tabella");
  return 0;
}
