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

    /**
     * CommonJS module to configure Grunt task runner
     */

    /**
     * The grunt configuration file uses a single module function to inject the
     * grunt object. Then, the "grunt.initConfig()" method is used to configure your
     * third-party tasks.
     *
     * Notice how you configured the "grunt-env" task in the configuration object,
     * where you basically created two environment variables sets: one for testing
     * and the other for development.
     *
     * Next, you used the "grunt.loadNpmTasks()" method to load the grunt-env module.
     * Be aware that you will need to call this method for any new third-party task
     * you add to the project.
     *
     * Finally, you created a default grunt task using the "grunt.registerTask()"
     * method. Notice how the "grunt.registerTask()" method accepts two arguments:
     * the first one sets the task name and the second argument is a collection of
     * other grunt tasks that will be executed when the parent task is used.
     *
     * This is a common pattern of grouping different tasks together to easily
     * automate several operations. In this case, the default task will only run
     * the "grunt-env" tasks to set the "NODE_ENV" variable for your development
     * environment.
     *
     * Furthermore, a new nodemon property is added. This property can relaunch a MEAN
     * application when there are changes in project files. The script property is
     * used to define the main script file, in this case, the "server.js" file.
     *
     * The options property configures Nodemon's operation and tells it to watch both
     * the HTML and JavaScript files that are placed in your config and app folders.
     *
     * The last changes you've made load the "grunt-nodemon" module and add the
     * nodemon task as a subtask of the default task.
     *
     * A new "mochaTest" configuration property is added with a "src" property that
     * tells the Mocha task where to look for the test files and an options property
     * that sets Mocha's reporter.
     *
     * A new "karma" configuration property uses the "configFile" property to set Karma's
     * configuration filename and a new "protractor" configuration property that
     * uses the "configFile" property to set Protractor's configuration file name.
     *
     * You finished by loading the "grunt-karma", "grunt-mocha-test", and
     * "grunt-protractor-runner" modules and creating a new test task containing
     * "mochaTest", "karma", and "protractor" as subtasks.
     *
     * A new "jshint" configuration with an "src" property that tells the linter task
     * which JavaScript files to test. You also added a new "csslint" configuration
     * with an "src" property that tells the linter task which CSS files to test.
     *
     * You finished by loading the "grunt-contrib-jshint" and "grunt-contrib-csslint"
     * modules, and creating a new lint task containing "jshint" and "csslint" as
     * subtasks.
     *
     * A new "watch" configuration property is added with two subconfigurations.
     *
     * The first one is to watch the JavaScript files and the second is to watch the
     * CSS files. These watch configurations will automatically run the "jshint" and
     * "csslint" tasks whenever file changes are detected.
     *
     * Then, you created a new configuration for the concurrent task that will run
     * both the nodemon and watch tasks concurrently. Notice that the concurrent
     * task will log the console output of these tasks since you set the
     * "logConcurrentOutput" option to true.
     *
     * You finished by loading the "grunt-contrib-watch" and "grunt-concurrent"
     * modules and modifying your default task to use the concurrent task.
     *
     * You began by modifying the nodemon task by adding a new "debug" subtask.
     * The debug subtask will use the "nodeArgs" property to start your application
     * in debug mode.
     *
     * Then, you modified the concurrent task by adding a new debug subtask as well.
     * This time, the debug subtask is simply using the "nodemon:debug" task and the
     * new "node-inspector" task.
     *
     * Near the end of the configuration object, you minimally configured the new
     * "node-inspector" task and then loaded the "grunt-node-inspector" module. You
     * finished by creating a debug task and modifying your default task.
     */

    module.exports = function(grunt) {
        grunt.initConfig({
            env: {
                dev: {
                    NODE_ENV: 'development'
                },
                test: {
                    NODE_ENV: 'test'
                }
            },
            nodemon: {
                dev: {
                    script: 'application.js',
                    options: {
                        ext: 'js, html',
                        watch: ['application.js', 'config/**/*.js', 'app/**/*.js']
                    }
                },
                debug: {
                    script: 'application.js',
                    options: {
                        nodeArgs: ['--debug'],
                        ext: 'js, html',
                        watch: ['application.js', 'config/**/*.js', 'app/**/*.js']
                    }
                }
            },
            mochaTest: {
                src: 'app/tests/**/*.js',
                options: {
                    reporter: 'spec'
                }
            },
            karma: {
                unit: {
                    configFile: 'karma.conf.js'
                }
            },
            protractor: {
                e2e: {
                    options: {
                        configFile: 'protractor.conf.js'
                    }
                }
            },
            jshint: {
                all: {
                    src: ['application.js', 'config/**/*.js', 'app/**/*.js',
                        'public/js/*.js', 'public/modules/**/*.js']
                }
            },
            csslint: {
                all: {
                    src: 'public/modules/**/*.css'
                }
            },
            watch: {
                js: {
                    files: ['application.js', 'config/**/*.js', 'app/**/*.js',
                        'public/js/*.js', 'public/modules/**/*.js'],
                    tasks: ['jshint']
                },
                css: {
                    files: 'public/modules/**/*.css',
                    tasks: ['csslint']
                }
            },
            concurrent: {
                dev: {
                    tasks: ['nodemon', 'watch'],
                    options: {
                        logConcurrentOutput: true
                    }
                },
                debug: {
                    tasks: ['nodemon:debug', 'watch', 'node-inspector'],
                    options: {
                        logConcurrentOutput: true
                    }
                }
            },
            'node-inspector': {
                debug: {}
            }
        });
        grunt.loadNpmTasks('grunt-env');
        grunt.loadNpmTasks('grunt-nodemon');
        grunt.loadNpmTasks('grunt-mocha-test');
        grunt.loadNpmTasks('grunt-karma');
        grunt.loadNpmTasks('grunt-protractor-runner');
        grunt.loadNpmTasks('grunt-contrib-jshint');
        grunt.loadNpmTasks('grunt-contrib-csslint');
        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.loadNpmTasks('grunt-concurrent');
        grunt.loadNpmTasks('grunt-node-inspector');

        /**
         * Grunt task that could be launched using the command "grunt <taskname>".
         * For instance: test task -> "grunt test" in application root folder.
         *
         * IMPORTANT NOTE: Node-inspector will only work on browsers that use the
         * Blink engine, such as Google Chrome or Opera.
         */

        grunt.registerTask('default', ['env:dev', 'lint', 'concurrent:dev']);
        grunt.registerTask('debug', ['env:dev', 'lint', 'concurrent:debug']);
        grunt.registerTask('test', ['env:test', 'lint', 'concurrent:debug']);
        grunt.registerTask('lint', ['jshint', 'csslint']);
    };