#include <relapp.h>
#include <stdtypes.h>
#include <lffiles.h>
#include "ba4.h"
#include "ba4400.h"

class BA4400_application : public TRelation_application
{
  TMask* _msk;
  TRelation* _rel;
  TLocalisamfile* _anag;
  TLocalisamfile* _ditte;

  bool user_create() ;                         
  bool user_destroy() ;                        
  virtual TMask* get_mask(int mode) { return _msk;}
  virtual bool changing_mask(int mode) { return FALSE;}
  virtual TRelation* get_relation() const { return _rel;}

public:
  BA4400_application() {}
  virtual ~BA4400_application() {}
};

HIDDEN bool clear_cap(TMask_field& f, KEY key)
{                     
  if (f.mask().query_mode())
    return TRUE;
  TMask& msk = f.mask() ;
  const short   dlg = f.dlg();

  if (dlg != FLD_UL1_COMULC && !f.to_check(key, TRUE)) 
    return TRUE;

  const TString16 val(f.get());
  const int capid = dlg == FLD_UL1_COMULC ? FLD_UL1_CAPULC : FLD_UL2_CAPTRIB; 

  if (val.empty())
    msk.set(capid, "");
  return TRUE; 
} 


bool BA4400_application::user_create() 
{
  _msk = new TMask("ba4400a") ;
  _msk->set_handler(FLD_UL1_COMULC, clear_cap);
  _msk->set_handler(FLD_UL2_COMTRIB, clear_cap);
  _rel = new TRelation(LF_UNLOC) ;
  _anag = new TLocalisamfile(LF_ANAG) ;
  _ditte = new TLocalisamfile(LF_NDITTE) ;
  set_search_field(FLD_UL1_CODULC);
  return TRUE;
}

bool BA4400_application::user_destroy()
{
  delete  _msk;
  delete _rel;
  delete _anag;
  delete _ditte;
  return TRUE;
}

int ba4400(int argc, char* argv[])
{
  
  BA4400_application a;
  a.run(argc, argv, "Anagrafica Unita' Locali");
  return 0;
}
