using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuintoCaminoV2.Data_objects;
using System.Xml;
using System.Data;
using System.IO;
using System.Xml.Xsl;
using System.Text;
namespace QuintoCaminoV2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getCharlas();
            getCursos();
            LeerXML();
        }


        public void LeerXML()
        {

            int i = 0;
            //var files = from arch in Directory.GetFiles(Server.MapPath(".") + "/Blog/App_Data/posts/")
            //            let archivo = new FileInfo(arch)
            //            orderby archivo.CreationTime
            //            select archivo.Name;


            //DataSet ds = new DataSet();
            //ds.ReadXml(Server.MapPath("~/Blog/App_Data/posts/" + files.Last().ToString()));

            //int numer = ds.Tables.Count;

            //lblTitulo.Text = ds.Tables[0].Rows[0][1].ToString();
            //lbldescripcion.Text = ds.Tables[0].Rows[0][2].ToString();
            //hplnkArt.NavigateUrl = "/Blog/post/" + ds.Tables[0].Rows[0][11].ToString();


            string strXmlSrc = "http://www.quintocamino.org/blog/syndication.axd";
            string strXslFile = Server.MapPath("uploads/rssTransform.xsl");

            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(strXmlSrc);

            XslCompiledTransform myXslDoc = new XslCompiledTransform();
            myXslDoc.Load(strXslFile);

            XmlNodeList rssNodes = myXmlDoc.SelectNodes("rss/channel/item");

            foreach (XmlNode rssNode in rssNodes)
            {
                i++;

                XmlNode rssSubnode = rssNode.SelectSingleNode("title");
                string title = rssSubnode != null ? rssSubnode.InnerText : "";

                rssSubnode = rssNode.SelectSingleNode("link");
                string link = rssSubnode != null ? rssSubnode.InnerText : "";

                if(i==1)
                {
                    HyperLink1.Visible = true;
                    HyperLink1.Text = title;
                    HyperLink1.NavigateUrl = link;
                }

                if (i == 2)
                {
                    HyperLink2.Visible = true;
                    HyperLink2.Text = title;
                    HyperLink2.NavigateUrl = link;
                }

                if (i == 3)
                {
                    HyperLink3.Visible = true;
                    HyperLink3.Text = title;
                    HyperLink3.NavigateUrl = link;
                }

                if (i == 4)
                {
                    HyperLink4.Visible = true;
                    HyperLink4.Text = title;
                    HyperLink4.NavigateUrl = link;
                }

                if (i == 5)
                {
                    HyperLink5.Visible = true;
                    HyperLink5.Text = title;
                    HyperLink5.NavigateUrl = link;
                }

                if (i ==6)
                {
                    HyperLink6.Visible = true;
                    HyperLink6.Text = title;
                    HyperLink6.NavigateUrl = link;
                }

                if (i == 7)
                {
                    HyperLink7.Visible = true;
                    HyperLink7.Text = title;
                    HyperLink7.NavigateUrl = link;
                }
               

            }


            //StringBuilder myStringBuilder = new StringBuilder();
            //StringWriter myStringWriter = new StringWriter(myStringBuilder);

            //myXslDoc.Transform(myXmlDoc, null, myStringWriter);
            ////litModuleMediaRSS.Text = myStringBuilder.ToString();



        }


        public void getCharlas()
        {

            DataLayer oDataLayer = new DataLayer();
            Data_objects.Charlas oCharlas = new Data_objects.Charlas();
            List<Data_objects.Charlas> lstCharlas = new List<Data_objects.Charlas>();
            lstCharlas = oDataLayer.get_Charlas(oCharlas);


            DataCharlas.DataSource = lstCharlas.Take(2);
            DataCharlas.DataBind();

            var oVar = from d in lstCharlas
                       where Convert.ToDateTime(d.vchFecha) > DateTime.Now
                       select d;
            foreach (Data_objects.Charlas oChar in oVar)
            {
                lblDescCharla.Text = oChar.vchDescripcionCorta.ToString();
                //lblFechaCharla.Text = Convert.ToDateTime(oChar.vchFecha).ToString("dd/MM/yyyy");
                //lblHoraCharla.Text = Convert.ToDateTime(oChar.vchFecha).ToString("dd/MM/yyyy");
                lblTitCharla.Text = oChar.vchTitulo;
            }



        }

        public void getCursos()
        {

            DataLayer oDataLayer = new DataLayer();
            Data_objects.Cursos oCursos = new Data_objects.Cursos();
            DataCursos.DataSource = oDataLayer.get_Cursos(oCursos);
            DataCursos.DataBind();


        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string oValor;
            LinkButton obj = (LinkButton)sender;
            oValor = obj.CommandArgument;

            //DetalleCharlas oCharla = new DetalleCharlas(Convert.ToInt16(oValor));
            Response.Redirect("Cursos/DetalleCursos.aspx?idCurso=" + oValor);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string oValor;
            LinkButton obj = (LinkButton)sender;
            oValor = obj.CommandArgument;

            //DetalleCharlas oCharla = new DetalleCharlas(Convert.ToInt16(oValor));
            Response.Redirect("Charlas/DetalleCharlas.aspx?IdCharla=" + oValor);
        }
    }
}