/// <summary>
        /// Generates the internal JSON representation to be used internally in EL COLOMBIANO web pages. The topic is the movies category.
        /// </summary>
        /// <param name="sender">Sender object which fired the event</param>
        /// <param name="e">Parameters sent from the event manager.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            WebClient client = new WebClient();
            string url = "https://www.planepoly.com:8181/PlanepolyCoreWeb/OSConcierge?lat=6.210506&long=-75.57096&idTipos=201&consulta=eventos&bypass=626262";
            Stream data = client.OpenRead(url);
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            data.Close();
            reader.Close();
            Response.AddHeader("Access-Control-Allow-Origin", "*");

            var movieList = JsonConvert.DeserializeObject<Movie>(s);
            var movieServiceList = movieList.servicios;
            foreach (var it in movieServiceList)
            {
                it.nombre = it.nombre.Trim();
            }
            var movieListOrdered = movieServiceList.OrderByDescending(x => x.estr).OrderBy(x => x.nombre).ToList();
            movieList.servicios = movieListOrdered;

            List<string> theaterNameList = new List<string>();
            movieList.servicios.ForEach(x => x.ptos.Select(y => y.nombre).ToList().ForEach(z => theaterNameList.Add(z)));
            var theatersList = theaterNameList.Distinct().OrderBy(y => y).ToList();
            var genresList = (from service in movieServiceList
                              orderby service.genero
                              select service.genero).Distinct().ToList<String>();
            var allMovieNameList = (from service in movieServiceList
                                 orderby service.nombre
                                 select service.nombre).Distinct().ToList<string>();
            MovieCatalog movieCatalog = new MovieCatalog();
            movieCatalog.theaters = theatersList;
            movieCatalog.genres = genresList;
            movieCatalog.movies = allMovieNameList;

            Dictionary<string, List<string>> theaterMoviesList = new Dictionary<string, List<string>>();
            foreach (var item in movieCatalog.theaters)
            {
                List<string> movieNameList = new List<string>();
                theaterMoviesList.Add(item, movieNameList);
            }

            // Creates now a list of all movies mapped to our structure, given
            // the Planepoly JSON structure.
            List<MovieLookup> movieLookupList = new List<MovieLookup>();
            foreach (var service in movieList.servicios)
            {
                MovieLookup movieLookup = new MovieLookup();
                movieLookup.name = service.nombre;
                movieLookup.img = service.img;
                movieLookup.url = service.url;
                movieLookup.premiere = service.estr;
                movieLookup.genre = service.genero;
                movieLookup.locations = new List<MovieLookupLocation>();
                foreach (var location in service.ptos)
                {
                    if (theaterMoviesList.ContainsKey(location.nombre))
                    {
                        var tempList = theaterMoviesList[location.nombre];
                        tempList.Add(service.nombre);
                    }

                    MovieLookupLocation mlLocation = new MovieLookupLocation();
                    mlLocation.name = location.nombre;
                    mlLocation.address = location.direccion;


                    List<MovieLookupShow> mlShowList = new List<MovieLookupShow>();
                    Dictionary<int, string> shows = new Dictionary<int, string>();
                    shows.Add(0, "");
                    shows.Add(1, "");
                    shows.Add(2, "");
                    shows.Add(3, "");
                    shows.Add(4, "");
                    shows.Add(5, "");
                    shows.Add(6, "");
                    shows.Add(7, "");
                    foreach (var show in location.funcs)
                    {
                        if (shows.ContainsKey(show.dia))
                        {
                            var valDay = shows[show.dia];
                            valDay += show.hora + " ";
                            shows[show.dia] = valDay;
                        }
                    }
                    foreach (var it in shows)
                    {
                        if (it.Value.Trim() != "")
                        {
                            MovieLookupShow mls = new MovieLookupShow();
                            mls.frequency = it.Key;
                            mls.hours = it.Value.Trim();
                            switch (mls.frequency)
                            {
                                case 0:
                                    mls.name = "Diario";
                                    break;
                                case 1:
                                    mls.name = "Lunes";
                                    break;
                                case 2:
                                    mls.name = "Martes";
                                    break;
                                case 3:
                                    mls.name = "Miércoles";
                                    break;
                                case 4:
                                    mls.name = "Jueves";
                                    break;
                                case 5:
                                    mls.name = "Viernes";
                                    break;
                                case 6:
                                    mls.name = "Sábado";
                                    break;
                                case 7:
                                    mls.name = "Domingo";
                                    break;
                                default:
                                    break;
                            }
                            mlShowList.Add(mls);
                        }
                    }
                    mlLocation.schedule = mlShowList;
                    movieLookup.locations.Add(mlLocation);

                }
                movieLookupList.Add(movieLookup);
            }
            foreach (var it in theaterMoviesList)
            {
                it.Value.Sort();
            }
            movieCatalog.theaterMovies = theaterMoviesList;

            var movieLookupListOrdered = (from item in movieLookupList
                                          orderby item.premiere descending, item.name
                                          select item).ToList();
            string movieCatalogJSON = JsonConvert.SerializeObject(movieCatalog);
            string movieLookupJSON = JsonConvert.SerializeObject(movieLookupListOrdered);
            
            // Now that we have just  gathered all the information, create static JSON versions
            // Now there are two files to consume the feed

            // Full movie (mapped from origin).
            string fileName = @"D:\SitiosWeb\Sitio\EC100A_Servicios\EC100A_PlanepolyWidget\planepoly-movies.json";
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(movieLookupJSON);
            }

            // Full movie catalog (mapped from origin)
            fileName = @"D:\SitiosWeb\Sitio\EC100A_Servicios\EC100A_PlanepolyWidget\planepoly-movies-catalog.json";
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(movieCatalogJSON);
            }

            // This is the page result.
            Response.Write(movieLookupJSON);
        }
        /// <summary>
        /// It makes a search for movies that meets the search criteria.
        /// Receives three parameters.
        /// t=Theater: Holds the theater to look for.
        /// m=Movie: Holds the movie to look for.
        /// g=Genre: Holds the genre to look for.
        /// 
        /// NOTE: Consumes the own EL COLOMBIANO JSON Format.
        /// </summary>
        /// <param name="sender">Sender object which fired the event</param>
        /// <param name="e">Parameters sent from the event manager.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string movieSearchListJSON = "";
            string fileNameMovies = @"D:\SitiosWeb\Sitio\EC100A_Servicios\EC100A_PlanepolyWidget\planepoly-movies.json";
            string t = Request.QueryString["t"];
            string m = Request.QueryString["m"];
            string g = Request.QueryString["g"];
            string s;
            using (StreamReader reader = new StreamReader(fileNameMovies))
            {
                s = reader.ReadToEnd();
            }

            if ((t == null && m == null & g == null) || (t == "-1" && m == "-1" && g == "-1"))
            {
                // Returns all movies available.
                // All movies are expanded in order to give to each movie all of its locations.
                List<MovieLookup> movieLookupList = JsonConvert.DeserializeObject<List<MovieLookup>>(s);
                List<MovieLookup> movieLookupAllList = new List<MovieLookup>();

                foreach (var movieInfo in movieLookupList)
                {
                    MovieLookup ml = new MovieLookup();
                    ml.name = movieInfo.name;
                    ml.img = movieInfo.img;
                    ml.url = movieInfo.url;
                    ml.premiere = movieInfo.premiere;
                    ml.genre = movieInfo.genre;
                    movieLookupAllList.Add(ml);
                }
                movieLookupAllList = (from item in movieLookupAllList
                                      orderby item.premiere descending, item.name
                                      select item).Distinct().ToList();
                string allMoviesJSON = JsonConvert.SerializeObject(movieLookupAllList);
                movieSearchListJSON = allMoviesJSON;
            }
            else
            {
                List<MovieLookup> movieSearchList = new List<MovieLookup>();
                List<MovieLookup> movieLookupList = JsonConvert.DeserializeObject<List<MovieLookup>>(s);
                List<MovieLookup> movieByGenre = null;
                List<MovieLookup> movieByMovie = null;
                List<MovieLookup> movieByTheater = null;
                List<MovieLookup> movieByTheaterMovie = null;

                if (t == "-1" && m == "-1" && g != "-1")
                {
                    // Search only by genre
                    var movieGenreList = (from item in movieLookupList
                                    where item.genre == g
                                    select item).ToList();

                    // Now we have all movies by genre, now it is needed to expand by location.
                    movieByGenre = new List<MovieLookup>();
                    foreach (var ml in movieGenreList)
                    {
                        // Sort locations
                        var sortedLocations = (from l in ml.locations
                                               orderby l.name
                                               select l).ToList();
                        foreach (var l in sortedLocations)
                        {
                            MovieLookup movieLookupInfo = new MovieLookup();
                            movieLookupInfo.name = ml.name;
                            movieLookupInfo.img = ml.img;
                            movieLookupInfo.url = ml.url;
                            movieLookupInfo.premiere = ml.premiere;
                            movieLookupInfo.genre = ml.genre;

                            List<MovieLookupLocation> foundMovieLocationList = new List<MovieLookupLocation>();
                            MovieLookupLocation loc = new MovieLookupLocation();
                            loc.name = l.name;
                            loc.address = l.address;
                            loc.schedule = l.schedule;
                            foundMovieLocationList.Add(loc);
                            movieLookupInfo.locations = foundMovieLocationList;

                            movieByGenre.Add(movieLookupInfo);                            
                        }
                    }
                    movieByGenre = (from it in movieByGenre
                                    orderby it.premiere descending, it.name
                                    select it).Distinct().ToList();
                }
                else
                {
                    if (t != "-1" && m != "-1")
                    {
                        // Search both theater and movie in that theater                        
                        movieByTheaterMovie = new List<MovieLookup>();
                        foreach (var ml in movieLookupList)
                        {
                            var sortedLocations = (from loc in ml.locations
                                                   orderby loc.name
                                                   select loc).Distinct().ToList();
                            foreach (var l in sortedLocations)
                            {
                                if (l.name == t && ml.name == m)
                                {
                                    MovieLookup foundMovie = new MovieLookup();
                                    foundMovie.name = ml.name;
                                    foundMovie.img = ml.img;
                                    foundMovie.url = ml.url;
                                    foundMovie.premiere = ml.premiere;
                                    foundMovie.genre = ml.genre;

                                    List<MovieLookupLocation> foundMovieLocationList = new List<MovieLookupLocation>();
                                    foundMovieLocationList.Add(l);
                                    foundMovie.locations = foundMovieLocationList;
                                    movieByTheaterMovie.Add(foundMovie);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (t == "-1" && m != "-1")
                        {
                            // Search only by movie name (this gives many movies in different locations --aka theaters).
                            movieByMovie = new List<MovieLookup>();
                            var existingMoviesList = (from item in movieLookupList
                                                      where item.name == m
                                                      select item).ToList();

                            // The existing movie must be expanded one item by all of its locations.
                            foreach (var ml in existingMoviesList)
                            {
                                var sortedLocations = (from loc in ml.locations
                                                       orderby loc.name
                                                       select loc).Distinct().ToList();
                                foreach (var l in sortedLocations)
                                {
                                    MovieLookup expando = new MovieLookup();
                                    expando.name = ml.name;
                                    expando.img = ml.img;
                                    expando.url = ml.url;
                                    expando.premiere = ml.premiere;
                                    expando.genre = ml.genre;

                                    List<MovieLookupLocation> expandoLocationList = new List<MovieLookupLocation>();
                                    expandoLocationList.Add(l);
                                    expando.locations = expandoLocationList;
                                    movieByMovie.Add(expando);
                                }
                            }
                            movieByMovie = (from ml in movieByMovie
                                            orderby ml.premiere descending, ml.name
                                            select ml).Distinct().ToList();
                        }
                        else
                        {
                            // Search only by theater
                            movieByTheater = new List<MovieLookup>();
                            foreach (var ml in movieLookupList)
                            {
                                var sortedLocations = (from loc in ml.locations
                                                       orderby loc.name
                                                       select loc).Distinct().ToList();
                                foreach (var l in sortedLocations)
                                {
                                    if (l.name == t)
                                    {
                                        MovieLookup foundMovie = new MovieLookup();
                                        foundMovie.name = ml.name;
                                        foundMovie.img = ml.img;
                                        foundMovie.url = ml.url;
                                        foundMovie.premiere = ml.premiere;
                                        foundMovie.genre = ml.genre;

                                        List<MovieLookupLocation> foundMovieLocationList = new List<MovieLookupLocation>();
                                        foundMovieLocationList.Add(l);
                                        foundMovie.locations = foundMovieLocationList;
                                        movieByTheater.Add(foundMovie);
                                    }
                                }
                            }
                        }
                    }
                }
                if (movieByGenre != null)
                {
                    movieSearchList.AddRange(movieByGenre);
                }
                if (movieByTheater != null)
                {
                    movieSearchList.AddRange(movieByTheater);
                }
                if (movieByTheaterMovie != null)
                {
                    movieSearchList.AddRange(movieByTheaterMovie);
                }
                if (movieByMovie != null)
                {
                    movieSearchList.AddRange(movieByMovie);
                }
                movieSearchList = (from item in movieSearchList
                                   orderby item.premiere descending, item.name
                                   select item).Distinct().ToList();
                movieSearchListJSON = JsonConvert.SerializeObject(movieSearchList);
            }

            Response.Write(movieSearchListJSON);
            Response.AddHeader("Access-Control-Allow-Origin", "*");
        }