/*
 * ditmer-embla
 * 
 *
 * Copyright (c) 2017 Ebbe Knudsen
 * 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.loadNpmTasks('grunt-postcss');
  grunt.loadNpmTasks('grunt-sass');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-mkdir');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-scss-lint');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-svg-sprite');
  grunt.loadNpmTasks('grunt-concurrent');
  grunt.loadNpmTasks('grunt-text-replace');
  grunt.loadNpmTasks("grunt-webpack");
  grunt.loadNpmTasks('grunt-svgmin');

  var setOptionsAndGruntConfig = function (thisTask, grunt) {

    var path = require("path");

    var options = thisTask.options({
      tempFolderPath: path.resolve(process.cwd() + "/embla-temp"),
      themeScssFilePath: path.resolve(process.cwd() + "/embla-variables-theme.scss"),
      emblaJsOutputPath: path.resolve(process.cwd() + "/dist/js"),
      emblaCssOutputPath: path.resolve(process.cwd() + "/dist/css"),
      emblaIconsOutputPath: path.resolve(process.cwd() + "/dist/icons"),
      emblaIconsTemplateOutputPath: "",
      emblaIllustrationsOutputPath: path.resolve(process.cwd() + "/dist/illustrations"),
      emblaIllustrationsTemplateOutputPath: "",
    });

    //Add to config, so we can use in other tasks
    grunt.config(thisTask.name, options);
  }

  grunt.registerTask("backup_original_config", "Remove original grunt config", function() {

    grunt.config.requires("getGruntConfigCallback");

    var originalConfig = grunt.config();

    var emblaOptionsConfigWithBackup = {
      ditmer_embla: originalConfig.ditmer_embla,
      build_dist: originalConfig.build_dist,

      getGruntConfigCallback: originalConfig.getGruntConfigCallback
    };

    grunt.initConfig(emblaOptionsConfigWithBackup); //Overwrite config
  });

  grunt.registerTask("restore_original_config", "Restore original grunt config", function() {

    grunt.task.requires("backup_original_config");

    var configWithBackup = grunt.config();

    grunt.initConfig(configWithBackup.getGruntConfigCallback()); //Overwrite config
  });


  grunt.registerTask("set_embla_config", "Set embla grunt config", function(runningEmblaTaskName) {

    var emblaTaskNameConfig = grunt.config(runningEmblaTaskName);
    var options = emblaTaskNameConfig;

    var emblaConfigGetter = require("../lib/grunt-task-configs.js");
    var emblaConfigs = emblaConfigGetter(options.tempFolderPath, options.themeScssFilePath, options.emblaJsOutputPath, options.emblaCssOutputPath, options.emblaIconsOutputPath, options.emblaIconsTemplateOutputPath, options.emblaIllustrationsOutputPath, options.emblaIllustrationsTemplateOutputPath);

    for(var emblaConfigPropertyName in emblaConfigs) {

      var existingConfig = grunt.config(emblaConfigPropertyName);

      var emblaConfig = emblaConfigs[emblaConfigPropertyName];
  
      grunt.config(emblaConfigPropertyName, emblaConfig);
    }


  });

  grunt.registerTask("embla_replace_icons", ["replace:replace-in-icons-cross-gray", "replace:replace-in-icons-cross-black", "replace:replace-in-icons-check-mark", "replace:replace-in-icons-success-dark", "replace:replace-in-icons-danger-dark", "replace:replace-in-icons-warning-dark", "replace:replace-in-icons-arrow-right", "replace:replace-in-icons-arrow-down"]);

  grunt.registerTask("handle_icons", [
    "copy:embla-rename-sketch-icons",
    "svgmin:embla-optimized-icons",
    "embla_replace_icons",
    "svg_sprite:embla-icons-to-sprite",
    "copy:embla-sprite-to-output",
  ]);

  grunt.registerTask("handle_illustrations", [
    "copy:embla-rename-sketch-illustrations",
    "svgmin:embla-optimized-illustrations",    
    "svg_sprite:embla-illustrations-to-sprite",
    "copy:embla-illustrations-sprite-to-output",
  ]);

  grunt.registerMultiTask('ditmer_embla', 'Create embla sass and icons', function() {
    
    grunt.config.requires("getGruntConfigCallback");

    setOptionsAndGruntConfig(this, grunt);

    var taskName = this.name;

    let tasksToRun = [
      "backup_original_config",
      "set_embla_config:" + taskName,
      "clean:embla-all",
      "handle_icons",
      "handle_illustrations"
    ];

    let emblaCssOutputPath = this.data.options.emblaCssOutputPath;
    let emblaThemeScssFilePath = this.data.options.themeScssFilePath;
    console.log("this.data.options.emblaCssOutputPath", this.data.options.emblaCssOutputPath);
    console.log("this.data.options.themeScssFilePath", this.data.options.themeScssFilePath);
    if(emblaCssOutputPath !== undefined && emblaCssOutputPath.length > 0 && emblaThemeScssFilePath !== undefined && emblaThemeScssFilePath.length > 0) {
      tasksToRun = tasksToRun.concat([
        "copy:embla-scss-theme-variables",
        "sass:embla-dist",
        "copy:embla-scss-theme-variables-clean",
        "postcss:embla-prod",
        "postcss:embla-dev",
      ]);
    }  


    let emblaIconsTemplateOutputPath = this.data.options.emblaIconsTemplateOutputPath;
    if(emblaIconsTemplateOutputPath !== undefined && emblaIconsTemplateOutputPath.length > 0) {
      tasksToRun.push("copy:embla-icons-template");
    }

    let emblaIllustrationsTemplateOutputPath = this.data.options.emblaIllustrationsTemplateOutputPath;
    if(emblaIllustrationsTemplateOutputPath !== undefined && emblaIllustrationsTemplateOutputPath.length > 0) {
      tasksToRun.push("copy:embla-illustrations-template");
    }

    tasksToRun = tasksToRun.concat([
      "clean:embla-temp",
      "restore_original_config"
    ]);

    grunt.task.run(tasksToRun);
  });

  grunt.registerTask("clean-temp", ["clean"]);

  grunt.registerMultiTask('build_dist', 'Build dist before publish', function() {
    
    setOptionsAndGruntConfig(this, grunt);

    var taskName = this.name;

    grunt.task.run([
      "backup_original_config",
      "set_embla_config:" + taskName,

      "clean:embla-dist",
      "clean:embla-all",
      "handle_icons",
      "handle_illustrations",

      "copy:embla-scss-theme-variables",
      "sass:embla-dist",
      "copy:embla-scss-theme-variables-clean",
      "postcss:embla-prod",
      "postcss:embla-dev",
      "webpack:embla-build-production",
      
      "clean:embla-temp",
      "restore_original_config"
      ]);
  });

};
