#include <colors.h>
#include <mask.h>

#include "sc0100.h"
#include "sc0100a.h"

HIDDEN bool gruppo_handler(TMask_field& f, KEY key)
{                          
  static bool ignore = FALSE;
  if (key == K_TAB && f.focusdirty())
  {                                 
    if (!ignore)
    {
      TMask_field& c = f.mask().field(F_CONTO);
      if (c.get().not_empty())
      {
        ignore = TRUE;
        c.set_dirty();
        c.on_key(K_TAB);
        ignore = FALSE;
      }
    }  
  }
  return TRUE;
}

///////////////////////////////////////////////////////////
// Gestione saldaconto extra-contabile
///////////////////////////////////////////////////////////

TSaldaconto_app::TSaldaconto_app() 
               : _allow_firm(TRUE)
{} 

bool TSaldaconto_app::create()
{   
  open_files(LF_TAB, LF_TABCOM, LF_CLIFO, LF_PCON, 0);                      
  open_files(LF_PARTITE, LF_SCADENZE, LF_PAGSCA, 0);  
  open_files(LF_CAUSALI, LF_ATTIV, 0);                      
  
  load_colors();

  _msk = new TMask("sc0100a");
  _msk->set_handler(F_GRUPPO, gruppo_handler);
  
  TConfig cnf(CONFIG_DITTA, "cg");
  const bool ges_sal = cnf.get_bool("GesSal");
  if (!ges_sal)                                                  
  {
    warning_box(FR("Attenzione: La ditta %ld non ha la\n"
                "la gestione del saldaconto attivata!"), get_firm());
  }              

  return TSkeleton_application::create();
}

bool TSaldaconto_app::destroy()
{
  delete _msk;
  return TSkeleton_application::destroy();
}

void TSaldaconto_app::on_config_change()
{
  TConfig cnf(CONFIG_DITTA, "cg");
  _ges_val = cnf.get_bool("GesVal");
  TPartita::carica_allineamento();
}

void TSaldaconto_app::main_loop()
{          
  TMask& m = curr_mask();
  
  bool ok = TRUE;
  while (ok)
  {      
    xvt_statbar_set(TR("Ricerca"), TRUE);
    m.reset();
    ok = m.run() == K_ENTER;
    if (ok)
    {
      _allow_firm = FALSE;
      edit_partite(m);
      _allow_firm = TRUE;
    }  
  }  
}   

void TSaldaconto_app::load_colors()
{
  TConfig conf(CONFIG_USER, "cg2");
  TAssoc_array& colori = (TAssoc_array&)conf.list_variables();
  for (THash_object* o = colori.get_hashobj(); o; o = colori.get_hashobj())
  {
    const TString& key = o->key();
    if (key.len() == 7 && key.compare("Color", 5, TRUE) == 0)
    {
      const COLOR col = conf.get_color(key);
      TString* strcol = new TString16;
      strcol->format("%ld", col);
      _colori.add(key.mid(5), strcol);
    }  
  }
}              

COLOR TSaldaconto_app::type2color(char tipor, char tipoc)
{  
  COLOR col;                         
  if (tipor > ' ')
  {
    const char key[3] = { tipoc, tipor, '\0' }; 
    TString* colstr = (TString*)_colori.objptr(key);
    if (colstr == NULL)
    {
      colstr = new TString16;
      colstr->format("%ld", tipoc == 'B' ? NORMAL_BACK_COLOR : NORMAL_COLOR);
      _colori.add(key, colstr);
    }    
    col = atol(*colstr);
  }  
  else
  {
    col = tipoc == 'B' ? NORMAL_BACK_COLOR : NORMAL_COLOR;
  }
  return col;
}

void TSaldaconto_app::type2colors(char tipor, COLOR& back, COLOR& fore)
{      
  back = type2color(tipor, 'B');
  fore = type2color(tipor, 'F');
}

///////////////////////////////////////////////////////////
// Handlers generali
///////////////////////////////////////////////////////////

#include "sc0100p.h"

void TSaldaconto_app::gioca_cambi(TMask& m, int force)
{               
  if (m.get(E_VALUTA).empty())
    return;
  
  const real totale = m.get_real(E_TOTALE);
  const real totval = m.get_real(E_TOTDOCVAL);
  const real cambio = m.get_real(E_CAMBIO);  

  if ( (force == 0x1 || totale.is_zero()) && !(totval.is_zero() || cambio.is_zero()) )
  {
    const TValuta cam(m, E_VALUTA, E_DATACAMBIO, E_CAMBIO);
    const real new_totale = cam.val2lit(totval);
    if (new_totale != totale)
      m.set(E_TOTALE, new_totale, TRUE);
  } 
  
  if ( (force == 0x2 || totval.is_zero()) && !(totale.is_zero() || cambio.is_zero()) )
  {
    const TValuta cam(m, E_VALUTA, E_DATACAMBIO, E_CAMBIO);
    const real new_totval = cam.lit2val(totale);
    if (new_totval != totval)
      m.set(E_TOTDOCVAL, new_totval, TRUE);
  } 
  
  if ( (force == 0x4 || cambio.is_zero()) && !(totale.is_zero() || totval.is_zero()) )
  {
    real new_cambio = totale / totval; new_cambio.round(5);
    if (new_cambio != cambio)
      m.set(E_CAMBIO, new_cambio, TRUE);
  } 
}              

///////////////////////////////////////////////////////////

int sc0100(int argc, char* argv[])
{
  TSaldaconto_app* salda = new TSaldaconto_app;
  salda->run(argc, argv, TR("Gestione Saldaconto"));
  delete salda;
  return 0;
}