#include <execp.h>
#include <tabapp.h>
#include <tabutil.h>

// @cmember Costruttore
TTable_application::TTable_application() 
               : _msk(NULL), _rel(NULL) 
{ }

// @cmember Distruttore
TTable_application::~TTable_application() 
{ }

// @cmember Indica se la futura <mf TTable_application::get_mask> ritornera' una maschera diversa
//        dalla corrente.
bool TTable_application::changing_mask(int mode) 
{ return FALSE; }

// @cmember Richiede la maschera da usare
TMask* TTable_application::get_mask(int mode) 
{ 
  CHECK(_msk, "Null mask");
  return _msk; 
}

// @cmember Ritorna la relazione da modificare
TRelation* TTable_application::get_relation() const 
{ 
  CHECK(_rel, "Null relation");
  return _rel; 
}

void TTable_application::print()
{
  TString16 cmd;
  cmd << "ba3 -1 " << get_tabname();
  TExternal_app stampa(cmd);
  stampa.run();
}

void TTable_application::init_query_mode(TMask& m) 
{
  m.enable(-GR_MODIFY_PROTECTED);
  m.enable(-GR_RECORD_PROTECTED);
}

void TTable_application::init_modify_mode(TMask& m) 
{
  m.disable(-GR_MODIFY_PROTECTED);
  const bool enable = !(_rel->curr().get_bool(FPC));
  m.enable(-GR_RECORD_PROTECTED,enable);
}

bool TTable_application::protected_record(TRectype& rec) 
{ 
  return rec.get_bool(FPC);
}

TString& TTable_application::get_mask_name(TString& t) const
{
  CHECK(_rel,"Can't use a NULL relation to retrieve table module");
  TTable& tab = (TTable&) _rel->lfile();

  TString16 m = _tabname;
  if (m[0] == '%') m.ltrim(1);
  
  t = tab.module();
  t << "tb" << m;
  t.lower(); 
  
  TFilename n(t); n.ext("msk");
  if (!n.custom_path())
    t.overwrite("ba");
  return t;
}

TMask* TTable_application::set_mask(TMask* m)
{
  if (_msk != NULL)  
  {
    delete _msk;
    _msk = NULL;
  }

  if (m != NULL) 
  {
    // imposta la maschera come maschera della applicazione
    _msk = m;
  } 
  else 
  {
    // alloca la maschera standard come maschera della applicazione
    TFilename name; get_mask_name(name); name.ext("msk");
    if (name.custom_path())
      _msk = new TMask(name);
    else
      error_box(FR("Impossibile trovare la maschera %s"), (const char*)name);
  }
  return _msk;
}

bool TTable_application::user_create()
{
  if (argc() < 3) 
    return false;
  
  _tabname = argv(2);
  _tabname.upper();
  _rel = new TRelation(_tabname);
  
  if (set_mask() == NULL)
    return false;
  
  const int campi = _msk->fields();
  for (int i = 0; i < campi; i++)
  {
    const TMask_field& f = _msk->fld(i);
    if (f.in_group(GR_SEARCH))
    {
      set_search_field(f.dlg());
      break;
    }
  }
  
  TFilename rpt; get_mask_name(rpt); rpt.ext("rpt");
  if (rpt.custom_path()) 
    enable_menu_item(M_FILE_PRINT);
  
  TString title;
  _msk->get_caption(title);
  set_title(title);
  return TRUE;
}

bool TTable_application::user_destroy() 
{
  if (_msk) delete _msk;
  if (_rel) delete _rel;

  return TRUE;
}

///////////////////////////////////////////////////
//	Tabelle Multirel
///////////////////////////////////////////////////

// @cmember Indica se la futura <mf TMultirel_application::get_mask> ritornera' una maschera diversa
//        dalla corrente.
bool TMultirel_application::changing_mask(int mode) 
{ return false; }

// @cmember Richiede la maschera da usare
TMask* TMultirel_application::get_mask(int mode) 
{ 
  CHECK(_msk, "Null mask");
  return _msk; 
}

