'use strict';

module.exports = function(grunt) {
    var path = require('path');

    // Unified Watch Object
    var watchFiles = {
        serverViews: ['app/views/**/*.*'],
        serverJS: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js'],
        clientCss: ['public/css/*.css','!public/css/*.min.css']
    };

    // Project Configuration
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            serverViews: {
                files: watchFiles.serverViews,
                options: {
                    livereload: false
                }
            },
            serverJS: {
                files: watchFiles.serverJS
            },
            clientCss: {
                files: watchFiles.clientCss,
                tasks: ['cssmin']
            }
        },
        nodemon: {
            dev: {
                script: 'server.js',
                options: {
                    ext: 'js,html',
                    watch: watchFiles.serverViews.concat(watchFiles.serverJS),
                    ignore: ['node_modules/**']
                }
            }
        },
        concurrent: {
            default: ['cssmin', 'nodemon:dev', 'watch'],
            options: {
                logConcurrentOutput: true,
                limit: 10
            }
        },
        env: {
            dev: {
                NODE_ENV: 'development'
            },
            test: {
                NODE_ENV: 'test'
            },
            secure: {
                NODE_ENV: 'secure'
            },
            pro: {
                NODE_ENV: 'production'
            }
        },
        cssmin: {
            target: {
                files: [{
                    expand: true,
                    cwd: 'public/css',
                    src: ['*.css', '!*.min.css'],
                    dest: 'public/css',
                    ext: '.min.css'
                }]
            }
        }
    });

    // Load NPM tasks
    require('load-grunt-tasks')(grunt);

    // Making grunt default to force in order not to break the project.
    grunt.option('force', true);

    // Default task(s).
    grunt.registerTask('default', ['env:dev', 'concurrent:default']);

    // Debug task.
    grunt.registerTask('debug', ['env:dev', 'concurrent:debug']);

    // Secure task(s).
    grunt.registerTask('production', ['env:pro', 'concurrent:default']);

    // Lint task(s).
    grunt.registerTask('lint', ['env:dev', 'jshint', 'csslint']);
};
