module.exports = function (grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    cssmin: {
      wui: {
        expand: true,
        cwd: 'src/wui',
        src: ['*.css', '!*.min.css'],
        dest: 'src/wui',
        ext: '.min.css'
      },
      upload: {
        expand: true,
        cwd: 'src/upload',
        src: ['*.css', '!*.min.css'],
        dest: 'src/upload',
        ext: '.min.css'
      }
    },
    uglify: {
      wui: {
        files: {
          'src/wui/main.min.js': ['src/wui/main.js'],
          'src/wui/role.min.js': ['src/wui/role.js']
        }
      },
      upload: {
        files: {
          'src/upload/upload.min.js': ['src/upload/upload.js']
        }
      }
    },
    compress: {
      wui: {
        options: {
          mode: 'gzip'
        },
        files: [
          {expand: true, cwd: 'src/wui/',src: ['*.min.js'], dest: 'dist/wui', ext: '.js'},
          {expand: true, cwd: 'src/wui/',src: ['*.min.css'], dest: 'dist/wui', ext: '.css'},
          {expand: true, cwd: 'src/wui/',src: ['*.min.html'], dest: 'dist/wui', ext: '.html'}
        ]
      },
      upload: {
        options: {
          mode: 'gzip'
        },
        files: [
          {expand: true, cwd: 'src/upload/',src: ['*.min.js'], dest: 'dist/upload', ext: '.js'},
          {expand: true, cwd: 'src/upload/',src: ['*.min.css'], dest: 'dist/upload', ext: '.css'},
          {expand: true, cwd: 'src/upload/',src: ['*.html'], dest: 'dist/upload', ext: '.html'}
        ]
      }
    },
    htmlmin: {
      wui: {
        options: {
          removeComments: true,
          collapseWhitespace: true,
          minifyJS : true
        },
        files: {
          'src/wui/login.min.html':       'src/wui/login.html',
          'src/wui/index.min.html':       'src/wui/index.html',
          'src/wui/settings.min.html':    'src/wui/settings.html',
          'src/wui/info.min.html':        'src/wui/info.html',
          'src/wui/history.min.html':     'src/wui/history.html',
          'src/wui/ups_history.min.html': 'src/wui/ups_history.html'
        }
      },
      upload: {
        options: {
          removeComments: true,
          collapseWhitespace: true,
          minifyJS : true
        },
        files: {
          'dist/upload/index.html':    'src/upload/index.html',
          'dist/upload/error.html':    'src/upload/error.html',
          'dist/upload/success.html':  'src/upload/success.html'
        }
      }
    },
    imagemin: {
      wui:{
        options: {
          optimizationLevel: 0
        },
        files: [
          {'dist/wui/rotek.png': 'src/wui/rotek.png'},
          {'dist/wui/favicon.ico': 'src/wui/favicon.ico'},
          {'dist/upload/favicon.ico': 'src/upload/favicon.ico'}
        ]
      }
    },
    clean: {
      wui: {
        src: ['src/wui/main.min.js', 'src/wui/*.min.html', '!src/wui/main.js', 'src/wui/role.min.js', '!src/wui/role.js', 'src/wui/main.min.css', '!src/wui/main.css']
      },
      upload: {
        src: ['src/upload/upload.min.js', '!src/upload/upload.js', 'src/upload/upload.min.css', '!src/upload/upload.css']
      },
      clear: {
        src: 'dist*/*'
      }
    },
    jshint: {
      all: ['Gruntfile.js', 'src/wui/main.js', 'src/upload/upload.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:wui','clean:upload']);
  grunt.registerTask('build', ['cssmin','uglify','htmlmin','compress','clean:wui','clean:upload','imagemin']);
  grunt.registerTask('clear', ['clean:clear']);
  grunt.registerTask('jhint', ['jshint']);
};
