module.exports = function(grunt) {
    grunt.initConfig({
        browserify: {
            dev: {
                src: ['app/ribica.js'],
                dest: 'build/ribica.bundle.js',
                options: {
                    transform: ['reactify'],
                    browserifyOptions: {
                        standalone: 'RIBICA',
                        debug: true
                    },
                    watch: true
                }
            },
            prod: {
                src: ['app/ribica.js'],
                dest: 'build/ribica.bundle.js',
                options: {
                    transform: ['reactify'],
                    browserifyOptions: {
                        standalone: 'RIBICA'
                    }
                }
            }
        },
        watch: {
            files: ['build/ribica.bundle.js', 'app/css/*.css'],
            tasks: ['config-dev', 'concat:css', 'concat:js']
        },
        connect: {
            server: {
                options: {
                    port: 3001,
                    base: 'build',
                    middleware: function(connect, options) {
                        return [
                            function(req, res) {
                                var filename = 'build/' + req.url;
                                if ((filename === 'build//') || !grunt.file.exists(filename)) filename = 'build/index.html';
                                res.end(grunt.file.read(filename));
                            }
                        ];
                    },
                }
            }
        },
        uglify: {
            my_target: {
                files: {
                    'build/ribica.js': ['build/ribica.js']
                }
            }
        },
        config: {
            dev: {
                options: {
                    variables: {
                        apiEndpoint: 'http://localhost:4567'

                    }
                }
            },
            prod: {
                options: {
                    variables: {
                        apiEndpoint: '/api'
                    }
                }
            }
        },
        replace: {
            dist: {
                options: {
                    variables: {
                        'apiEndpoint': '<%= grunt.config.get("apiEndpoint") %>'
                    },
                    force: true
                },
                files: [{
                    expand: true,
                    flatten: true,
                    src: ['build/ribica.bundle.js'],
                    dest: 'build/configured'
                }]
            }
        },
        concat: {
            css: {
                src: [
                    'app/css/*.css'

                ],
                dest: 'build/ribica.css'
            },
            js: {
                src: ['build/configured/ribica.bundle.js'],
                dest: 'build/ribica.js'
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-browserify');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-config');
    grunt.loadNpmTasks('grunt-replace');


    grunt.registerTask('default', []);
    grunt.registerTask('config-dev', ['config:dev', 'replace']);
    grunt.registerTask('config-prod', ['config:prod', 'replace']);
    grunt.registerTask('dev', ['browserify:dev', 'config-dev', 'concat:css', 'concat:js', 'connect:server:keepalive']);
    grunt.registerTask('build', ['browserify:prod', 'config-prod', 'concat:css', 'concat:js', 'uglify']);

};
