'use strict';

var path = require('path');
var util = require('util');

module.exports = function(grunt) {
  grunt.registerMultiTask('vjslanguages', 'A Grunt plugin for compiling VideoJS language assets.', function() {
    this.files.forEach(function(f) {
      var languageName, languageData, jsTemplate, jsFilePath;
      // Multiple Files Case
      if(util.isArray(f.src)){
        for(var i =0; i < f.src.length; i++) {
          console.log('-', f.src[i], f.dest);
          languageName = path.basename(f.src[i], '.json');
          languageData = grunt.file.readJSON(f.src[i]);
          jsTemplate = 'videojs.addLanguage("' + languageName + '",' + JSON.stringify(languageData,null,' ') + ');';
          jsFilePath = path.join(f.dest, languageName + '.js');
          grunt.file.write(jsFilePath, jsTemplate);
          grunt.log.writeln('- [' + languageName +'] Language Built. File "' + jsFilePath + '" created.');
        }
      }
      // Singular File Case
      else {
        languageName = path.basename(f.src, '.json');
        languageData = grunt.file.readJSON(f.src);
        jsTemplate = 'videojs.addLanguage("' + languageName + '",' + JSON.stringify(languageData,null,' ') + ');';
        jsFilePath = path.join(f.dest, languageName + '.js');
        grunt.file.write(jsFilePath, jsTemplate);
        grunt.log.writeln('- [' + languageName +'] Language Built. File "' + jsFilePath + '" created.');
      }
    });
  });
};
@@ -208,24 +208,382 @@
 unction() %7B%0A
+%0A    var createLanguageFile = function(languageName, languageData, jsFilePath) %7B%0A      var jsTemplate = 'videojs.addLanguage(%22' + languageName + '%22,' + JSON.stringify(languageData,null,' ') + ');';%0A%0A      grunt.file.write(jsFilePath, jsTemplate);%0A      grunt.log.writeln('- %5B' + languageName +'%5D Language Built. File %22' + jsFilePath + '%22 created.');%0A    %7D;%0A%0A
     this.fil
@@ -648,20 +648,8 @@
 ata,
- jsTemplate,
  jsF
@@ -768,54 +768,8 @@
 ) %7B%0A
-          console.log('-', f.src%5Bi%5D, f.dest);%0A
     
@@ -895,265 +895,89 @@
   js
-Template = 'videojs.addLanguage(%22' + languageName + '%22,' + JSON.stringify(languageData,null,' ') + ');';%0A          jsFilePath = path.join(f.dest, languageName + '.js');%0A          grunt.file.write(jsFilePath, jsTemplate);%0A          grunt.log.writeln('- %5B' + 
+FilePath = path.join(f.dest, languageName + '.js');%0A          createLanguageFile(
 lang
@@ -988,64 +988,34 @@
 Name
- +'%5D L
+, l
 anguage
- Built. File %22' + jsFilePath + '%22 created.'
+Data, jsFilePath
 );%0A 
@@ -1191,259 +1191,87 @@
   js
-Template = 'videojs.addLanguage(%22' + languageName + '%22,' + JSON.stringify(languageData,null,' ') + ');';%0A        jsFilePath = path.join(f.dest, languageName + '.js');%0A        grunt.file.write(jsFilePath, jsTemplate);%0A        grunt.log.writeln('- %5B' + 
+FilePath = path.join(f.dest, languageName + '.js');%0A        createLanguageFile(
 lang
@@ -1282,64 +1282,34 @@
 Name
- +'%5D L
+, l
 anguage
- Built. File %22' + jsFilePath + '%22 created.'
+Data, jsFilePath
 );%0A 

refactor code