/*
 * grunt-replacebyfilename
 * https://github.com/zhengzhaolong/replacebyfilename
 *
 * Copyright (c) 2016 YCell
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('replace', 'replace text in html by filename that you assign the directory', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
          addhash:true
    }),
        replacements = this.data.replacements;

    this.files.forEach(function(f) {
      // Concat specified files.
      var src = f.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      }).map(function(filepath) {
        // Read file source.
          if(options.addhash){
            addhash(filepath,replacements);
          }else{
            removeHash(filepath,replacements);
          }
      });

      // Write the destination file.
      //grunt.file.write(f.dest, src);
      //
      //// Print a success message.
      //grunt.log.writeln('File "' + f.dest + '" created.');
    });
  });

  function addhash(filepath,replacements){
    var fileContent =  grunt.file.read(filepath);
    replacements.forEach(function(obj){
      var path = obj.path,
          files = grunt.file.expand(path),
          from = obj.from,
          to = obj.to;
      fileContent = fileContent.replace(from,function(){
        var arr  = Array.from(arguments),
            $num = arr.length-3,
            reg = null,
            toArr = to.split(/[$\d]+/g),
            result = '',
            match = '';
        if($num === 2){
          reg = new RegExp(arr[1]+'/'+arr[2]+"\\.(\\w+)\\.\\w+",'g');
        }else if($num === 1){
          reg = new RegExp(arr[1]+"\\.(\\w+)\\.\\w+",'g');
        }
        files.forEach(function (file) {
          var result = reg.exec(file);
          if(result !== null){
            match = result[1];
          }
        });
        if($num === 1){ //without dir
          if(match !== ''){
            result = toArr[0]+arr[1]+'.'+match+toArr[1];
          }else{
            result = toArr[0]+arr[1]+toArr[1];
          }
        }else if($num === 2){
          if(match !== ''){
            result = toArr[0]+arr[1]+toArr[1]+arr[2]+'.'+match+toArr[2];
          }else{
            result = toArr[0]+arr[1]+toArr[1]+arr[2]+toArr[2];
          }
        }
        return result;
      });
    });
    grunt.file.write(filepath, fileContent);
  }

  function removeHash(filepath,replacements)

};

{
    var fileContent =  grunt.file.read(filepath);
    replacements.forEach(function(obj){
      var from = obj.from,
          to = obj.to;
      fileContent = fileContent.replace(from,function(){
        var arr  = Array.from(arguments),
            $num = arr.length-3,
            toArr = to.split(/[$\d]+/g),
            result = '';
        if($num === 1){ //without dir
          // {from:/css\/(\w+)\.\w+\.css/g,to:'css/$1.css'},
          result = toArr[0]+arr[1] + toArr[1];
        }else if($num === 2){
            result = toArr[0]+arr[1]+toArr[1]+arr[2]+toArr[2];
        }
        return result;
      });
    });
    grunt.file.write(filepath, fileContent);
  }