'use strict';

module.exports = function (grunt) {
    require('load-grunt-tasks')(grunt);
    require('time-grunt')(grunt);

    // configurable paths
    var yeomanConfig = {
        app: 'app',
        dist: 'dist'
    };

    try {
        yeomanConfig.app = require('./bower.json').appPath || yeomanConfig.app;
    } catch (e) {}

    grunt.loadNpmTasks('grunt-express-server');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.initConfig({
        yeoman: yeomanConfig,
        watch: {
            options: {
                livereload: true
            },
            files: [
                '<%= yeoman.app %>/{,*/}*.html',
                '.tmp/styles/{,*/}*.css',
                '{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js',
                '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
            ],
            tasks:  []
        },
        express: {
            dev: {
                options: {
                    script: 'server.js'
                }
            }
        },
        open: {
            server: {
                url: 'http://localhost:3000'
            }
        },
        clean: {
            dist: {
                files: [{
                    dot: true,
                    src: [
                        '.tmp',
                        '<%= yeoman.dist %>/app/scripts/angular/*',
                        '<%= yeoman.dist %>/app/scripts/angular-resource/*',
                        '<%= yeoman.dist %>/app/scripts/angular-ui-bootstrap/*',
                        '<%= yeoman.dist %>/app/scripts/angular-ui-bootstrap/template/model/*',
                        '<%= yeoman.dist %>/app/scripts/controllers/*',
                        '<%= yeoman.dist %>/app/scripts/jquery/*',
                        '<%= yeoman.dist %>/app/*',
                        '<%= yeoman.dist %>/*',
                        '!<%= yeoman.dist %>/.git*'
                    ]
                }]
            },
            server: '.tmp'
        },
        // Put files not handled in other tasks here
        copy: {
            dist: {
                files: [{
                        expand: true,
                        dot: true,
                        cwd: '<%= yeoman.app %>',
                        dest: '<%= yeoman.dist %>/app',
                        src: [
                            '*.{ico,png}',
                            'index.html',
                            'images/{,*/}*.{gif,jpg,webp}',
                            'styles/fonts/*'
                        ]
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/images',
                        dest: '<%= yeoman.dist %>/app/images',
                        src: [
                            '*'
                        ]
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/scripts',
                        dest: '<%= yeoman.dist %>/app/scripts/',
                        src: '*.js'
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/styles',
                        dest: '<%= yeoman.dist %>/app/styles/',
                        src: '{,*/}*.css'
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components',
                        dest: '<%= yeoman.dist %>/app/scripts/',
                        src: [
                            'angular/angular.js',
                            'angular-resource/angular-resource.js',
                            'jquery/jquery.js'
                        ]
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/angular-ui-bootstrap/src/collapse/',
                        dest: '<%= yeoman.dist %>/app/scripts/angular-ui-bootstrap/',
                        src: 'collapse.js'
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/angular-ui-bootstrap/src/modal/',
                        dest: '<%= yeoman.dist %>/app/scripts/angular-ui-bootstrap/',
                        src: 'modal.js'
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/angular-ui-bootstrap/',
                        dest: '<%= yeoman.dist %>/app/scripts/angular-ui-bootstrap/',
                        src: 'template/modal/*.html'
                    }, {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/angular-ui-bootstrap/src/transition/',
                        dest: '<%= yeoman.dist %>/app/scripts/angular-ui-bootstrap/',
                        src: 'transition.js'
                    }, {
                        expand: true,
                        dot: true,
                        dest: '<%= yeoman.dist %>',
                        src: [
                            'package.json',
                            'server.js',
                            'routes/*.js'
                        ]
                    }
               ]
            }
        },
    });

    grunt.registerTask('reload', [
            'open',
            'watch'
    ]);

    grunt.registerTask('test', [
        'clean:server',
        'concurrent:test',
        'autoprefixer',
        'connect:test',
        'karma'
    ]);

    grunt.registerTask('build', [
        'clean:dist',
        'copy:dist'
    ]);

    grunt.registerTask('default', [
        'jshint',
        'test',
        'build'
    ]);
};