#include <confapp.h>
#include <urldefid.h>
#include <utility.h>
#include <mask.h>
#include <relation.h>

#define comunque

bool TConfig_application::create()
{
  TApplication::create();

  _last_choice = BAR_ITEM(1);

  // process args
  TString arg(16);
  for (int i = 0; i < argc(); i++)
  {
    arg = argv(i);
    if (arg == "-c")
      _which_config = atoi(argv(++i));
    else
      if (arg[0] == '-')
        continue;
      else
      {
        TString* argp = new TString(arg);
        _paragraphs.add(argp);
      }
  }

  user_create();
  dispatch_e_menu(_last_choice);
  return TRUE;
}

bool TConfig_application::destroy()
{
  bool b = user_destroy();
  TApplication::destroy();
  return b;
}

bool TConfig_application::menu(MENU_TAG m)
{
  // funziona da se' fino a 20 voci della menubar
  if (m >= BAR_ITEM(1) && m <= BAR_ITEM(20))
  {
    _last_choice = m;
    do_config((m - BAR_ITEM(0))/100);
  }
  return xvt_test_menu_tag(BAR_ITEM(2));
}

void TConfig_application::do_config(int m)
{
  TString par(name()); 
  if (m < _paragraphs.items())
    par = (TString&)_paragraphs[m];
  else par.cut(2); 

  for (;;)
  {
    TConfig cnf(_which_config, par);
    
    const TFilename maskname(cnf.get("EdMask"));
    if (!maskname.empty())
    {
      TMask m(maskname);
      
      // carica campi
      for (int i = 0; i < m.fields(); i++)
      {
        TMask_field& f = m.fld(i);
        const TFieldref* fref = f.field();
        if (fref != NULL)
        {
          const char* fname = fref->name();
          if (fname != NULL)
          {
            TString& oldvl = cnf.get(fname);
            if (!oldvl.empty())
              f.set(oldvl);
          }
        }
      }
      
      // run mask
      if (!preprocess_config(m,cnf)) 
        break;

      int k = m.run();
      if (postprocess_config(m,cnf))
      {
        bool tosave = k == K_ENTER || k == K_SAVE;
        
        if (k == K_ENTER || k == K_QUIT)
        {
          // aggiusta campi
          for (i = 0; i < m.fields(); i++)
          {
            TMask_field& f = m.fld(i);
            if (f.dirty())
            {
              const TFieldref* fref = f.field();
              if (fref != NULL)
              {
                const char* fname = fref->name();
                const char* value = f.get();
                const char* oldvl = cnf.get(fname);
                
                if (!tosave)
                  tosave = yesno_box("Modifiche non registrate. Salvare?");
                
                if (!tosave) break;
                
                if (postprocess_config_changed(par,fname, oldvl,value))
                  cnf.set(fname, value, NULL, TRUE); 
              }
            }
          }
        }
        else break;  
      }  
      if (k == K_QUIT) 
        break comunque;
    }
    else 
    {
      warning_box("Nessun parametro da configurare");
      break;
    }
  }
}

bool TConfig_application::preprocess_config (TMask& mask, TConfig& config)
{ 
  return TRUE;
}

bool TConfig_application::postprocess_config (TMask& mask, TConfig& config)
{ 
  return TRUE;
}

bool TConfig_application::postprocess_config_changed(const char* par, 
                                                     const char* var, 
                                                     const char* oldv, 
                                                     const char* newv)
{
  return TRUE; 
}
