module.exports = function(grunt) {
    "use strict"

    grunt.initConfig({
        // VERIFICAR AS ALTERAÇÕES NA PASTA
        watch: {
            options: {
                livereload: true
            },

            templates:{
                files: ["dev/tpl/includes/*.html","dev/tpl/pages/*.html"],
                tasks: ["includes"]
            },

            jsBootstrap: {
                files: "dev/js/bootstrap/*.js",
                tasks: ["uglify:bootstrap",'notify:uglify']
            },

            jsScripts: {
                files: "dev/js/scripts/*.js",
                tasks: ["uglify:scripts", "jshint:scripts",'notify:uglify']
            },

            jsPlugins: {
                files: "dev/js/plugins/*.js",
                tasks: ["uglify:plugins", "jshint:plugins",'notify:uglify']
            },

            sassBootstrap: {
                files: "dev/sass/bootstrap/*.{scss,sass}",
                tasks: ["sass:bootstrap1","notify:sass"]
            },

            sassStyles: {
                files: "dev/sass/styles/*.{scss,sass}",
                tasks: ["sass:styles","notify:sass"]
            },

            img: {
                files: "assets/img/*",
                tasks: ["tinyimg"]
            },

            sprites:{
                files: ["dev/sprites/*.png"],
                tasks: ["sprite"]
            },

            html: {
                files: "*.html"
            }
        },

        // JUNTA E COMPRIME ARQUIVOS JS
        uglify: {
            bootstrap: {
                options: {
                    mangle: false,
                    beautify: true,
                    mangle: {
                        reserved: ['jQuery']
                    }
                },
                files: {
                    'assets/js/bootstrap.min.js': [
                        'dev/js/bootstrap/transition.js',
                        'dev/js/bootstrap/alert.js',
                        'dev/js/bootstrap/button.js',
                        'dev/js/bootstrap/carousel.js',
                        'dev/js/bootstrap/collapse.js',
                        'dev/js/bootstrap/dropdown.js',
                        'dev/js/bootstrap/modal.js',
                        'dev/js/bootstrap/tab.js',
                        'dev/js/bootstrap/affix.js',
                        'dev/js/bootstrap/scrollspy.js',
                        'dev/js/bootstrap/tooltip.js',
                        'dev/js/bootstrap/popover.js'
                    ]
                }
            },

            scripts: {
                options: {
                    mangle: false,
                    mangle: {
                        reserved: ['jQuery']
                    }
                },
                files: {
                    'assets/js/scripts.min.js': ['dev/js/scripts/*.js']
                }
            },

            plugins: {
                options: {
                    mangle: true,
                    beautify: true,
                    mangle: {
                        reserved: ['jQuery']
                    }
                },
                files: {
                    'assets/js/plugins.min.js': ['dev/js/plugins/*.js']
                }
            }
        },

        // VERIFICA SE OS ARQUIVOS JS POSSUEM ERROS
        jshint: {
            scripts: {
                src: ['assets/js/scripts.min.js']
            },

            /*
            bootstrap: {
                src: ['assets/js/bootstrap.min.js']
            },
            */

            plugins: {
                src: ['dev/js/plugins/*.js']
            }
        },

        // COMPILAR SASS
        sass: {
            bootstrap1: {
                options: {
                    style: 'compact'
                },
                files: {
                    'assets/css/bootstrap.min.css': 'dev/sass/bootstrap/bootstrap.scss'
                }
            },

            styles: {
                options: {
                    style: 'compressed'
                },
                files: {
                    'assets/css/styles.min.css': 'dev/sass/styles/styles.scss'
                }
            }
        },

        // BROWSER SYNC E LIVE RELOAD
        connect: {
            server: {
                options: {
                    port: 9000,
                    base: ".",
                    hostname: "localhost",
                    livereload: true,
                    open: true
                }
            }
        },

        tinyimg: {
            dynamic: {
                files: [{
                    expand: false,
                    cwd: 'assets/img/',
                    src: ['assets/img/*.{png,jpg,svg}'],
                    dest: 'assets/img/'
                }]
            }
        },

        includes: {
            files: {
                src: ['dev/tpl/pages/*.html'], // Source files
                dest: '.', // Destination directory
                flatten: true,
                options: {
                    silent: true,
                    debug: true
                }
            }
        },


        // CONVERTER PX EM REM
        px_to_rem:{
            dev:{
                options:{
                    base: 16,
                    fallback: false,
                    fallback_existing_rem: false,
                    ignore: [],
                    map: false
                },

                files: {
                    'assets/css/styles.min.css': 'assets/css/styles.min.css'
                }
            },
        },


        // ADICIONAR PREFIXOS DE NAVEGADORES ANTIGOS NO CSS
        autoprefixer:{
            options:{
                browsers: ['opera 12', 'ff 15', 'chrome 25', 'ie 8', 'ie 9']
            },

            estilos: {
                src: 'assets/css/styles.min.css'
            }
        },


        // GERAR UM SPRITE PARA OS CSS
        sprite:{
            all: {
                src: 'dev/sprites/*.png',
                dest: 'assets/img/sprites.png',
                destCss: 'dev/sass/styles/sprites.scss',
                imgPath: '../img/sprites.png',
                extend: 'scss'
            }
        },


        notify: {
            options: {
                enabled: true,
                max_jshint_notifications: 5,
                title: "Project Name",
                success: false,
                duration: 3
            },

            server: {
                options: {
                    title: 'Server iniciado!',  // optional
                    message: 'O server já está rodando corretamente!', //required
                }
            },

            sass: {
                options: {
                    title: 'Tarefa concluída',  // optional
                    message: 'O SASS já compilou os arquivos CSS!', //required
                }
            },

            uglify: {
                options: {
                    title: 'Tarefa concluída',  // optional
                    message: 'O Uglify já juntou e compactou os arquivos JS e o JSHint verificou se possuem erros!', //required
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-notify');
    grunt.loadNpmTasks('grunt-tinyimg');
    grunt.loadNpmTasks('grunt-includes');
    grunt.loadNpmTasks('grunt-px-to-rem');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-spritesmith');

    grunt.registerTask('wait', function() {
        var done = this.async()
        setTimeout(done, 1000)
    });

    grunt.registerTask( "server", [ "connect", "notify:server", "watch"]);

    grunt.registerTask( "build", [ "px_to_rem", "autoprefixer"]);



};