#include <applicat.h>
#include <automask.h>
#include "../ve/velib.h"

#include "ci0.h"
#include "ci0300.h"

///////////////////////////////////////////////////////////
// TConfigurazioneIndustriale_mask
///////////////////////////////////////////////////////////

void TConfigurazioneIndustriale_mask::moveup_element(long selrow)
{
  if (items()<=1)
    return;

  if (selrow<=0) 
    return;

  if (selrow>items()-1) 
    return;

  rows_array().swap(selrow,selrow - 1);
  TSheet_field& s = sheet();
	s.check_row(selrow - 1);
	s.check_row(selrow);
  s.force_update();
  s.select(selrow-1);

}

void TConfigurazioneIndustriale_mask::movedown_element(long selrow)
{
  if (items()<=1)
    return;

  if (selrow<0) 
    return;

  if (selrow>=items()-1) 
    return;

  rows_array().swap(selrow,selrow+1);
  TSheet_field& s = sheet();
	s.check_row(selrow);
	s.check_row(selrow + 1);
  s.force_update();
  s.select(selrow+1);

}

bool TConfigurazioneIndustriale_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
	switch (o.dlg())
	{
	case F_MOVEUP:
    moveup_element(sheet().selected());
		break;
	case F_MOVEDN:
    movedown_element(sheet().selected());
		break;
	case FR_TIPODOC:
		if (e == fe_modify)
		{
			const TCodice_numerazione cod_num(o.mask().get(FR_NUM));
			const TString& tipo = o.get();
			const int last = cod_num.ntipi_doc();
			for (int i = 0; i < last; i++ )
			{
				const TString& curtipo = cod_num.tipo_doc(i);
				if (curtipo == tipo)
					return true;
			}
			return o.error_box(TR("Tipo non valido per la numerazione selezionata!"));
		}

	case FR_COLL:
		if (e == fe_modify && !o.mask().is_running())
		{
			TMask & row_mask = o.mask();

			row_mask.set(FR_NUM, "");
			row_mask.set(FR_TIPODOC, "");
			row_mask.set(FR_STATO, "");
			row_mask.set(FR_VAR, "");
			row_mask.set(FR_MODCOLL, "");
			const int row = sheet().selected();
      TSheet_field& s = sheet();
			s.update_row(row);
			s.check_row(row);
			s.force_update(row);
		}
	default:
		break;
	}     

  return true;
}  

///////////////////////////////////////////////////////////
// TConfigurazioneIndustriale_app
///////////////////////////////////////////////////////////

// Overrides

bool TConfigurazioneIndustriale_app::create()
{
  _mask = new TConfigurazioneIndustriale_mask();

  file_to_sheet();

  return TSkeleton_application::create();
}

bool TConfigurazioneIndustriale_app::destroy()
{
  delete _mask;
  return TSkeleton_application::destroy();
}
                                      
void TConfigurazioneIndustriale_app::main_loop()
{  
  KEY exitval;

  while ((exitval=_mask->run()) != K_QUIT)
  {
    switch(exitval)
    {
    case K_ESC:
      return;
    default:
      break;
    }
  }

  sheet_to_file();
}

// Conversione Array <-> File

void TConfigurazioneIndustriale_app::file_to_sheet()
{
  TConfig configfile(CONFIG_DITTA, "ci");

  TConfigurazioneIndustriale_mask& m = mask();
  TSheet_field& s = m.sheet();
	
	for (int item = 0; configfile.exist("Descr", item); item++)
  {
    TToken_string & t = s.row(item);
    t.add(configfile.get("Filter", NULL,item, "F01"));
    t.add(configfile.get("Descr", NULL,item, "Fatture"));
		t.add(configfile.get("InsertRemain", NULL,item, ""));
		t.add(configfile.get("ModifyRemain", NULL,item, ""));
    t.add(configfile.get("DocColl", NULL, item, ""));
    t.add(configfile.get("NumColl", NULL, item, ""));
    t.add(configfile.get("TipoColl", NULL, item, ""));
    t.add(configfile.get("StatoColl", NULL, item, ""));
    t.add(configfile.get("ModColl", NULL, item, ""));
    t.add(configfile.get("Var", NULL, item, ""));
		t.add(configfile.get("DeleteClosed", NULL, item, ""));
		s.check_row(item);
  }

  FOR_EACH_MASK_FIELD(m, i, f)
  {
    const TFieldref* fr = f->field();
    if (fr != NULL)
    {
      const TString& v = configfile.get(fr->name());
      f->set(v);
    }
  }
  
  s.force_update();
}

void TConfigurazioneIndustriale_app::sheet_to_file()
{
  TConfigurazioneIndustriale_mask& m = mask();
  TConfig configfile(CONFIG_DITTA, "ci");
  int totlen = 0;
	int items = m.items();
  configfile.remove_all();

  if (items>_maxelem)
  {
    message_box("Il massimo numero di filtri gestibili � pari a %d, pertanto verranno considerate solamente le prime %d righe", _maxelem, _maxelem);
    items=16;
  }

  TString currdesc;
	for (int i=0;i<items;i++)
  {
    TToken_string& ri = m.row(i);
    currdesc = ri.get(1);
    configfile.set("Filter",       ri.get(0), NULL, true, i);
    configfile.set("Descr",        currdesc, NULL, true, i);
    configfile.set("InsertRemain", ri.get(2), NULL, true, i);
    configfile.set("ModifyRemain", ri.get(3), NULL, true, i);
    configfile.set("DocColl",      ri.get(4), NULL, true, i);
    configfile.set("NumColl",      ri.get(5), NULL, true, i);
    configfile.set("TipoColl",     ri.get(6), NULL, true, i);
    configfile.set("StatoColl",    ri.get(7), NULL, true, i);
    configfile.set("ModColl",      ri.get(8), NULL, true, i);
    configfile.set("Var",          ri.get(9), NULL, true, i);
		configfile.set("DeleteClosed", ri.get(10), NULL, true, i);
    totlen += currdesc.len();
  }

  FOR_EACH_MASK_FIELD(m, i, f)
  {
    const TFieldref* fr = f->field();
    if (fr != NULL)
    {
      const TString& v = f->get();
      configfile.set(fr->name(), v);
    }
  }

  if (totlen>_maxlen)
    warning_box(TR("E' probabile che la lunghezza delle descrizioni inserite ne impedisca la totale visualizzazione sullo schermo"));
  
}

///////////////////////////////////////////////////////////
// Main
///////////////////////////////////////////////////////////

int ci0300(int argc, char* argv[])
{
  TConfigurazioneIndustriale_app a ;
  a.run(argc, argv, TR("Contabilit� Industriale"));
  return 0;
}