/**
 * Created by fran lopez on 8/12/15.
 */

    /**
     * AngularJS controller for films AngularJS module. This controller should
     * be able to provide you with all the methods needed to perform CRUD
     * operations.
     */

    angular.module('films').controller('FilmsController',
        ['$scope', '$routeParams', '$location', 'FilmsByTitle',
            'FilmsByYear', 'FilmsByGenre', 'FilmsByProtagonist',
            'FilmsByDirector', 'FilmsByRating', 'ListFilmYears',
            'ListFilmGenres', 'ListFilmRatings', 'FilmDetails',
            function ($scope, $routeParams, $location, FilmsByTitle,
                      FilmsByYear, FilmsByGenre, FilmsByProtagonist,
                      FilmsByDirector, FilmsByRating, ListFilmYears,
                      ListFilmGenres, ListFilmRatings, FilmDetails) {

                /**
                 * This method use a HTTP GET request to change the current
                 * path to another.
                 *
                 * The request includes two parameters to find films by a
                 * pattern inserted by user in the view "films/filmsByTitle".
                 * The search will be done by "find()" controller method.
                 *
                 * The first parameter is the search type. In this case is
                 * "filmsByTitle". The second parameter is the pattern
                 * inserted by user.
                 */

                $scope.listFilmsByTitle = function() {

                    var searchType          = 'filmsByTitle';
                    var patternForFilmTitle = this.title;

                    if ((patternForFilmTitle == undefined)
                        || (patternForFilmTitle.length == 0))
                        $scope.error = 'El título de la película no puede' +
                            ' ser nulo';
                    else
                        $location.path('films/listOfFilms/' + searchType + '/'
                            + patternForFilmTitle);
                };

                /**
                 * This method use a HTTP GET request to change the current
                 * path to another.
                 *
                 * The request includes two parameters to find films by a
                 * year inserted by user in the view "films/filmsByYear".
                 * The search will be done by "find()" controller method.
                 *
                 * The first parameter is the search type. In this case is
                 * "filmsByYear". The second parameter is the year inserted
                 * by user.
                 */

                $scope.listFilmsByYear = function() {

                    var searchType  = 'filmsByYear';
                    var filmYear    = this.year;

                    if ((filmYear == undefined) || (filmYear.length == 0))
                        $scope.error = 'El año de estreno de la película no'
                            + ' puede ser nulo';
                    else
                        $location.path('films/listOfFilms/' + searchType + '/'
                            + filmYear);
                };

                /**
                 * This method use a HTTP GET request to change the current
                 * path to another.
                 *
                 * The request includes two parameters to find films by a
                 * genre inserted by user in the view "films/filmsByGenre".
                 * The search will be done by "find()" controller method.
                 *
                 * The first parameter is the search type. In this case is
                 * "filmsByGenre". The second parameter is the genre inserted
                 * by user.
                 */

                $scope.listFilmsByGenre = function() {

                    var searchType  = 'filmsByGenre';
                    var filmGenre    = this.genre;

                    if ((filmGenre == undefined) || (filmGenre.length == 0))
                        $scope.error = 'El género de la película no'
                            + ' puede ser nulo';
                    else
                        $location.path('films/listOfFilms/' + searchType + '/'
                            + filmGenre);
                };

                /**
                 * This method use a HTTP GET request to change the current
                 * path to another.
                 *
                 * The request includes two parameters to find films by a
                 * name of protagonist inserted by user in the view
                 * "films/filmsByProtagonist". The search will be done by
                 * "find()" controller method.
                 *
                 * The first parameter is the search type. In this case is
                 * "filmsByProtagonist". The second parameter is the name of
                 * a protagonist inserted by user.
                 */

                $scope.listFilmsByProtagonist = function() {

                    var searchType      = 'filmsByProtagonist';
                    var filmProtagonist = this.protagonist;

                    if ((filmProtagonist == undefined)
                        || (filmProtagonist.length == 0))
                        $scope.error = 'El nombre del protagonista de la '
                            + 'película no puede ser nulo';
                    else
                        $location.path('films/listOfFilms/' + searchType + '/'
                            + filmProtagonist);
                };

                /**
                 * This method use a HTTP GET request to change the current
                 * path to another.
                 *
                 * The request includes two parameters to find films by a
                 * name of director inserted by user in the view
                 * "films/filmsByDirector". The search will be done by
                 * "find()" controller method.
                 *
                 * The first parameter is the search type. In this case is
                 * "filmsByDirector". The second parameter is the name of
                 * a director inserted by user.
                 */

                $scope.listFilmsByDirector = function() {

                    var searchType      = 'filmsByDirector';
                    var filmDirector    = this.director;

                    if ((filmDirector == undefined)
                        || (filmDirector.length == 0))
                        $scope.error = 'El nombre del director de la '
                            + 'película no puede ser nulo';
                    else
                        $location.path('films/listOfFilms/' + searchType + '/'
                            + filmDirector);
                };

                /**
                 * This method use a HTTP GET request to change the current
                 * path to another.
                 *
                 * The request includes two parameters to find films by a
                 * rating inserted by user in the view "films/filmsByRating".
                 * The search will be done by "find()" controller method.
                 *
                 * The first parameter is the search type. In this case is
                 * "filmsByRating". The second parameter is the rating inserted
                 * by user.
                 */

                $scope.listFilmsByRating = function() {

                    var searchType = 'filmsByRating';
                    var filmRating = this.rating;

                    if ((filmRating == undefined) || (filmRating.length == 0))
                        $scope.error = 'La valoración de la '
                            + 'película no puede ser un valor nulo';
                    else
                        $location.path('films/listOfFilms/' + searchType + '/'
                            + filmRating);
                };

                /**
                 * This method will retrieve a list of films based on the
                 * "searchType" route parameter and "patternToSearch" route
                 * parameter, which the function obtains directly from the URL.
                 *
                 * For every value of "searchType" route parameter, this method
                 * does a different request to the server. Every request are
                 * done by AngularJS service. For every request to the server
                 * is necessary a different AngularJS service.
                 *
                 * "patternToSearch" route parameter stores information of
                 * pattern to use in the search.
                 *
                 * This method use a HTTP GET request to execute the operation.
                 */

                $scope.find = function() {

                    $scope.searchType = $routeParams.searchType;

                    switch ($routeParams.searchType)
                    {
                        case 'filmsByTitle':
                            $scope.films = FilmsByTitle.query({
                                patternForFilmTitle: $routeParams.patternToSearch
                            });
                            break;
                        case 'filmsByYear':
                            $scope.films = FilmsByYear.query({
                                filmYear: $routeParams.patternToSearch
                            });
                            break;
                        case 'filmsByGenre':
                            $scope.films = FilmsByGenre.query({
                                filmGenre: $routeParams.patternToSearch
                            });
                            break;
                        case 'filmsByProtagonist':
                            $scope.films = FilmsByProtagonist.query({
                                patternForFilmProtagonist: $routeParams.patternToSearch
                            });
                            break;
                        case 'filmsByDirector':
                            $scope.films = FilmsByDirector.query({
                                patternForFilmDirector: $routeParams.patternToSearch
                            });
                            break;
                        case 'filmsByRating':
                            $scope.films = FilmsByRating.query({
                                filmRating: $routeParams.patternToSearch
                            });
                            break;
                    }
                };

                /**
                 * This method will retrieve a list of premiere dates of all
                 * films in the system.
                 *
                 * This method use a HTTP GET request to execute the
                 * operation.
                 */

                $scope.getYears = function() {

                    $scope.years = ListFilmYears.query({});
                };

                /**
                 * This method will retrieve a list of genres of all
                 * films in the system.
                 *
                 * This method use a HTTP GET request to execute the
                 * operation.
                 */

                $scope.getGenres = function() {

                    $scope.genres = ListFilmGenres.query({});
                };

                /**
                 * This method will retrieve a list of ratings of all
                 * films in the system.
                 *
                 * This method use a HTTP GET request to execute the
                 * operation.
                 */

                $scope.getRatings = function() {

                    $scope.ratings = ListFilmRatings.query({});
                };

                /**
                 * This method will retrieve information of a single film,
                 * stored in the system.
                 *
                 * This method use a HTTP GET request to execute the
                 * operation.
                 */

                $scope.getFilmDetails = function() {

                    $scope.filmId   = $routeParams.filmId;
                    $scope.film     = FilmDetails.get({
                        filmId: $routeParams.filmId
                    });
                };
            }
        ]
    );