#ifndef __UTILITY_H
#include <utility.h>
#endif

#ifndef __PCONTI_H
#include <pconti.h>
#endif

#include <defmask.h>

#include "sc2201.h"

///////////////////////////////////////////////////////////////////////////////
// TSelection_ext_mask                                                       //
///////////////////////////////////////////////////////////////////////////////

TSelection_ext_mask::TSelection_ext_mask(const char* name) : TSelection_mask(name),
           _pcon_rel(NULL), _pcon_cur_k1(NULL), _pcon_cur_k2(NULL), _pcon_sh_k1(NULL), _pcon_sh_k2(NULL)
{     
  _pcon_rel  = new TRelation(LF_PCON);
  
  _pcon_cur_k1 = new TCursor(_pcon_rel, "", 1);
  _pcon_cur_k2 = new TCursor(_pcon_rel, "", 2);
  
  
  _pcon_sh_k1 = new TCursor_sheet(_pcon_cur_k1, " |GRUPPO|CONTO|SOTTOCONTO|DESCR", "Selezione conti per gruppo/conto/sottoconto",
                                 "@1|Gruppo|Conto|Sottoconto|Descrizione@50",0,1);
  _pcon_sh_k2 = new TCursor_sheet(_pcon_cur_k2, " |DESCR|GRUPPO|CONTO|SOTTOCONTO", "Selezione conti per descrizione",
                                 "@1|Descrizione@50|Gruppo|Conto|Sottoconto",0,1);

  set_handler(SC_CLIFO,   rpcon_handler);  // Redefined handler...
  set_handler(SC_SORTCF,  rsortpcon_handler); // Redefined handler...
  set_handler(SC_SELECT,  bselectpcon_handler); // Redefined handler...
  set_handler(SC_RESET,   bresetpcon_handler);  // Redefined handler...
  set_handler(SC_PCONCODFR3, ffrompcon_handler); // New handler...
  set_handler(SC_PCONCODTO3, ftopcon_handler);   // New handler...
  set_handler(SC_PCONDESFR, fdfrompcon_handler); // New handler...
  set_handler(SC_PCONDESTO, fdtopcon_handler);   // New handler...
}

TSelection_ext_mask::~TSelection_ext_mask()
{
  delete _pcon_sh_k2;
  delete _pcon_sh_k1;
  delete _pcon_cur_k2;
  delete _pcon_cur_k1;
  delete _pcon_rel;
}             

TCursor_sheet& TSelection_ext_mask::cur_sheet()
{             
  TCursor_sheet* cs;
  if (get_key() == 1) cs = get_who() == 'P' ? _pcon_sh_k1 : NULL;
  else                cs = get_who() == 'P' ? _pcon_sh_k2 : NULL;
  
  if (get_who() != 'P')
    return TSelection_mask::cur_sheet();
  CHECK(cs, "Can't use a NULL TCursor_sheet");
  return *cs;
}                 

void TSelection_ext_mask::reset_sheets()
{
  _pcon_sh_k1->uncheck(-1);
  _pcon_sh_k2->uncheck(-1);
  TSelection_mask::reset_sheets();
  reset(SC_PCONCODFR1);
  reset(SC_PCONCODFR2);
  reset(SC_PCONCODFR3);
  reset(SC_PCONCODTO1);
  reset(SC_PCONCODTO2);
  reset(SC_PCONCODTO3);
  reset(SC_PCONDESFR);
  reset(SC_PCONDESTO);
}

