module.exports = function(grunt) {

    var config = [];
    config.faviconFile = 'app/presenters/templates/includes/generated/favicon.latte';

    grunt.initConfig({

        watch: {
            less: {
                files: 'web-sources/less/**/*.less',
                tasks: ['string-replace', 'less', 'cssmin']
            },
            doctrineCache: {
                files: 'app/model/entities/**/*.php',
                tasks: ['clean:doctrineCache']
            },
            javaScripts: {
                files: 'web-sources/js/**/*.js',
                tasks: ['concat:js', 'uglify']
            }
        },

        less: {
            guildGate: {
                files: {
                    "www/css/style.css" : "web-sources/less/guild-gate.less"
                }
            }
        },

        concat: {
            js: {
                src: ['web-sources/libs/jquery/dist/jquery.js',
                    'web-sources/libs/nette-live-form-validation/live-form-validation.js',
                    'web-sources/libs/nette.ajax.js/nette.ajax.js',
                    'web-sources/libs/happy/dist/happy.min.js',
                    'web-sources/libs/moment/moment.js',
                    'web-sources/libs/eonasdan-bootstrap-datetimepicker/src/js/bootstrap-datetimepicker.js',
                    //'web-sources/libs/bootstrap-datepicker/dist/js/bootstrap-datepicker.js',
                    'web-sources/libs/jquery-ui-sortable/jquery-ui-sortable.js',
                    'web-sources/libs/ublaboo-datagrid/assets/dist/datagrid.js',
                    'web-sources/libs/ublaboo-datagrid/assets/dist/datagrid-spinners.js',
                    'web-sources/libs/bootstrap/dist/js/bootstrap.js',
                    'web-sources/libs/AdminLTE/dist/js/app.js',
                    'vendor/xhulav/BootstrapForms/src/assets/bootstrap-forms.js',
                    'web-sources/libs/imageviewer/dist/viewer.js',
                    'vendor/nasext/dependent-select-box/client-side/dependentSelectBox.js',
                    'web-sources/libs/select2/dist/js/select2.js',
                    'web-sources/js/init-libraries.js',
                    'web-sources/js/scripts/*.js'],
                dest: 'www/js/main.js'
            }
        },

        cssmin: {
            css: {
                src: ['www/css/style.css'],
                dest: 'www/css/style.min.css'
            }
        },

        uglify: {
            js: {
                src: 'www/js/main.js',
                dest: 'www/js/main.min.js'
            }
        },

        clean: {
            doctrineCache: {
                src: ['temp/cache/_Doctrine.Annotations', 'temp/cache/_Doctrine.Default.metadata']
            },
            latte: {
               src: ['temp/cache/latte']
            },
            server: {
                src: ['temp/cache']
            },
            fonts: {
                src: ['www/fonts/*', '!www/fonts/.gitignore']
            },
            fileUpload: {
                src: ['www/fileupload']
            },
            tinyMCE: {
                src: ['www/tinymce']
            }
        },

        copy: {
            fonts: {
                files: [
                    {expand: true, flatten: true, src: ['web-sources/libs/bootstrap/fonts/*'], dest: 'www/fonts', filter: 'isFile'},
                    {expand: true, flatten: true, src: ['web-sources/libs/font-awesome/fonts/*'], dest: 'www/fonts', filter: 'isFile'}
                ]
            },
            fileUpload: {
                files: [
                    {expand: true, cwd: 'vendor/jzechy/jquery-fileupload/src/www/fileupload/', src: ['**'], dest: 'www/fileupload/'}
                ]
            },
            tinyMCE: {
                files: [
                    {expand: true, cwd: 'web-sources/libs/tinymce/', src: ['**'], dest: 'www/tinymce/'}
                ]
            }
        },

        'string-replace': {
            adminLte: {
                files: {
                    './': 'web-sources/libs/AdminLTE/build/less/**/*.less'
                },
                options: {
                    saveUnchanged: false,
                    replacements: [{
                        pattern: '@import "../../bootstrap-less/mixins.less";',
                        replacement: ''
                    },{
                        pattern: '@import (reference) "../bootstrap-less/mixins.less";',
                        replacement: ''
                    }]
                }
            }
        },

        realFavicon: {
            favicons: {
                src: 'web-sources/favicon-source-file.png',
                dest: 'www/img/favicon/',
                options: {
                    iconsPath: '/img/favicon/',
                    html: [config.faviconFile],
                    design: {
                        ios: {
                            pictureAspect: 'backgroundAndMargin',
                            backgroundColor: '#000000',
                            margin: '14%'
                        },
                        desktopBrowser: {},
                        windows: {
                            pictureAspect: 'noChange',
                            backgroundColor: '#da532c',
                            onConflict: 'override'
                        },
                        androidChrome: {
                            pictureAspect: 'backgroundAndMargin',
                            margin: '10%',
                            backgroundColor: '#000000',
                            themeColor: '#000000',
                            manifest: {
                                name: 'GuildGate',
                                display: 'browser',
                                orientation: 'notSet',
                                onConflict: 'override',
                                declared: true
                            }
                        },
                        safariPinnedTab: {
                            pictureAspect: 'blackAndWhite',
                            threshold: 50,
                            themeColor: '#b03500'
                        }
                    },
                    settings: {
                        compression: 1,
                        scalingAlgorithm: 'Mitchell',
                        errorOnImageTooSmall: true
                    },
                    versioning: {
                        paramName: 'v'
                    }
                }
            }
        }
    });

    grunt.registerTask('deploy', ['string-replace', 'less', 'concat', 'cssmin', 'uglify', 'clean', 'copy']);
    grunt.registerTask('faviconFile', 'Creates an empty file', function() {
        grunt.file.write(config.faviconFile, '');
    });
    grunt.registerTask('generateFavicon', ['faviconFile', 'realFavicon']);

    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-string-replace');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-real-favicon');
};