using SteamWare;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mod_filtro : ApplicationUserControl
{
  protected ObjectDataSource _ods;
  protected string _valore
  {
    get
    {
      return memLayer.ML.StringSessionObj(string.Format("valFiltro_{0}", this.ID));
    }
    set
    {
      memLayer.ML.setSessionVal(string.Format("valFiltro_{0}", this.ID), value, false);
    }
  }
  protected bool _changeCheckEnabled = true;
  protected bool _changeCheckVisible = true;
  protected bool _changeSelEnabled = true;
  protected string _where = "";
  protected string _showFiltered
  {
    get
    {
      return memLayer.ML.StringSessionObj(string.Format("_showFiltered_{0}", this.ID));
    }
    set
    {
      memLayer.ML.setSessionVal(string.Format("_showFiltered_{0}", this.ID), value);
      traduciObj();
    }
  }
  protected string _showAll
  {
    get
    {
      return memLayer.ML.StringSessionObj(string.Format("_showAll_{0}", this.ID));
    }
    set
    {
      memLayer.ML.setSessionVal(string.Format("_showAll_{0}", this.ID), value);
      traduciObj();
    }
  }

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);
    if (!Page.IsPostBack)
    {
      if (string.IsNullOrEmpty(_showFiltered)) _showFiltered = "mostraSoloSelez";
      if (string.IsNullOrEmpty(_showAll)) _showAll = "mostraTutti";
    }
  }

  /// <summary>
  /// traduzione oggetti
  /// </summary>
  protected override void traduciObj()
  {
    if (_changeCheckVisible)
    {
      if (!chkFilt.Checked)
      {
        chkFilt.Text = traduci(_showFiltered);
      }
      else
      {
        chkFilt.Text = traduci(_showAll);
      }
    }
  }
  /// <summary>
  /// get/set messaggio di attivazione filtraggio
  /// </summary>
  public string filterChekText
  {
    get
    {
      return _showFiltered;
    }
    set
    {
      _showFiltered = value;
    }
  }
  /// <summary>
  /// get/set messaggio di disattivazione filtraggio
  /// </summary>
  public string filterUnchekText
  {
    get
    {
      return _showAll;
    }
    set
    {
      _showAll = value;
    }
  }
  protected void chkFilt_CheckedChanged(object sender, EventArgs e)
  {
    // cambio modalità visualizzazione del filtro...
    dlFiltr.Visible = !dlFiltr.Visible;
    raiseSelEvent();
  }

  protected void raiseSelEvent()
  {
    if (!dlFiltr.Visible)
    {
      memLayer.ML.setSessionVal(string.Format("valFiltro_{0}", this.ID), "", false);
    }
    else
    {
      _valore = dlFiltr.SelectedValue;
    }
    if (eh_selValore != null)
    {
      eh_selValore(this, new EventArgs());
    }
  }
  protected void updateChk()
  {
    chkFilt.Enabled = _changeCheckEnabled;
    if (!_changeCheckEnabled)
    {
      chkFilt.Checked = true;
      dlFiltr.Visible = true;
    }
  }
  protected void updateSel()
  {
    dlFiltr.Enabled = _changeSelEnabled;
  }
  protected void updateChkLbl()
  {
    chkFilt.Visible = _changeCheckVisible;
  }
  protected override void bindControlli()
  {
    updateChk();
    fixValore();
    where.Text = _where;
    traduciObj();
  }
  protected SteamWare.tipoVistaMod _modo = SteamWare.tipoVistaMod.editing;
  protected void dlFiltr_SelectedIndexChanged(object sender, EventArgs e)
  {
    raiseSelEvent();
  }


  /// <summary>
  /// condizione where per ridurre elementi mostrati in selettore
  /// </summary>
  public string condizione
  {
    get
    {
      return _where;
    }
    set
    {
      _where = value;
    }
  }

  /// <summary>
  /// fornisce il valore scelto o "*" se nulla selezionato
  /// </summary>
  public string valore
  {
    get
    {
      string answ = "*";
      if (chkFilt.Checked)
      {
        answ = dlFiltr.SelectedValue;
      }
      return answ;
    }
    set
    {
      _valore = value;
      fixValore();
    }
  }

  /// <summary>
  /// primo valore di una chiave multicampo
  /// </summary>
  public string val_1
  {
    get
    {
      string answ = "";
      if (chkFilt.Checked)
      {
        try
        {
          string[] _dataKey = dlFiltr.SelectedValue.Split('#');
          answ = _dataKey[0];
        }
        catch
        { }
      }
      else
      {
        answ = "*";
      }
      return answ;
    }
  }
  /// <summary>
  /// secondo valore di una chiave multicampo
  /// </summary>
  public string val_2
  {
    get
    {
      string answ = "";
      if (chkFilt.Checked)
      {
        try
        {
          string[] _dataKey = dlFiltr.SelectedValue.Split('#');
          answ = _dataKey[1];
        }
        catch
        { }
      }
      else
      {
        answ = "*";
      }
      return answ;
    }
  }
  /// <summary>
  /// fornisce il valore scelto o "0" se nulla selezionato o non valido
  /// </summary>
  public int valoreInt
  {
    get
    {
      int answ = -1;
      try
      {
        if (chkFilt.Checked)
        {
          answ = Convert.ToInt32(dlFiltr.SelectedValue);
        }
      }
      catch
      { }
      return answ;
    }
  }
  protected void fixValore()
  {
    if (_ods != null)
    {
      try
      {
        dlFiltr.DataSource = _ods;
        dlFiltr.DataBind();
        if (_valore != "")
        {
          dlFiltr.SelectedValue = _valore;
          dlFiltr.Visible = true;
          chkFilt.Checked = true;
        }
      }
      catch
      { }
    }
  }


  public event EventHandler eh_selValore;

  /// <summary>
  /// determina se sia possibile (de)selezionare il check di filtraggio, altrimenti sempre attivo
  /// </summary>
  public bool changeCheckEnabled
  {
    get
    {
      return _changeCheckEnabled;
    }
    set
    {
      _changeCheckEnabled = value;
      updateChk();
    }
  }
  /// <summary>
  /// determina se sia possibile modificare la selezione
  /// </summary>
  public bool changeSelEnabled
  {
    get
    {
      return _changeSelEnabled;
    }
    set
    {
      _changeSelEnabled = value;
      updateSel();
    }
  }
  /// <summary>
  /// determina se sia possibile visibile il check di filtraggio
  /// </summary>
  public bool changeCheckVisible
  {
    get
    {
      return _changeCheckVisible;
    }
    set
    {
      _changeCheckVisible = value;
      updateChkLbl();
    }
  }
  /// <summary>
  /// get/set della checkbox
  /// </summary>
  public bool isChecked
  {
    get
    {
      return chkFilt.Checked;
    }
    set
    {
      chkFilt.Checked = value;
      dlFiltr.Visible = value;
      updateChkLbl();
    }
  }

  /// <summary>
  /// oggetto ODS con cui popolare il selettore, VINCOLO abbia campi value(key) / label
  /// </summary>
  public ObjectDataSource ods
  {
    get
    {
      return _ods;
    }
    set
    {
      _ods = value;
      bindControlli();
    }
  }
  /// <summary>
  /// resetta il controllo (un-checked)
  /// </summary>
  public void reset()
  {
    chkFilt.Checked = false;
    dlFiltr.Visible = false;
    memLayer.ML.emptySessionVal(string.Format("valFiltro_{0}", this.ID));
  }
  /// <summary>
  /// get/set per la larghezza della combo dropdown
  /// </summary>
  public int comboWidth
  {
    get
    {
      return Convert.ToInt32(dlFiltr.Width);
    }
    set
    {
      dlFiltr.Width = value;
    }
  }

}