void TSelection_ext_mask::select_des_pcon_range(const TString& from, const TString& to)
{
  TCursor_sheet& c = cur_sheet();
  const long items = c.items();
  const int key = get_key();   
  CHECK(key == 2, "La chiave deve essere la 2");
  TString s_from(from), s_to(to);
  
  if (s_to.empty() && items)
    s_to = c.row(items-1).get(1);
  s_from.upper();
  s_to.upper();
  
  if (s_from > s_to)                    // Controlla limiti
  {
    s_to = from;
    s_from = to;
    set(SC_PCONDESFR, to);
    set(SC_PCONDESTO, from);
  }
  else
    {
      if (to.not_empty()) s_to = to;
      s_from = from;
    }

  TString last;
  TString firs;
  
  TCursor* crs = c.cursor();
  TRectype& rec = crs->file().curr();
  rec.zero();
  rec.put(PCN_DESCR,s_from);
  const TRecnotype start = crs->read(_isgteq);
  firs = rec.get(PCN_DESCR);
  rec.zero();
  rec.put(PCN_DESCR,s_to);
  TRectype recx(rec);
  TRecnotype end = crs->read(_isgteq);
  if (rec != recx) 
  {
    end--;
    (*crs)-=1;
  }
  last = rec.get(PCN_DESCR);
  c.uncheck(-1);
  for (long i = start; i <= end; i++)
      c.check(i);
  if (get(SC_PCONDESFR).not_empty())
    set(SC_PCONDESFR, firs);
  if (get(SC_PCONDESTO).not_empty())
    set(SC_PCONDESTO, last);
  set(SC_NSEL, c.checked());
}

void TSelection_ext_mask::set_des_pcon_limits()
{  
  long first=-1, last=-1;
  TString from,to;
  TCursor_sheet& c = cur_sheet();
  const long items = c.items();
  const int key = get_key();
  CHECK(key == 2, "La chiave deve essere la 2");
  
  for (long i = 0; i < items; i++) 
    if (c.checked(i))
    {
      if (first == -1)
        first = i;
    }
  if (first != -1)
    for (long j = (items-1); j >= 0 ; j--)
      if (c.checked(j))
        if (last == -1)
        {
          last = j;
          break;
        }
  if (first!= -1 && last != -1)
  {
    TToken_string fitem(c.row(first));
    TToken_string litem(c.row(last));
    from = fitem.get(1);
    to   = litem.get(1);
    if (from>to)
    {
      TString temp(to);
      to=from;
      from=temp;
    }
  }
  set(SC_PCONDESFR, from);
  set(SC_PCONDESTO, to);
  set(SC_NSEL,    c.checked());
}

void TSelection_ext_mask::select_pcon_range(int grf, int cof,long sof,int grt,int cot, long sot)
{
  TCursor_sheet& c = cur_sheet();
  const long items = c.items();
  const int key = get_key();
  
  TCursor* crs = c.cursor();
  if (grt==0 && cot==0 && sot==0 && items)      
  { 
     TToken_string& t = c.row(items-1);
     grt = t.get_int(key);
     cot = t.get_int(key+1);
     sot = t.get_long(key+2);
  }
  if (grf > grt || (grf==grt && cof > cot) || (grf==grt && cof==cot && sof>sot))
  {     // Swap them...
    int t1 = grf,t2 = cof;long t3 = sof;
    grf=grt;cof=cot;sof=sot; 
    grt=t1;cot=t2;sot=t3;
  }

  TRectype& rec = crs->file().curr();
  rec.zero();
  rec.put(PCN_GRUPPO,grf);
  rec.put(PCN_CONTO,cof);
  rec.put(PCN_SOTTOCONTO,sof);
  
  const TRecnotype start = crs->read(_isgteq);
  grf = rec.get_int(PCN_GRUPPO);
  cof = rec.get_int(PCN_CONTO);
  sof = rec.get_long(PCN_SOTTOCONTO);
  rec.zero();
  rec.put(PCN_GRUPPO,grt);
  rec.put(PCN_CONTO,cot);
  rec.put(PCN_SOTTOCONTO,sot);
  TRectype recx(rec);
  TRecnotype end = crs->read(_isgteq);
  if (rec != recx) 
  {
    end--;
    (*crs)-=1;
  }
  grt = rec.get_int(PCN_GRUPPO);
  cot = rec.get_int(PCN_CONTO);
  sot = rec.get_long(PCN_SOTTOCONTO);
  c.uncheck(-1);
  for (long i = start; i <= end; i++)
      c.check(i);
  set(SC_PCONCODFR1, grf);
  set(SC_PCONCODFR2, cof);
  set(SC_PCONCODFR3, sof);
  set(SC_PCONCODTO1, grt);
  set(SC_PCONCODTO2, cot);
  set(SC_PCONCODTO3, sot);
  set(SC_NSEL,    c.checked());
}

