#include <stdlib.h>

#include <applicat.h>
#include <isam.h>
#include <mask.h>
#include <progind.h>
#include <sheet.h>
#include <urldefid.h>
#include <utility.h>
#include <validate.h>

#include "ba1.h"
#include "ba1300.h"
#include "ba1300a.h"


class BA1300_application:public TApplication
{
  TTestSheet    * _testsheet;
  TTestProc     * _test,
  * _debug;
  TMask         * _mask;
  long            _firm;

protected:
  virtual bool menu(MENU_TAG);
  virtual bool create () ;
  virtual bool destroy();
  virtual bool extended_firm() const { return TRUE; }

public:
  BA1300_application() : _testsheet(NULL), _mask(NULL) {}
  TTestSheet * get_sheet() {return _testsheet;}
  TTestProc  * get_test()  {return _test;}
  TMask *      get_mask()  {return _mask;}
  TArray_sheet * field_sheet(TMask *);

};

HIDDEN bool seq_handler(TMask_field& f, KEY key)
{
  if (key==K_ENTER)
    if (!fexist((const char*)f.get()))
    {
      warning_box("File inesistente");
      return FALSE;
    }
  return TRUE;
}

bool BA1300_application::create() // initvar e arrmask

{
  TApplication::create();

  _firm = get_firm();
  
  if (argc() > 2)
  {
    return FALSE;
  }
  else
  {
    _mask = new TMask ("ba1300a");  // Maschera principale
    set_firm();
    _testsheet = new TTestSheet("Selezione Files") ;  
    get_sheet()->add_button(DLG_RESET,"~Reset",K_F6);
  }  
  return TRUE ;
}

bool BA1300_application::destroy() // releasev e arrmask

{
  if (_firm) set_firm(_firm);
  if (_testsheet != NULL) delete  _testsheet;
  if (_mask != NULL) delete  _mask;
  return TApplication::destroy() ;
}

TArray_sheet * BA1300_application::field_sheet(TMask * m)
{
  TEdit_field& f=(TEdit_field&) m->field(F_FILE);
  TArray_sheet * s=(TArray_sheet*) f.sheet()->sheet();
  return s;
}

bool BA1300_application::menu(MENU_TAG m)
{
  KEY tasto,tastoS;
  long numtest;
  TMask *msk;
  TDir  *cdir;

  disable_menu_item(M_FILE_NEW);
  switch (m)
  {
  case BAR_ITEM(2):
  {
    msk = new TMask("ba1300b");
    cdir = new TDir;
    cdir->get(LF_DIR,_nolock,_nordir,_sysdirop);
    const int nitems=(int)cdir->eod();
    for (int i=0; i<nitems;i++) // fill sheet
    {
      TToken_string riga(128);
      riga.cut(0);
      cdir->get(i+1,_nolock,_nordir,_sysdirop);
      riga=format("%3d",i+1);
      riga.add(cdir->name());
      riga.add(format("%ld",cdir->eod()));
      riga.add(format("%ld",cdir->eox()));
      riga.add(format("%u",cdir->len()));
      riga.add(cdir->des());
      field_sheet(msk)->add(riga);
    }
    msk->set_handler(F_SEQUENZA,seq_handler);
    if (msk->run()==K_ENTER)
    {
      _debug = new TTestProc;
      _debug->set_file(msk->get_int(F_FILE));
      _debug->load_random(msk->get(F_SEQUENZA));                                 
      if (!_debug->stopped())
        _debug->do_debug();
      else
        warning_box("File di sequenza non valido");
      delete _debug;
    }
    delete msk;
    delete cdir;
  }  
  break;
 case BAR_ITEM(1):
  do {
    switch(tasto=get_mask()->run())
    {
    case K_ENTER:  
      if (get_sheet()->checked()>0 && (get_mask()->get_long(F_ITERAZIONI)>0 || get_mask()->get_bool(F_CONTINUO)))
      {
        _test = new TTestProc;
        get_test()->set_sheet(get_sheet());
        get_test()->set_rmc();
        get_test()->get_win()->set_color(COLOR_BLACK,COLOR_WHITE);
        get_test()->get_win()->clear(COLOR_WHITE);
        get_test()->get_win()->set_caption("Verifica di stabilita'");
        get_test()->get_win()->WriteW(10,5,"Test N.             :");
        get_test()->get_win()->WriteW(10,7,"Nome File           :");
        get_test()->get_win()->WriteW(10,9,"Record cancellati   :");
        get_test()->get_win()->WriteW(10,10,"Operazione in corso :");
        get_test()->get_win()->WriteW(10,12,"Tempo trascorso     :");
        get_test()->get_win()->WriteW(10,13,"Tempo stimato       :");
        get_test()->get_win()->WriteW(10,15,"Ultimo Errore       :");
        numtest=0;
        while (((++numtest)<=get_mask()->get_long(F_ITERAZIONI) || get_mask()->get_bool(F_CONTINUO)) && (!get_test()->stopped()))
        {
          if (get_mask()->get_bool(F_CONTINUO))
            get_test()->get_win()->WriteW(33,5,format("%4ld (CONTINUO)",numtest));
          else
            get_test()->get_win()->WriteW(33,5,format("%4ld/%-4ld",numtest,_mask->get_long(F_ITERAZIONI)));
          get_test()->set_numt(numtest);
          get_test()->do_test_1();
        }
        if (get_test()->rm_copy())
        { 
          get_test()->get_win()->WriteW(33,10,"CANCELLAZIONE COPIE         ");
          for  (int i=0;i<get_sheet()->items();i++)
            if (get_sheet()->checked(i))
              get_test()->remove_copy(i+1);
        }
        delete _test;
      }
      else
        warning_box("Nessun file selezionato");
      break;
    case K_F4:      
      get_sheet()->sr_status();
      while ((tastoS=get_sheet()->run()) == K_F6) 
        for (int i=0; i<get_sheet()->items();i++) 
          get_sheet()->uncheck(i);
      if (tastoS == K_ESC)                     
        get_sheet()->sr_status(FALSE); 
      else
      {  // Cannot select first two files.
        get_sheet()->uncheck(0);
        get_sheet()->uncheck(1);
      }
      break;
    case K_F5:      
      if (fexist((const char*)TString(20).format("test%creport.txt",Separator)))
      {
        TTestReport rep_win("Report");
        rep_win.load_report();
        rep_win.run();
      } else
        warning_box("Report non disponibile");
      break;
    default:
      break;
    }
  } while (tasto != K_ESC);
  break;
}
  enable_menu_item(M_FILE_NEW);
  return xvt_test_menu_tag(BAR_ITEM(2));
}

int ba1300(int argc, char** argv)
{
  BA1300_application a;
  a.run(argc,argv,"Test dell'ISAM");
  return 0;
}
