#include <relapp.h>
#include <stdtypes.h>
#include <modaut.h>
#include <prefix.h>
#include <utility.h>

#include "ba1.h"
#include "ba0100a.h"

class TSet_users : public TRelation_application
{
  TMask* _msk;
  TRelation* _rel;

  virtual bool user_create();                         
  virtual 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 int write(const TMask& m); 
  virtual int rewrite(const TMask& m);
  void  enable_aut(TMask& m);
  static bool user_handler(TMask_field& f, KEY key);
  static bool password_handler(TMask_field& f, KEY key);
  virtual void  init_query_mode(TMask& m) { enable_aut(m);}
  virtual void  init_insert_mode(TMask& m) { enable_aut(m);}
  virtual void  init_modify_mode(TMask& m) { enable_aut(m);}


public:
  TSet_users() : _msk(NULL), _rel(NULL) {}
};

bool TSet_users::user_handler(TMask_field& f, KEY key)

{
  if (!f.mask().query_mode() || !f.to_check(key)) return TRUE;
  
  const bool ok = f.get().find(' ') < 0;

  if (!ok) return f.error_box("Il nome dell'utente non deve contenere spazi");
  return ok;
}

bool TSet_users::password_handler(TMask_field& f, KEY key)

{
  if (f.mask().query_mode() || key != K_ENTER) return TRUE;
  const bool ok = f.get().len() > 3 && f.get().find(' ') < 0;

  if (!ok) return f.error_box("La password deve essere lunga almeno 4 caratteri e non contenere spazi");
  return ok;
}

void TSet_users::enable_aut(TMask& m)
{
  const bool prassi = (m.get(F_USER) == "PRASSI");

  for (int i = 1; i < ENDAUT; i++)
  {
    if (i == MUAUT) continue;
    const int pos = _msk->id2pos(F_BASE + i);
    if (pos >= 0)
    {
      TMask_field& f = m.fld(pos);

      f.enable_default();
      if (f.enabled())
        f.enable(!prassi && has_module(i, CHK_DONGLE));
      if (prassi) f.set("X");
    }
  }
}

bool TSet_users::user_create()
{
  if (user() != "PRASSI") 
    return error_box("Utente non abilitato all'uso di questo programma");
  _msk = new TMask("ba1400a") ;
  _rel = new TRelation(LF_USER);
  _msk->field(F_MU).disable();
  _msk->set_handler(F_USER, user_handler);
  _msk->set_handler(F_PASSWORD, password_handler);
  return TRUE;
}

int TSet_users::read(TMask& m) 

{
  TRelation_application::read(m);
  const TRelation *r = get_relation();

  m.set(F_PASSWORD, decode(r->lfile().get("PASSWORD")));
  return NOERR;
}

int TSet_users::write(const TMask& m) 

{
  TRelation *r = get_relation();
  TString16 s(encode(m.get(F_PASSWORD)));
  
  r->lfile().put("PASSWORD", (const char *) s);
  return TRelation_application::write(m);
}

int TSet_users::rewrite(const TMask& m) 

{
  TRelation *r = get_relation();
  TString16 s(encode(m.get(F_PASSWORD)));

  r->lfile().put("PASSWORD",  s);
  return TRelation_application::rewrite(m);
}


bool TSet_users::user_destroy()
{
  if (_msk != NULL) delete  _msk;
  if (_rel != NULL) delete  _rel;
  return TRUE;
}

int ba1400(int argc, char** argv)
{
  TSet_users a ;
  a.run(argc, argv, "Configurazione utenti");
  return 0;
}
