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

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

    angular.module('tvSeries').controller('TVSeriesController',
        ['$scope', '$routeParams', '$location', 'TVSeriesByTitle', 
            'TVSeriesByYear', 'TVSeriesByGenre',
            'TVSeriesByProtagonist', 'TVSeriesByDirector',
            'TVSeriesByRating', 'ListTVSerieYears',
            'ListTVSerieGenres', 'ListTVSerieRatings', 'TVSerieDetails',
            function ($scope, $routeParams, $location, TVSeriesByTitle,
                      TVSeriesByYear, TVSeriesByGenre,
                      TVSeriesByProtagonist,
                      TVSeriesByDirector, TVSeriesByRating,
                      ListTVSerieYears, ListTVSerieGenres,
                      ListTVSerieRatings, TVSerieDetails) {

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

                $scope.listTVSeriesByTitle = function() {

                    var searchType              = 'tvSeriesByTitle';
                    var patternForTVSerieTitle  = this.title;

                    if ((patternForTVSerieTitle == undefined)
                        || (patternForTVSerieTitle.length == 0))
                        $scope.error = 'El título de la serie no puede' +
                            ' ser nulo';
                    else
                        $location.path('tvSeries/listOfTVSeries/'
                            + searchType + '/'
                            + patternForTVSerieTitle);
                };

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

                $scope.listTVSeriesByYear = function() {

                    var searchType  = 'tvSeriesByYear';
                    var tvSerieYear = this.year;

                    if ((tvSerieYear == undefined) || (tvSerieYear.length == 0))
                        $scope.error = 'El año de estreno de la serie no'
                            + ' puede ser nulo';
                    else
                        $location.path('tvSeries/listOfTVSeries/'
                            + searchType + '/'
                            + tvSerieYear);
                };

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

                $scope.listTVSeriesByGenre = function() {

                    var searchType      = 'tvSeriesByGenre';
                    var tvSerieGenre    = this.genre;

                    if ((tvSerieGenre == undefined) || (tvSerieGenre.length == 0))
                        $scope.error = 'El género de la serie no'
                            + ' puede ser nulo';
                    else
                        $location.path('tvSeries/listOfTVSeries/'
                            + searchType + '/'
                            + tvSerieGenre);
                };

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

                $scope.listTVSeriesByProtagonist = function() {

                    var searchType          = 'tvSeriesByProtagonist';
                    var tvSerieProtagonist  = this.protagonist;

                    if ((tvSerieProtagonist == undefined)
                        || (tvSerieProtagonist.length == 0))
                        $scope.error = 'El nombre del protagonista de la '
                            + 'serie no puede ser nulo';
                    else
                        $location.path('tvSeries/listOfTVSeries/'
                            + searchType + '/'
                            + tvSerieProtagonist);
                };

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

                $scope.listTVSeriesByDirector = function() {

                    var searchType      = 'tvSeriesByDirector';
                    var tvSerieDirector = this.director;

                    if ((tvSerieDirector == undefined)
                        || (tvSerieDirector.length == 0))
                        $scope.error = 'El nombre del director de la '
                            + 'serie no puede ser nulo';
                    else
                        $location.path('tvSeries/listOfTVSeries/'
                            + searchType + '/'
                            + tvSerieDirector);
                };

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

                $scope.listTVSeriesByRating = function() {

                    var searchType      = 'tvSeriesByRating';
                    var tvSerieRating   = this.rating;

                    if ((tvSerieRating == undefined) || (tvSerieRating.length == 0))
                        $scope.error = 'La valoración de la '
                            + 'serie no puede ser un valor nulo';
                    else
                        $location.path('tvSeries/listOfTVSeries/'
                            + searchType + '/'
                            + tvSerieRating);
                };

                /**
                 * This method will retrieve a list of tv series 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 'tvSeriesByTitle':
                            $scope.tvSeries = TVSeriesByTitle.query({
                                patternForTVSerieTitle: $routeParams.patternToSearch
                            });
                            break;
                        case 'tvSeriesByYear':
                            $scope.tvSeries = TVSeriesByYear.query({
                                tvSerieYear: $routeParams.patternToSearch
                            });
                            break;
                        case 'tvSeriesByGenre':
                            $scope.tvSeries = TVSeriesByGenre.query({
                                tvSerieGenre: $routeParams.patternToSearch
                            });
                            break;
                        case 'tvSeriesByProtagonist':
                            $scope.tvSeries = TVSeriesByProtagonist.query({
                                patternForTVSerieProtagonist: $routeParams.patternToSearch
                            });
                            break;
                        case 'tvSeriesByDirector':
                            $scope.tvSeries = TVSeriesByDirector.query({
                                patternForTVSerieDirector: $routeParams.patternToSearch
                            });
                            break;
                        case 'tvSeriesByRating':
                            $scope.tvSeries = TVSeriesByRating.query({
                                tvSerieRating: $routeParams.patternToSearch
                            });
                            break;
                    }
                };

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

                $scope.getYears = function() {

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

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

                $scope.getGenres = function() {

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

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

                $scope.getRatings = function() {

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

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

                $scope.getTVSerieDetails = function() {

                    $scope.TVSerieId   = $routeParams.TVSerieId;
                    $scope.tvSerie     = TVSerieDetails.get({
                        TVSerieId: $routeParams.TVSerieId
                    });
                };
            }
        ]
    );