private void MovieVMToMovieNavigationProps(MovieVM model, Movie movie)
        {
            HashSet<Actor> actors = new HashSet<Actor>();
            if (model.Actors != null)
            {
                foreach (var act in model.Actors)
                {
                    var currentActor = db.Actors.All().FirstOrDefault(a => a.Id == act.Id);
                    if (currentActor != null)
                    {
                        actors.Add(currentActor);
                    }
                }
            }

            movie.Actors = actors;
            if (model.Category != null)
            {
                var currentCategory = db.Categories.All().FirstOrDefault(c => c.Id == model.Category.Id);
                if (currentCategory != null)
                {
                    movie.Category = currentCategory;
                }
                else
                {
                    movie.Category = db.Categories.All().First();
                }
            }
            else
            {
                movie.Category = db.Categories.All().First();
            }

            if (model.Country != null)
            {
                var currentCountry = db.Country.All().FirstOrDefault(c => c.Id == model.Country.Id);
                if (currentCountry != null)
                {
                    movie.Country = currentCountry;
                }
                else
                {
                    movie.Country = db.Country.All().First();
                }
            }
            else
            {
                movie.Country = db.Country.All().First();
            }

            if (model.Director != null)
            {
                var currentDirector = db.Directors.All().FirstOrDefault(c => c.Id == model.Director.Id);
                if (currentDirector != null)
                {
                    movie.Director = currentDirector;
                }
                else
                {
                    movie.Director = db.Directors.All().First();
                }
            }
            else
            {
                movie.Director = db.Directors.All().First();
            }
        }
        public ActionResult GetMovie(string movieTitle)
        {
            IMDb imdb = new IMDb(movieTitle, true);

            var movieDate = int.Parse(imdb.Year);
            if (db.Movies.All().Any(m => m.Title == movieTitle && m.Year == movieDate))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Movie already exists");
            }

            var movie = new Movie()
            {
                Title = imdb.Title,
                Year = int.Parse(imdb.Year),
                Duration = int.Parse(imdb.Runtime),
                Description = imdb.Storyline,
                ParentsGuide = 0,
                Poster = GetPoster(imdb.PosterFull),
            };
            // actors
            movie.Actors = new List<Actor>();
            foreach (var actor in imdb.Cast)
            {
                var actorName = actor.ToString().Split(' ');
                if (actorName.Length > 1)
                {
                    var currActorFirst = actorName[0];
                    var currActorLast = actorName[1];

                    if (currActorFirst != null && currActorLast != null
                        && db.Actors.All().All(a => !a.FirstName.Contains(currActorFirst)
                        && !a.LastName.Contains(currActorLast)))
                    {
                        movie.Actors.Add(new Actor()
                        {
                            FirstName = currActorFirst,
                            LastName = currActorLast,
                        });
                    }
                }
            }
            // directors
            var directorName = imdb.Directors[0].ToString().Split(' ');
            if (directorName.Length > 1)
            {
                var currDirectorFirst = directorName[0];
                var currDirectorLast = directorName[0];
                if (currDirectorFirst != null && currDirectorLast != null
                    && db.Directors.All().All(d => !d.FirstName.Contains(currDirectorFirst)
                    && !d.LastName.Contains(currDirectorLast)))
                {
                    movie.Director = imdb.Directors.ToArray().Select(d => new Director()
                    {
                        FirstName = d.ToString().Split(' ')[0],
                        LastName = d.ToString().Split(' ')[1],
                    }).FirstOrDefault();
                }
            }
            // categories
            var currGenre = imdb.Genres[0].ToString();
            var imdbCategories = imdb.Genres.ToArray().Select(i => new Category()
                                 {
                                     Name = i.ToString()
                                 }).ToList();
            var matchingCategories = db.Categories.All().ToList()
                .Where(c => imdbCategories.Any(cc => cc.Name == c.Name)).ToList();

            if (matchingCategories.Count() > 0)
            {
                movie.Category = matchingCategories.FirstOrDefault();
            }
            else
            {
                movie.Category = new Category()
                {
                    Name = currGenre
                };
            }
            // countries
            var currCountry = imdb.Countries[0].ToString();
            var imdbCountries = imdb.Countries.ToArray().Select(i => new Country()
                {
                    Name = i.ToString()
                }).ToList();
            var matchingCountries = db.Country.All().ToList()
                .Where(c => imdbCountries.Any(cc => cc.Name == c.Name)).ToList();

            if (matchingCountries.Count() > 0)
            {
                movie.Country = matchingCountries.FirstOrDefault();
            }
            else
            {
                movie.Country = new Country()
                {
                    Name = currCountry
                };
            }

            db.Movies.Add(movie);
            db.SaveChanges();

            var movies = db.Movies.All().Select(MovieVM.FromMovie);

            return View("Index", movies.ToList());
        }
        public ActionResult Create([DataSourceRequest]DataSourceRequest request, MovieVM model)
        {
            if (ModelState.IsValid)
            {
                Movie movie = new Movie();
                movie = MovieVM.ToMovieSimple(movie, model);

                MovieVMToMovieNavigationProps(model, movie);

                model = new MovieVM()
                {
                    Id = movie.Id,
                    Title = movie.Title,
                    Category = new CategoryVM()
                    {
                        Id = movie.Category.Id,
                        Name = movie.Category.Name
                    },
                    CategoryName = movie.Category.Name,
                    DirectorName = movie.Director.FirstName + " " + movie.Director.LastName,
                    Director = new DirectorVM()
                    {
                        Id = movie.Director.Id,
                        Name = movie.Director.FirstName + " " + movie.Director.LastName,
                    },
                    Actors = movie.Actors.Select(a => new ActorVM()
                    {
                        Id = a.Id,
                        Name = a.FirstName + " " + a.LastName,
                    }),
                    Country = new CountryVM()
                    {
                        Id = movie.Country.Id,
                        Name = movie.Country.Name,
                    },
                    CountryName = movie.Country.Name,
                    ParentsGuideId = (int)(movie.ParentsGuide),
                    Poster = movie.Poster,
                    Year = movie.Year,
                    Duration = movie.Duration,
                    Description = movie.Description,
                };

                model.ParentsGuideName = Enum.GetName(typeof(ParentsGuide), model.ParentsGuideId);

                try
                {
                    db.Movies.Add(movie);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("DatabaseException",
                       "Database error while updating the entries. Try updating again." + ex.Message);
                }
            }

            return Json((new[] { model }.ToDataSourceResult(request, ModelState)), JsonRequestBehavior.AllowGet);
        }