using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class address_book_Default : System.Web.UI.Page
{
    static String PAGINA = "Default.aspx";
    static String PAGINA_INSERT = "Insert.aspx";
    static String PAGINA_ERRO = "../Default.aspx";

    String[] array_letras = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", 
                              "U", "V", "W", "X", "Y", "Z" };

    String[] array_pesquisas = { "Nome", "Tipo" };

    protected void Page_Load(object sender, EventArgs e)
    {
        tag_novo_contacto.HRef = PAGINA_INSERT;

        try
        {
            String pesquisa = Request.QueryString["pesquisa"];

            LinkButton l_button_ = new LinkButton();
            l_button_.PostBackUrl = PAGINA + "?pesquisa=1LETRA&id_letra=1" ;
            l_button_.Text ="# ";
            l_button_.CausesValidation = false;
            placeholder_letras.Controls.Add(l_button_);

            for (int i = 0; i < array_letras.Length; i++)
            {
                LinkButton l_button = new LinkButton();
                l_button.PostBackUrl = PAGINA + "?pesquisa=1LETRA&id_letra=" + array_letras[i];
                l_button.Text = array_letras[i] + " ";
                l_button.CausesValidation = false;
                placeholder_letras.Controls.Add(l_button);
            }


            if (pesquisa != null && pesquisa.CompareTo("1LETRA") == 0 && Request.QueryString["id_letra"]!= null)
            {
                
                String id_letra = Request.QueryString["id_letra"];

                

                if ( id_letra.Equals("1"))
                {
                    DataClassesDataContext dc = new DataClassesDataContext();
                    
                    var contactos = from p in dc.CONTACTOs
                                    where p.NOME.StartsWith("0") == true
                                    || p.NOME.StartsWith("1") == true
                                    || p.NOME.StartsWith("2") == true
                                    || p.NOME.StartsWith("3") == true
                                    || p.NOME.StartsWith("4") == true
                                    || p.NOME.StartsWith("5") == true
                                    || p.NOME.StartsWith("6") == true
                                    || p.NOME.StartsWith("7") == true
                                    || p.NOME.StartsWith("8") == true
                                    || p.NOME.StartsWith("9") == true 
                                    
                                    orderby p.NOME ascending
                                    select p;

                    grid_view_contactos.DataSource = contactos;
                    grid_view_contactos.DataBind(); 
                }
                else
                {
                    DataClassesDataContext dc = new DataClassesDataContext();
                    var contactos = from p in dc.CONTACTOs
                                    where p.NOME.StartsWith(id_letra) == true
                                    || p.NOME.StartsWith(id_letra.ToLower()) == true
                                    orderby p.NOME ascending
                                    select p;

                    grid_view_contactos.DataSource = contactos;
                    grid_view_contactos.DataBind();
               }

                
               
                
            }
            else
            {

                DataClassesDataContext dc = new DataClassesDataContext();
                var contactos = from p in dc.CONTACTOs
                                orderby p.NOME ascending
                                select p;

                grid_view_contactos.DataSource = contactos;
                grid_view_contactos.DataBind();
            }

        }
        catch (Exception ex)
        {

        }
    }


    protected void Button_Pesquisar_Click(object sender, EventArgs e)
    {
        String pesquisa = text_box_pesquisa.Text;
        int index_drop_box = drop_down_esquisa.SelectedIndex;
        
        try
        {
            DataClassesDataContext dc = new DataClassesDataContext();

            if (index_drop_box == 0)
            {                
                // nome
                var contactos = from p in dc.CONTACTOs
                              where p.NOME.Contains(pesquisa)
                              orderby p.ID_CONTACTO ascending
                              select p;
                
                grid_view_contactos.DataSource = contactos;
                grid_view_contactos.DataBind();
            }

            else if (index_drop_box == 1)
            {
                // Tipo

                var contactos = from p in dc.CONTACTOs
                                where p.TIPO.Contains(pesquisa)
                                orderby p.ID_CONTACTO ascending
                                select p;

                grid_view_contactos.DataSource = contactos;
                grid_view_contactos.DataBind();
            }

        }
        catch (Exception ex)
        {
        }
    }

    protected void Button_Reset_Click(object sender, EventArgs e)
    {
        Response.Redirect(PAGINA);
    }

    public void Mostra_Todos_Contactos()
    {
        try
        {

        }
        catch (Exception ex)
        {
        }
    }

    protected void Delete_Section(object sender, EventArgs e)
    {

        LinkButton lbutton = (LinkButton)sender;

        Response.Redirect(lbutton.CommandArgument);

    }
}
