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


namespace GMW
{
  public partial class SearchMag : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        txtRicerca = "";
        doTraduci();
        pnlResults.Visible = true; //false;
                                   // se ho in URL la chiave "search" lo riporto come search...
        if (ricercaUrl != "")
        {
          ricerca = ricercaUrl;
          //string request = Request.Url;
          Response.Redirect("SearchMag");
        }
        // se ho in sessione ricerca la mostro...
        if (ricerca != "")
        {
          txtRicerca = ricerca;
        }
        doSearch();
      }
      mod_SearchProvUDC1.eh_selValore += new EventHandler(mod_SearchProvUDC1_eh_selValore);
      mod_SearchProvParticolari1.eh_selValore += new EventHandler(mod_SearchProvParticolari1_eh_selValore);
      mod_SearchProvUDC1.eh_reset += new EventHandler(mod_SearchProvUDC1_eh_reset);
      mod_SearchProvParticolari1.eh_reset += new EventHandler(mod_SearchProvParticolari1_eh_reset);
      txtSearch.Focus();
    }
    /// <summary>
    /// ricerca corrente
    /// </summary>
    protected string ricerca
    {
      get
      {
        return memLayer.ML.StringSessionObj("FullSearchValue");
      }
      set
      {
        if (value != "")
        {
          memLayer.ML.setSessionVal("FullSearchValue", value, false);
        }
        else
        {
          memLayer.ML.emptySessionVal("FullSearchValue");
          doSearch();
        }
      }
    }

    /// <summary>
    /// ricerca corrente
    /// </summary>
    protected string txtRicerca
    {
      get
      {
        return txtSearch.Text.Trim();
      }
      set
      {
        txtSearch.Text = value;
      }
    }
    /// <summary>
    /// restituisce termine ricerca contenuto in URL (se disponibile)
    /// </summary>
    protected string ricercaUrl
    {
      get
      {
        string answ = memLayer.ML.QSS("search");// "";
#if false
        try
        {
            answ = Request.QueryString["search"].ToString();
        }
        catch
        {
            answ = "";
        }  
#endif
        return answ;
      }
    }

    void mod_SearchProvParticolari1_eh_reset(object sender, EventArgs e)
    {
      mod_SearchProvUDC1.doReset();
      resetSessione();
    }
    /// <summary>
    /// richiesta svuotamento da parte di UDC
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void mod_SearchProvUDC1_eh_reset(object sender, EventArgs e)
    {
      resetSessione();
      mod_SearchProvParticolari1.doReset();
    }
    /// <summary>
    /// svuoto variabili in sessione
    /// </summary>
    private void resetSessione()
    {
      // disattivo riceche...
      memLayer.ML.setSessionVal("RicercaUdcAttiva", false, false);
      memLayer.ML.setSessionVal("RicercaParticolariAttiva", false, false);
      // svuoto in session
      memLayer.ML.emptySessionVal("Particolare_sel");
      memLayer.ML.emptySessionVal("Esponente_sel");
      memLayer.ML.emptySessionVal("CodMag_sel");
      memLayer.ML.emptySessionVal("CodCellaMag");
      memLayer.ML.emptySessionVal("IdxCellaMag");
      memLayer.ML.emptySessionVal("IdxBlocco_sel");
      memLayer.ML.emptySessionVal("UDC_sel");
    }

    void mod_SearchProvParticolari1_eh_selValore(object sender, EventArgs e)
    {
      mod_SearchProvUDC1.doRefresh();
    }

    void mod_SearchProvUDC1_eh_selValore(object sender, EventArgs e)
    {
      mod_SearchProvParticolari1.doRefresh();
    }


    private void doTraduci()
    {
      chkLikeType.Text = traduci("AbilitaRicercaLike");
      chkLikeType.ToolTip = traduci("AbilitaRicercaLikeExpl");
      txtSearch.Attributes.Remove("placeholder");
      txtSearch.Attributes.Add("placeholder", traduci("insertCod4search"));
    }
    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
      doSearch();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
      doSearch();
    }
    /// <summary>
    /// effettua la ricerca
    /// </summary>
    private void doSearch()
    {
      memLayer.ML.setSessionVal("FullSearchValue", txtRicerca, false);
      memLayer.ML.setSessionVal("SearchIsLikeBased", chkLikeType.Checked, false);
      if (txtRicerca != "")
      {
        pnlResults.Visible = true;
        if (chkLikeType.Checked)
        {
          // è generica, mostro tutto!
          mod_SearchProvAL1.Visible = true;
          mod_SearchProvAL1.doSearch();
          // NON faccio ricerca LIKE x Dtx !!!
          mod_SearchProvDtx1.Visible = false;
          mod_SearchProvParticolari1.Visible = true;
          mod_SearchProvParticolari1.doSearch();
          mod_SearchProvUDC1.Visible = true;
          mod_SearchProvUDC1.doSearch();
          //mod_SearchProvCelle1.Visible = true;
          //mod_SearchProvCelle1.doSearch();
          //Mod_SearchProvImballi1.Visible = true;
          //Mod_SearchProvImballi1.doSearch();

        }
        else
        {
          // in base al tipo di richiesta determino che risultati visualizzare...
          switch (txtRicerca.Substring(0, 1))
          {
            //case "I":
            //    mod_SearchProvAL1.Visible = false;
            //    mod_SearchProvDtx1.Visible = false;
            //    mod_SearchProvParticolari1.Visible = false;
            //    mod_SearchProvUDC1.Visible = false;
            //    //mod_SearchProvCelle1.Visible = false;
            //    //Mod_SearchProvImballi1.Visible = true;
            //    //Mod_SearchProvImballi1.doSearch();
            //    break;
            case "A":
              mod_SearchProvAL1.Visible = true;
              mod_SearchProvAL1.doSearch();
              mod_SearchProvDtx1.Visible = false;
              mod_SearchProvParticolari1.Visible = false;
              mod_SearchProvUDC1.Visible = false;
              //mod_SearchProvCelle1.Visible = true;
              //mod_SearchProvCelle1.doSearch();
              //Mod_SearchProvImballi1.Visible = false;
              break;
            case "U":
              mod_SearchProvAL1.Visible = false;
              mod_SearchProvDtx1.Visible = false;
              mod_SearchProvUDC1.Visible = true;
              mod_SearchProvUDC1.doSearch();
              //mod_SearchProvCelle1.Visible = false;
              mod_SearchProvParticolari1.Visible = false;
              //Mod_SearchProvImballi1.Visible = false;
              break;
            default:
              mod_SearchProvAL1.Visible = true;
              mod_SearchProvAL1.doSearch();
              mod_SearchProvDtx1.Visible = true;
              mod_SearchProvDtx1.doSearch();
              mod_SearchProvParticolari1.Visible = true;
              mod_SearchProvParticolari1.doSearch();
              mod_SearchProvUDC1.Visible = true;
              mod_SearchProvUDC1.doSearch();
              //mod_SearchProvCelle1.Visible = true;
              //mod_SearchProvCelle1.doSearch();
              //Mod_SearchProvImballi1.Visible = true;
              //Mod_SearchProvImballi1.doSearch();
              break;
          }
        }
      }
      else
      {
        memLayer.ML.emptySessionVal("FullSearchValue");
        pnlResults.Visible = false;
      }
    }
    /// <summary>
    /// wrapper metodo traduzione
    /// </summary>
    /// <param name="lemma"></param>
    /// <returns></returns>
    public string traduci(string lemma)
    {
      return user_std.UtSn.Traduci(lemma);
    }
    /// <summary>
    /// abilita ricerca "like"
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void chkLikeType_CheckedChanged(object sender, EventArgs e)
    {
      doSearch();
    }

    /// <summary>
    /// effettuato reset
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkReset_Click(object sender, EventArgs e)
    {
      txtSearch.Text = "";
      doSearch();
    }
  }
}