void TSelection_ext_mask::set_pcon_limits()
{  
  int gf=0,gl=0,cf=0,cl=0;
  long sf=0L,sl=0L;
  
  TCursor_sheet& c = cur_sheet();
  const long items = c.items();
  const int key = get_key();
  long first = -1, last = -1;
  
  // Ricerca veloce di estremi (vale solo su GR/CO/SO)
  for (long i = 0; i < items; i++) 
    if (c.checked(i))
    {
      if (first == -1)
        first = i;
    }
  if (first != -1)  //Optimization... If nothing was found previously skip this test.
    for (long j = (items-1); j >= 0 ; j--)
      if (c.checked(j))
        if (last == -1)
        {
          last = j;
          break;
        }
  if (first!= -1 && last != -1) // Something selected?
  {
    TToken_string fitem(c.row(first));
    TToken_string litem(c.row(last));
    gf = fitem.get_int(key);
    cf = fitem.get_int(key+1);
    sf = fitem.get_long(key+2);
    gl = litem.get_int(key);
    cl = litem.get_int(key+1);
    sl = litem.get_long(key+2);
    if (gf > gl || (gf==gl && cf > cl) || (gf==gl && cf==cl && sf>sl))
    {     // Swap them...
      int t1 = gf,t2 = cf;long t3 = sf;
      gf=gl;cf=cl;sf=sl; 
      gl=t1;cl=t2;sl=t3;
    }
  }
  set(SC_PCONCODFR1, gf);
  set(SC_PCONCODFR2, cf);
  set(SC_PCONCODFR3, sf);
  set(SC_PCONCODTO1, gl);
  set(SC_PCONCODTO2, cl);
  set(SC_PCONCODTO3, sl);
  set(SC_NSEL,    c.checked());
}

bool TSelection_ext_mask::ffrompcon_handler(TMask_field& f, KEY k)
{    
  TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
                                   
  if (k == K_F9)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    TCursor_sheet& c = m.cur_sheet();
    const int key = m.get_key();
    c.disable_check();
    c.disable(DLG_USER);
    if (c.run() == K_ENTER)
    {      
      TToken_string& t = c.row(c.selected());
      int grf,grt,cof,cot;
      long sof,sot;
      grf =t.get_int(key);
      cof =t.get_int(key+1);
      sof =t.get_long(key+2);
      grt = m.get_int(SC_PCONCODTO1);
      cot = m.get_int(SC_PCONCODTO2);
      sot = m.get_long(SC_PCONCODTO3);
      m.set(SC_PCONCODFR1, grf);
      m.set(SC_PCONCODFR2, cof);
      m.set(SC_PCONCODFR3, sof);
      m.select_pcon_range(grf,cof,sof,grt,cot,sot);
    }
    c.enable(DLG_USER);
  }
  else
    if (k == K_TAB && f.focusdirty())
    {
      int grf,grt,cof,cot;
      long sof,sot;
      grf = m.get_int(SC_PCONCODFR1);
      cof = m.get_int(SC_PCONCODFR2);
      sof = m.get_long(SC_PCONCODFR3);
      grt = m.get_int(SC_PCONCODTO1);
      cot = m.get_int(SC_PCONCODTO2);
      sot = m.get_long(SC_PCONCODTO3);
      m.select_pcon_range(grf,cof,sof,grt,cot,sot);
    }
  return TRUE;
}

bool TSelection_ext_mask::ftopcon_handler(TMask_field& f, KEY k)
{                                                
  TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
                                 
  if (k == K_F9)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    TCursor_sheet& c = m.cur_sheet();
    const int key = m.get_key();
    c.disable_check();
    c.disable(DLG_USER);
    if (c.run() == K_ENTER)
    {      
      TToken_string& t = c.row(c.selected());
      int grf,grt,cof,cot;
      long sof,sot;
      grt =t.get_int(key);
      cot =t.get_int(key+1);
      sot =t.get_long(key+2);
      grf = m.get_int(SC_PCONCODFR1);
      cof = m.get_int(SC_PCONCODFR2);
      sof = m.get_long(SC_PCONCODFR3);
      m.set(SC_PCONCODTO1, grt);
      m.set(SC_PCONCODTO2, cot);
      m.set(SC_PCONCODTO3, sot);
      m.select_pcon_range(grf,cof,sof,grt,cot,sot);
    }
    c.enable(DLG_USER);
  }
  else
    if (k == K_TAB && f.focusdirty())
    {
      int grf,grt,cof,cot;
      long sof,sot;
      grf = m.get_int(SC_PCONCODFR1);
      cof = m.get_int(SC_PCONCODFR2);
      sof = m.get_long(SC_PCONCODFR3);
      grt = m.get_int(SC_PCONCODTO1);
      cot = m.get_int(SC_PCONCODTO2);
      sot = m.get_long(SC_PCONCODTO3);
      m.select_pcon_range(grf,cof,sof,grt,cot,sot);
    }
  return TRUE;
}

