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

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

  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;}
  virtual int read(TMask& m);
  virtual void init_query_mode(TMask& m) ;
  virtual void init_query_insert_mode(TMask& m) ;
  static bool gest_res(TMask_field& f, KEY key);


public:
  TAttivita_application() {}
  virtual ~TAttivita_application() {}
};
HIDDEN inline TAttivita_application & app() { return (TAttivita_application &) main_app();}


bool TAttivita_application::gest_res(TMask_field& f, KEY key)
{

  if (!f.to_check(key)) return TRUE;
  
  TMask &        msk = f.mask();
  TLocalisamfile & anag = *app()._anag;
  TLocalisamfile & comuni = *app()._comuni;

  anag.setkey(1);
  anag.zero();
  anag.put(N_TIPOA, msk.get(LST_AT2_TIPOADS));
  anag.put(N_CODANAGR, msk.get(FLD_AT2_CODANGRDS));
  if (anag.read() != NOERR)
    return FALSE;

  TArray comarr(2), f1(2), f2(2), f3(2);
  TString cod_com = anag.get(N_CODCOMRF);
  
  comarr.add(cod_com);
  cod_com = anag.get(N_CODCOMRES);
  comarr.add(cod_com);
  cod_com = N_INDRF;
  f1.add(cod_com);
  cod_com = N_CIVRF;
  f2.add(cod_com);
  cod_com = N_CAPRF;
  f3.add(cod_com);
  cod_com = N_INDRES;
  f1.add(cod_com);
  cod_com = N_CIVRES;
  f2.add(cod_com);
  cod_com = N_CAPRES;
  f3.add(cod_com);

  for (int i = 0; i < comarr.items(); i++)
  {
    cod_com = (TString&) comarr[i];
    if ( cod_com.not_empty() )
    {
      comuni.zero() ;
      comuni.put(N_COM, cod_com );
      comuni.setkey(1) ;
      if (comuni.read() == NOERR)
      {
        msk.set( FLD_AT2_ANAGR_DEP_IND ,  (const char*) anag.get((TString&) f1[i]) ) ; 
        msk.set( FLD_AT2_ANAGR_DEP_CIV ,  (const char*) anag.get((TString&) f2[i]) ) ; 
        msk.set( FLD_AT2_ANAGR_DEP_CAP ,  (const char*) anag.get((TString&) f3[i]) ) ; 
        msk.set( FLD_AT2_DEP_ANAGR_COMUNI_DESCR ,  (const char*) comuni.get(N_DENCOM) ) ; 
        msk.set( FLD_AT2_DEP_ANAGR_COMUNI_PROV ,  (const char*) comuni.get(N_PROV) ) ; 
        break;
      }
    }
  }     
  return TRUE;
}

HIDDEN bool clear_cap(TMask_field& f, KEY key)
{                     
  if (f.mask().query_mode() || !f.to_check(key, TRUE)) 
    return TRUE;

  TMask& msk = f.mask() ;
  const TString16 val(f.get());

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


bool TAttivita_application::user_create()
{
  _msk = new TMask("ba4500a") ;
  _msk->set_handler(FLD_AT2_CODANGRDS, gest_res);
  _msk->set_handler(FLD_AT2_COMCS, clear_cap);
  _rel = new TRelation(LF_ATTIV) ;
  _anag = new TLocalisamfile(LF_ANAG) ;
  _ditte = new TLocalisamfile(LF_NDITTE) ;
  _comuni = new TLocalisamfile(LF_COMUNI) ;
  set_search_field(FLD_AT1_CODDITTAH);
  return TRUE;
}

void TAttivita_application::init_query_mode(TMask& m)

{
  m.show(FLD_AT1_CODATTH);
  m.hide(FLD_AT1_CODATT);
}


void TAttivita_application::init_query_insert_mode(TMask& m)

{
  m.show(FLD_AT1_CODATT);
  m.hide(FLD_AT1_CODATTH);
}


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

int TAttivita_application::read(TMask& m) 
{
  const int err = TRelation_application::read(m);
  if (err == NOERR)
  {
    m.field(FLD_AT2_CODANGRDS).set_focusdirty();
    gest_res(m.field(FLD_AT2_CODANGRDS), K_TAB);
  }
  return err;
}

int ba4500(int argc, char* argv[])
{
  TAttivita_application a;
  a.run(argc, argv, "Anagrafica Attivita'");
  return 0;
}


