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

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

    angular.module('books').controller('BooksController',
        ['$scope', '$routeParams', '$location', 'BooksByTitle',
            'BooksByYear', 'BooksByGenre', 'BooksByProtagonist',
            'BooksByWriter', 'BooksByRating', 'ListBookYears',
            'ListBookGenres', 'ListBookRatings', 'BookDetails',
            function ($scope, $routeParams, $location, BooksByTitle,
                      BooksByYear, BooksByGenre, BooksByProtagonist,
                      BooksByWriter, BooksByRating, ListBookYears,
                      ListBookGenres, ListBookRatings, BookDetails) {

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

                $scope.listBooksByTitle = function() {

                    var searchType          = 'booksByTitle';
                    var patternForBookTitle = this.title;

                    if ((patternForBookTitle == undefined)
                        || (patternForBookTitle.length == 0))
                        $scope.error = 'El título del libro no puede' +
                            ' ser nulo';
                    else
                        $location.path('books/listOfBooks/' + searchType + '/'
                            + patternForBookTitle);
                };

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

                $scope.listBooksByYear = function() {

                    var searchType  = 'booksByYear';
                    var bookYear    = this.publicationDate;

                    if ((bookYear == undefined) || (bookYear.length == 0))
                        $scope.error = 'La fecha de publicación del libro no'
                            + ' puede ser nula';
                    else
                        $location.path('books/listOfBooks/' + searchType + '/'
                            + bookYear);
                };

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

                $scope.listBooksByGenre = function() {

                    var searchType  = 'booksByGenre';
                    var bookGenre   = this.genre;

                    if ((bookGenre == undefined) || (bookGenre.length == 0))
                        $scope.error = 'El género del libro no'
                            + ' puede ser nulo';
                    else
                        $location.path('books/listOfBooks/' + searchType + '/'
                            + bookGenre);
                };

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

                $scope.listBooksByProtagonist = function() {

                    var searchType      = 'booksByProtagonist';
                    var bookProtagonist = this.protagonist;

                    if ((bookProtagonist == undefined)
                        || (bookProtagonist.length == 0))
                        $scope.error = 'El nombre del protagonista del '
                            + 'libro no puede ser nulo';
                    else
                        $location.path('books/listOfBooks/' + searchType + '/'
                            + bookProtagonist);
                };

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

                $scope.listBooksByWriter = function() {

                    var searchType = 'booksByWriter';
                    var bookWriter = this.writer;

                    if ((bookWriter == undefined)
                        || (bookWriter.length == 0))
                        $scope.error = 'El nombre del escritor del '
                            + 'libro no puede ser nulo';
                    else
                        $location.path('books/listOfBooks/' + searchType + '/'
                            + bookWriter);
                };

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

                $scope.listBooksByRating = function() {

                    var searchType = 'booksByRating';
                    var bookRating = this.rating;

                    if ((bookRating == undefined) || (bookRating.length == 0))
                        $scope.error = 'La valoración del '
                            + 'libro no puede ser un valor nulo';
                    else
                        $location.path('books/listOfBooks/' + searchType + '/'
                            + bookRating);
                };

                /**
                 * This method will retrieve a list of books 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 'booksByTitle':
                            $scope.books = BooksByTitle.query({
                                patternForBookTitle: $routeParams.patternToSearch
                            });
                            break;
                        case 'booksByYear':
                            $scope.books = BooksByYear.query({
                                bookYear: $routeParams.patternToSearch
                            });
                            break;
                        case 'booksByGenre':
                            $scope.books = BooksByGenre.query({
                                bookGenre: $routeParams.patternToSearch
                            });
                            break;
                        case 'booksByProtagonist':
                            $scope.books = BooksByProtagonist.query({
                                patternForBookProtagonist: $routeParams.patternToSearch
                            });
                            break;
                        case 'booksByWriter':
                            $scope.books = BooksByWriter.query({
                                patternForBookWriter: $routeParams.patternToSearch
                            });
                            break;
                        case 'booksByRating':
                            $scope.books = BooksByRating.query({
                                bookRating: $routeParams.patternToSearch
                            });
                            break;
                    }
                };

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

                $scope.getYears = function() {

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

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

                $scope.getGenres = function() {

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

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

                $scope.getRatings = function() {

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

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

                $scope.getBookDetails = function() {

                    $scope.bookId   = $routeParams.bookId;
                    $scope.book     = BookDetails.get({
                        bookId: $routeParams.bookId
                    });
                };
            }
        ]
    );