'use strict';

var markdown = require('node-markdown').Markdown;

var LIVERELOAD_PORT = 35729;
var lrSnippet = require('connect-livereload')({ port: LIVERELOAD_PORT });
var mountFolder = function (connect, dir) {
    return connect.static(require('path').resolve(dir));
};

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

    // configurable paths
    var yeomanConfig = {
        module: 'angular.library',
        src: 'src',
        dist: 'dist',
        tmp: '.tmp'
    };
    try {
        yeomanConfig.app = require('./bower.json').appPath || yeomanConfig.app;
    } catch (e) {
    }

    grunt.initConfig({
        filename: 'angular-library',
        yeoman: yeomanConfig,
        dist: 'dist',
        pkg: grunt.file.readJSON('package.json'),
        meta: {
            banner: '/**\n' +
                ' * <%= pkg.description %>\n' +
                ' * @version v<%= pkg.version %><%= buildtag %>\n' +
                ' * @link <%= pkg.homepage %>\n' +
                ' * @license MIT License, http://www.opensource.org/licenses/MIT\n' +
                ' */'
        },

        // ===========
        // Common tasks
        // ===========

        clean: {
            dist: {
                dot: true,
                src: [
                    '<%= yeoman.tmp %>',
                    '<%= yeoman.dist %>/*'
                ]
            },
            server: '<%= yeoman.tmp %>'
        },

        // ===========
        // Build tasks.
        // ===========

        /**
         * Encode images in base64 to embed them in css files.
         */
        imageEmbed: {
            dist: {
                src: [ '<%= yeoman.src %>/css/main.css'],
                dest: '<%= yeoman.tmp %>/<%= filename %>.css',
                options: {
                    deleteAfterEncoding: false
                }
            }
        },
        /**
         * Minify css based on the css generated by image-embed.
         */
        cssmin: {
            options: {
                banner: '<%= meta.banner %>'
            },
            dist: {
                files: {
                    '<%= yeoman.dist %>/css/<%= filename %>.css': [
                        '<%= yeoman.tmp %>/<%= filename %>.css'
                    ]
                }
            }
        },
        /**
         * Transform html templates to js to use the $templateCache.
         */
        ngtemplates: {
            app: {
                options: {
                    module: '<%= yeoman.module %>',
                    concat: 'js'
                },
                cwd: '<%= yeoman.src %>',
                src: 'js/**/*.html',
                dest: '<%= yeoman.tmp %>/templates.js'
            }
        },

        /**
         * Minify angular files
         */
        ngmin: {
            dist: {
                expand: true,
                cwd: '<%= yeoman.src %>/js',
                src: ['**/*.js'],
                dest: '<%= yeoman.tmp %>/generated'
            }
        },

        /**
         * Concat generates files by ngmin.
         */
        concat: {
            js: {
                // .mdl must be the first file, it's the module declaration.
                src: [ '<%= yeoman.tmp %>/generated/library.mdl.js', '<%= yeoman.tmp %>/generated/**/*.js' ],
                dest: '<%= yeoman.dist %>/js/<%= filename %>-tpls.js',
                options: {
                    banner: '<%= meta.banner %>\n'
                }
            }
        },

        /**
         * Uglify.
         */
        uglify: {
            options: {
                banner: '<%= meta.banner %>',
                mangle: false
            },
            js: {
                src: ['<%= yeoman.dist %>/js/<%= filename %>-tpls.js'],
                dest: '<%= yeoman.dist %>/js/<%= filename %>-tpls.min.js'
            }
        },

        // ===========
        // Tests tasks.
        // ===========

        /**
         * Html w3c validation.
         */
        validation: {
            options: {
                doctype: 'HTML5',
                reset: true,
                relaxerror: [ 'Element head is missing a required instance of child element title.' ]
            },
            files: {
                src: ['<%= yeoman.src %>/js/**/*.html' ]
            }
        },

        /**
         * Javascript code style.
         */
        jshint: {
            files: ['Gruntfile.js', '<%= yeoman.src %>/js/**/*.js'],
            options: {
                node: true,
                curly: true,
                immed: true,
                newcap: true,
                noarg: true,
                sub: true,
                boss: true,
                eqnull: true,
                globals: {
                    angular: true
                }
            }
        },

        /**
         * Karma test runner.
         */
        karma: {
            unit: {
                configFile: 'karma.conf.js'
            }
        },

        // ===========
        // Server tasks.
        // ===========

        connect: {
            options: {
                port: 9001,
                // Change this to '0.0.0.0' to access the server from outside.
                hostname: 'localhost'
            },
            livereload: {
                options: {
                    middleware: function (connect) {
                        return [
                            lrSnippet,
                            mountFolder(connect, '<%= yeoman.tmp %>'),
                            mountFolder(connect, yeomanConfig.src)
                        ];
                    }
                }
            }
        },

        /**
         * Watch and files and reload browser.
         */
        watch: {
            options: {
                livereload: true
            },
            files: {
                files: ['<%= yeoman.src %>/**/*.html', '<%= yeoman.src %>/css/*.css', '<%= yeoman.src %>/js/**/*.js' ],
                tasks: [  ]
            }
        },
        concurrent: {
            server: [ ]
        },

        /**
         * Opens the default browser.
         */
        open: {
            server: {
                url: 'http://localhost:<%= connect.options.port %>'
            }
        },

        // ===========
        // Release tasks.
        // ===========

        bump: {
            options: {
                files: [ 'package.json', 'bower.json' ],
                updateConfigs: [ 'pkg' ],
                commitMessage: 'chore(release): v%VERSION%',
                commitFiles: [ 'package.json', 'bower.json', 'dist', 'CHANGELOG.md' ],
                pushTo: 'origin'
            }
        },
        changelog: {
            options: {
                dest: 'CHANGELOG.md',
                templateFile: 'misc/changelog.tpl.md'
            }
        }
    });

    grunt.registerTask('server', function () {
        grunt.task.run([ 'clean:server', 'concurrent:server', 'connect:livereload', 'open', 'watch' ]);
    });

    grunt.registerTask('dist', [ 'default' ]);
    grunt.registerTask('default', [ 'before-build', 'build' ]);

    grunt.registerTask('before-build', [ 'jshint', 'validation' ]);
    grunt.registerTask('build', [ 'clean:dist', 'build-css', 'build-js' ]);
    grunt.registerTask('build-css', [ 'imageEmbed', 'cssmin']);
    grunt.registerTask('build-js', [ 'ngmin', 'ngtemplates', 'concat', 'uglify' ]);

    grunt.registerTask('release', [ 'prepare-release', 'dist', 'changelog', 'perform-release' ]);
    grunt.registerTask('prepare-release', [ 'bump-only' ]);
    grunt.registerTask('perform-release', [ 'bump-commit' ]);

    return grunt;
};
@@ -738,38 +738,16 @@
 Config,%0A
-        dist: 'dist',%0A
         
@@ -5881,16 +5881,37 @@
 files: %5B
+%0A                    
 '%3C%25= yeo
@@ -5932,16 +5932,36 @@
 *.html',
+%0A                   
  '%3C%25= ye
@@ -5983,16 +5983,82 @@
 /*.css',
+%0A                    '%3C%25= yeoman.src %25%3E/*.js',%0A                   
  '%3C%25= ye
@@ -6072,32 +6072,48 @@
 c %25%3E/js/**/*.js'
+%0A               
  %5D,%0A            
@@ -6124,17 +6124,16 @@
 tasks: %5B
- 
  %5D%0A     

Fix watch grunt task to watch js in the base app dir. Remove dist unused config property.