// @cmember Ritorna la relazione da modificare
TRelation* TMultirel_application::get_relation() const 
{ 
  CHECK(_rel, "Null relation");
  return _rel; 
}

void TMultirel_application::print()
{
	TString16 tabname;
	get_mask_name(tabname);
	TString cmd;
  cmd << "ba8 -4 "<< tabname;
  TExternal_app stampa(cmd);
  stampa.run();
}

void TMultirel_application::get_mask_name(TString& tabname) const
{
	tabname = name().left(4);
	tabname << _tabname;
}

bool TMultirel_application::find(word k)
{                   
	bool ok = TRelation_application::find(k);

	if (!ok && k < 2)
  {
	  file().setkey(1);
		file().zero();
		for (TEditable_field* e = get_mask()->get_key_field(1, true); e; e = get_mask()->get_key_field(1, false))
		{
			if (e->shown() || e->ghost())                        // Ignora campi invisibili non ghost
				e->autosave(*get_relation());
		}

		TRectype rec(file().curr());
		ok = file().read(_isequal) == NOERR;
	}

  return ok;
}

void TMultirel_application::init_insert_mode(TMask& m) 
{
	TLocalisamfile multi(LF_MULTIREL);

	file().curr().zero();
	for (TEditable_field* e = get_mask()->get_key_field(1, true); e; e = get_mask()->get_key_field(1, false))
	{
		if ((!e->in_group(1)) && (e->shown() || e->ghost())) 
			e->autosave(*get_relation());
	}

	multi.curr() = file().curr();
	if (multi.read(_isgteq) == NOERR && multi.curr() == file().curr())
		set_descr(m, multi.get("DATA"));
  for (int i = m.fields() - 1; i >= 0; i--)
  {
    TMask_field& c = m.fld(i);

    if (c.in_group(1))
		{
			c.enable();
			if (c.is_edit())
			{
				TEdit_field& e = (TEdit_field&)c;

				if (e.browse() != NULL)
					e.enable_check();
			}
		}
  }
}

void TMultirel_application::init_modify_mode(TMask& m) 
{
  for (int i = m.fields() - 1; i >= 0; i--)
  {
    TMask_field& c = m.fld(i);

    if (c.in_group(1))
		{
			c.enable();
			_second =	c.get();
			if (c.is_edit())
			{
				TEdit_field& e = (TEdit_field&)c;

				if (e.browse() != NULL)
					e.enable_check();
			}
		}
  }
}

int TMultirel_application::rewrite(const TMask& m)
{
	int err = TRelation_application::rewrite(m);

	if (err != NOERR)
		err = TRelation_application::write(m);

	TRelation & rel = *get_relation();
	const TString & second = rel.lfile().get("SECOND");

	if (err == NOERR && second != _second)
	{

		rel.save_status();
		rel.lfile().put("SECOND", _second);
		rel.remove();
		rel.restore_status();
	}
	return err;
}

bool TMultirel_application::user_create()
{
  if (argc() < 3) 
    return false;
  
  _tabname = argv(2);
  _tabname.upper();
  _rel = new TRelation(LF_MULTIREL);
  
	//costruisce il nome della maschera interessata
	//nome modulo + 'ts' + nome tabella
	TString16 tabname;
	get_mask_name(tabname);
  _msk = new TMask(tabname);
  
	FOR_EACH_MASK_FIELD((*_msk), i, f)
  {
    if (f->in_key(1) && !f->in_group(1))
    {
      set_search_field(f->dlg());
      break;
    }
  }
  
  TFilename rep = tabname; 
  rep.ext("rep");
  if (rep.custom_path()) 
    enable_menu_item(M_FILE_PRINT);
  
  TString title;
  _msk->get_caption(title);
  set_title(title);
  return true;
}

bool TMultirel_application::user_destroy() 
{
  if (_msk) delete _msk;
  if (_rel) delete _rel;

  return true;
}

// @cmember Costruttore
TMultirel_application::TMultirel_application() 
               : _msk(NULL), _rel(NULL) 
{ }

// @cmember Distruttore
TMultirel_application::~TMultirel_application() 
{ }