﻿namespace ProcessJsonToHtml
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web.UI;
    using Newtonsoft.Json;
    using System.Xml.Linq;
    using System.Web;
    using Newtonsoft.Json.Linq;

    class ProcessJsonToHtml
    {
        static void Main()
        {
            string rssLocation = "https://www.youtube.com/feeds/videos.xml?channel_id=UCLC-vbm7OWvpbqzXaoAMGGw";
            string fileLocation = "../../telerik.xml";
            string jsonFileLocation = "../../telerik.json";

            Console.WriteLine("(Problem I and II)");
            Console.WriteLine("Downloading Telerik Academy Youtube RSS feed...");
            XDocument document;
            
            using (WebClient web = new WebClient())
            {
                web.DownloadFile(rssLocation, fileLocation);
                document = XDocument.Load(fileLocation);
                Console.WriteLine("Feed downloaded.");
                Console.WriteLine(new string('-', 70));
            }

            Console.WriteLine("(Problem III)");
            Console.WriteLine("Parsing XML to JSON...");
            string jsonFromXml = JsonConvert.SerializeXNode(document, Formatting.Indented);

            System.IO.File.WriteAllText(jsonFileLocation, jsonFromXml);
            var jsonObject = JObject.Parse(jsonFromXml);
            Console.WriteLine("Convertion to JSON complete.");
            Console.WriteLine(new string('-', 70));

            Console.WriteLine("(Problem IV)");
            Console.WriteLine("Print all video titles to console.");
            var videos = jsonObject["feed"]["entry"];
            var titles = videos.Select(v => v["title"]);
            var videoObjects = new List<Video>();

            foreach (var title in titles)
            {
                Console.WriteLine(title);
            }

            Console.WriteLine(new string('-', 70));

            Console.WriteLine("Problem V");
            Console.WriteLine("Parsing JSON to POCO Objects...");
            foreach (var video in videos)
            {
                videoObjects.Add(JsonConvert.DeserializeObject<Video>(video.ToString()));
            }
            Console.WriteLine("Parsing completed.");
            Console.WriteLine(new string('-', 70));

            Console.WriteLine("Problem VI");
            Console.WriteLine("Creating HTML from the POCO objects...");

            StringWriter stringWriter = new StringWriter();
            using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter, "   "))
            {
                // Head
                writer.RenderBeginTag(HtmlTextWriterTag.Head);
                    // Title
                    writer.RenderBeginTag(HtmlTextWriterTag.Title);
                    writer.Write("Telerik Videos");
                    writer.RenderEndTag();

                    // Link to stylesheet
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "styles.css");
                    writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
                    writer.RenderBeginTag(HtmlTextWriterTag.Link);
                    writer.RenderEndTag();

                writer.RenderEndTag();

                // Meta for the encoding
                writer.AddAttribute(HtmlTextWriterAttribute.Content, "text/html; charset=UTF-8");
                writer.AddAttribute("http-equiv", "Content-Type");
                writer.RenderBeginTag(HtmlTextWriterTag.Meta);
                writer.RenderEndTag();
                writer.WriteLine();

                foreach (var video in videoObjects)
                {
                    // wrapper
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "video-wrapper");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                        // header
                        writer.RenderBeginTag(HtmlTextWriterTag.H2);
                        writer.Write(video.Title);
                        writer.RenderEndTag();
                        writer.WriteLine();

                        // Iframe
                        writer.AddAttribute(HtmlTextWriterAttribute.Width, "400");
                        writer.AddAttribute(HtmlTextWriterAttribute.Height, "300");
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, string.Format("https://www.youtube.com/embed/{0}", video.Id));
                        writer.AddAttribute("frameborder", "0");
                        writer.AddAttribute("allowfullscreen", "1");
                        writer.RenderBeginTag(HtmlTextWriterTag.Iframe);
                        writer.RenderEndTag();
                        writer.WriteLine();

                        // Link to YouTube
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "link-youtube");
                        writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("https://youtu.be/{0}", video.Id));
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write("Watch on YouTube");
                        writer.RenderEndTag();
                        writer.WriteLine();

                        // Date
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "date");
                        writer.RenderBeginTag(HtmlTextWriterTag.P);
                        var dateTime = DateTime.Parse(video.DateOfPublishing);
                        writer.Write(string.Format("Video published on {0} of {1} {2}", dateTime.Day, dateTime.ToString("MMMM"), dateTime.Year));
                        writer.RenderEndTag();

                    writer.RenderEndTag();
                    writer.WriteLine();
                }
            }

            File.WriteAllText("../../index.html", stringWriter.ToString());
            Console.WriteLine("index.html created see directory for results");
        }
    }
}