var src_folders = ['wui-3', 'wui-3-rt', 'wui-7', 'wui-9', 'wui-9-mts', 'wui-10', 'wui-11', 'upload'];

var cssminConf = function() {
  var conf = {};
  for (var i = src_folders.length - 1; i >= 0; i--) {
    var srcName = src_folders[i];
    conf[srcName] = {
      expand: true,
      cwd: 'src/'+srcName,
      src: ['*.css', '!*.min.css'],
      dest: 'src/'+srcName,
      ext: '.min.css'
    };
  }
  return conf;
};

var uglifyConf = function() {
  var conf = {};
  for (var i = src_folders.length - 1; i >= 0; i--) {
    var srcName = src_folders[i];
    conf[srcName] = {
      files: [{
        expand: true,
        cwd: 'src/'+srcName,
        src: '**/*.js',
        dest: 'src/'+srcName,
        rename: function (dst, src) {
          return dst + '/' + src.replace('.js', '.min.js');
        }
      }]
    };
  }
  return conf;
};

var compressConf = function() {
  var conf = {};
  for (var i = src_folders.length - 1; i >= 0; i--) {
    var srcName = src_folders[i];
    conf[srcName] = {
      options: { mode: 'gzip' },
      files: [
        {expand: true, cwd: 'src/'+srcName+'/',src: ['*.min.js'], dest: 'dist/'+srcName, ext: '.js'},
        {expand: true, cwd: 'src/'+srcName+'/',src: ['*.min.css'], dest: 'dist/'+srcName, ext: '.css'},
        {expand: true, cwd: 'src/'+srcName+'/',src: ['*.min.html'], dest: 'dist/'+srcName, ext: '.html'}
      ]
    };
  }
  return conf;
};

var htmlminConf = function() {
  var conf = {};
  for (var i = src_folders.length - 1; i >= 0; i--) {
    var srcName = src_folders[i];
    conf[srcName] = {
      options: { removeComments: true, collapseWhitespace: true },
      files: [{
        expand: true,
        cwd: 'src/'+srcName,
        src: ['*.html'],
        dest: 'src/'+srcName,
        rename: function (dst, src) {
          return dst + '/' + src.replace('.html', '.min.html');
        }
      }]
    };
  }
  return conf;
};

var imageminConf = function() {
  var conf = {};
  for (var i = src_folders.length - 1; i >= 0; i--) {
    var srcName = src_folders[i];
    var logo = {};
    if (srcName ===  'wui-3-rt') {
      logo['dist/'+srcName+'/logo.png'] = 'src/'+srcName+'/logo.png';
    } else {
      logo['dist/'+srcName+'/rotek.png'] = 'src/'+srcName+'/rotek.png';
    }
    var favicon = {};
    favicon['dist/'+srcName+'/favicon.ico'] = 'src/'+srcName+'/favicon.ico';
    if (srcName === 'upload') {
      conf[srcName] = {
        options: { optimizationLevel: 0 },
        files: [ favicon ]
      };
    } else {
      conf[srcName] = {
        options: { optimizationLevel: 0 },
        files: [ logo, favicon ]
      };
    }
  }
  return conf;
};

module.exports = function (grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    cssmin: cssminConf(),
    uglify: uglifyConf(),
    compress: compressConf(),
    htmlmin: htmlminConf(),
    imagemin: imageminConf(),
    clean: {
      all: {
        src: [
          '!src/**/*.js',
          '!src/**/*.css',
          'src/**/*.min.js',
          'src/**/*.min.css',
          'src/**/*.min.html'
        ]
      },
      clear: {
        src: 'dist*/*'
      }
    },
    jshint: {
      all: ['Gruntfile.js', 'src/**/*.js']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-htmlmin');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-compress');
  grunt.loadNpmTasks('grunt-contrib-imagemin');
  grunt.loadNpmTasks('grunt-contrib-jshint');

  grunt.registerTask('default', ['cssmin','uglify','htmlmin','clean:all']);
  grunt.registerTask('build', ['cssmin','uglify','htmlmin','compress','clean:all', 'imagemin']);
  grunt.registerTask('clear', ['clean:clear']);
  grunt.registerTask('jhint', ['jshint']);
};
