module.exports = function(grunt) {
    // load config functio
    function loadConfig(path) {
        var glob = require('glob');
        var object = {};
        var key;

        glob.sync('*', {
            cwd: path
        }).forEach(function(option) {
            key = option.replace(/.js$/, '');
            object[key] = require(path + option)(config);
        });

        return object;
    }

    require('load-grunt-tasks')(grunt);
    grunt.loadTasks('tasks');

    // Project configuration.
    var config = {
        pkg: grunt.file.readJSON('package.json'),
        env: process.env,
        cssFilesToInject: [
            'linker/styles/bootstrap*.css',
            'linker/**/*.css'
        ].map(function(path) {
            return '.tmp/public/' + path;
        }),
        jsFilesToInject: [
            'linker/js/vendor/socket.io.js',
            'linker/js/vendor/sails.io.js',
            'linker/js/vendor/jquery-2.1.0.js',
            'linker/js/vendor/bootstrap.js',
            'linker/js/vendor/angular.js',
            'linker/js/vendor/angular-*.js',
            'linker/js/vendor/underscore.js',
            'linker/js/app.js',
            'linker/js/**/module.js',
            'linker/**/*.js'
        ].map(function(path) {
            return '.tmp/public/' + path;
        }),
        templateFilesToInject: [
            'linker/**/*.html'
        ].map(function(path) {
            return 'assets/' + path;
        })
    };

    grunt.util._.extend(config, loadConfig('./tasks/options/'));
    grunt.initConfig(config);

    // task aliases

    // When Sails is lifted:
    grunt.registerTask('default', [
        'compileAssets',
        'linkAssets',
        'watch'
    ]);

    grunt.registerTask('compileAssets', [
        'clean:dev',
        'less:dev',
        'jade:dev',
        'copy:dev',
        'coffee:dev'
    ]);

    grunt.registerTask('linkAssets', [
        // Update link/script/template references in `assets` index.html
        'sails-linker:devJs',
        'sails-linker:devStyles',
        'sails-linker:devTpl',
        'sails-linker:devJsJADE',
        'sails-linker:devStylesJADE',
        'sails-linker:devTplJADE'
    ]);

    // Build the assets into a web accessible folder.
    // (handy for phone gap apps, chrome extensions, etc.)
    grunt.registerTask('build', [
        'compileAssets',
        'linkAssets',
        'clean:build',
        'copy:build'
    ]);

    // When sails is lifted in production
    grunt.registerTask('prod', [
        'clean:dev',
        'jade:dev',
        'less:dev',
        'copy:dev',
        'coffee:dev',
        'concat',
        'uglify',
        'cssmin',
		'copy:fonts',
        'sails-linker:prodJs',
        'sails-linker:prodStyles',
        'sails-linker:devTpl',
        'sails-linker:prodJsJADE',
        'sails-linker:prodStylesJADE',
        'sails-linker:devTplJADE'
    ]);

    // When API files are changed:
    // grunt.event.on('watch', function(action, filepath) {
    //   grunt.log.writeln(filepath + ' has ' + action);

    //   // Send a request to a development-only endpoint on the server
    //   // which will reuptake the file that was changed.
    //   var baseurl = grunt.option('baseurl');
    //   var gruntSignalRoute = grunt.option('signalpath');
    //   var url = baseurl + gruntSignalRoute + '?action=' + action + '&filepath=' + filepath;

    //   require('http').get(url)
    //   .on('error', function(e) {
    //     console.error(filepath + ' has ' + action + ', but could not signal the Sails.js server: ' + e.message);
    //   });
    // });
};