bool TSelection_ext_mask::fdfrompcon_handler(TMask_field& f, KEY k)
{    
  TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();

  if (k == K_F9)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    TCursor_sheet& c = m.cur_sheet();
    
    c.disable_check();
    c.disable(DLG_USER);
    if (c.run() == K_ENTER)
    {      
      TToken_string& t = c.row(c.selected());
      const TString des1(t.get(3 - m.get_key()));
      const TString des2 = m.get(SC_PCONDESTO);
      m.set(SC_PCONDESFR, des1);
      m.select_des_pcon_range(des1, des2);
    }
    c.enable(DLG_USER);
  }
  else
    if (k == K_TAB && f.focusdirty())
    {
      const TString des1(f.get());
      const TString des2(m.get(SC_PCONDESTO));
      m.select_des_pcon_range(des1, des2);
    }
  return TRUE;
}

bool TSelection_ext_mask::fdtopcon_handler(TMask_field& f, KEY k)
{                                                
  TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();

  if (k == K_F9)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    TCursor_sheet& c = m.cur_sheet();

    c.disable_check();
    c.disable(DLG_USER);
    if (c.run() == K_ENTER)
    {      
      TToken_string& t = c.row(c.selected());
      const TString des1(m.get(SC_PCONDESFR));
      const TString des2(t.get(3 - m.get_key()));
      m.set(SC_PCONDESTO, des2);
      m.select_des_pcon_range(des1, des2);
    }
    c.enable(DLG_USER);
  }
  else
    if (k == K_TAB && f.focusdirty())
    {
      const TString des2(f.get());
      const TString des1(m.get(SC_PCONDESFR));
      m.select_des_pcon_range(des1, des2);
    }
  return TRUE;
}

bool TSelection_ext_mask::bresetpcon_handler(TMask_field& f, KEY k)
{
  if (k == K_SPACE)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    m.reset_sheets();
  }  
  return TRUE;
}

bool TSelection_ext_mask::bselectpcon_handler(TMask_field& f, KEY k)
{
  if (k == K_SPACE)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    TCursor_sheet& c = m.cur_sheet();
    c.enable_check();
    c.run();
    char who = m.get_who();
    int key  = m.get_key();
    if (who =='P')
      if (key == 2)
        m.set_des_pcon_limits();
      else
        m.set_pcon_limits();
    else
      if (key == 2)
        m.set_des_clifo_limits();
      else
        m.set_clifo_limits();
  }
  return TRUE;
}

bool TSelection_ext_mask::rpcon_handler(TMask_field& f, KEY k)
{   
  if (k == K_SPACE)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    char who = f.get()[0];
    if (who != m.get_who())
    {
      m.set_key(1);
      m.set_who(who);
    }
    m.reset_sheets();
  }
  return TRUE;
}

bool TSelection_ext_mask::rsortpcon_handler(TMask_field& f, KEY k)
{   
  if (k == K_SPACE)
  {
    TSelection_ext_mask& m = (TSelection_ext_mask&)f.mask();
    const int ordine = atoi(f.get());
    TString   tipo   = m.get(SC_CLIFO);
    bool g3 = FALSE, g4 = FALSE, g5 = FALSE, g6 = FALSE;
    if (tipo != "P")
    {
      g3 = ordine == 1;
      g4 = !g3;
    }
    else
    {
      g5 = ordine == 1;
      g6 = !g5;
    }
    m.show(-3, g3); // Abilita clifo per codice
    m.show(-4, g4); // Abilita clifo per ragsoc
    m.show(-5, g5); // Abilita pcon per conto
    m.show(-6, g6); // Abilita pcon per descrizione
    m.set_key(ordine);
    m.reset_sheets();
  }
  return TRUE;
}
