'use strict';

var gulp = require('gulp');
var util = require('gulp-util');
var sass = require('gulp-sass');
var watch = require('gulp-watch');
var autoprefixer = require('gulp-autoprefixer');
var jsmin = require('gulp-jsmin');
var cssmin = require('gulp-cssmin');
var rename = require('gulp-rename');
var mainBowerFiles = require('main-bower-files');
// var jsonToSass = require('gulp-json-to-sass');
// var concat = require('gulp-concat');
// var through = require('through2');
var svgmin = require('gulp-svgmin');
// var fs = require('fs');
// var json2js = require('gulp-json2js');
// var data = require('gulp-data');

// var shared_variables = JSON.parse(fs.readFileSync('./assets/json/shared_variables.json'));

function handleError(err) {
  console.log(err.toString());
}

var config = {
  assetsDir : './assets',
  production: !!util.env.production
}

gulp.task('scripts', function () {
  gulp.src(['./assets/scripts/main.js']) // './assets/scripts/shared_variables.js',
    // .pipe(concat('main.js'))
    .pipe(config.production ? jsmin() : util.noop())
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest('./assets/dist/scripts/'));
});

gulp.task('styles', function () {
  gulp.src(['./assets/styles/app.scss'])
    .pipe(sass().on('error', sass.logError))
    .pipe(autoprefixer({
        browsers: ['last 2 versions'],
        cascade: false
    })).on('error', handleError)
    .pipe(config.production ? cssmin() : util.noop())
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest('./assets/dist/styles/'));
});

gulp.task('bower', function() {
    gulp.src(mainBowerFiles({
      "overrides":{
        "history.js":{
          "main":"./scripts/bundled/html4+html5/jquery.history.js"
        },
        "overlayScrollbars":{
          "main":[
            "./js/jquery.overlayScrollbars.min.js",
            "./css/OverlayScrollbars.min.css"
          ]
        },
        "masonry-layout":{
          "main":"./dist/masonry.pkgd.min.js"
        },
        "imagesloaded":{
          "main":"./imagesloaded.pkgd.min.js"
        },
        "jquery":{"ignore":true},
        "ev-emitter":{"ignore":true},
        "fizzy-ui-utils":{"ignore":true},
        "get-size":{"ignore":true},
        "outlayer":{"ignore":true},
      }
    }))
      .pipe(gulp.dest('./assets/dist/bower/'));
});

gulp.task('svg', function () {
  gulp.src(['./assets/img/*.svg', '!./assets/img/edlp-loader-anim.svg'])
    .pipe(svgmin())
    .pipe(gulp.dest('./assets/dist/img'));
  gulp.src('./assets/img/edlp-loader-anim.svg')
    .pipe(gulp.dest('./assets/dist/img'));
});

// default gulp task
// ,  'vars2sass' 'vars2js',
gulp.task('default', ['bower', 'scripts', 'styles', 'svg'], function() {
  // gulp.watch('./assets/json/*.json', ['vars2js', 'styles', 'scripts']); //, 'vars2sass'
  gulp.watch('./assets/styles/*.scss', ['styles']);
  gulp.watch('./assets/styles/base/*.scss', ['styles']);
  gulp.watch('./assets/scripts/*.js', ['scripts']);
  gulp.watch('./assets/img/*.svg', ['svg']);
});



// this task stays here just for memeomry about how to share variables between js and css

// gulp.task('vars2sass', function () {
//   gulp.src('./assets/json/shared_variables.json')
//     .pipe(jsonToSass({
//       jsonPath: './assets/json/shared_variables.json',
//       scssPath: './assets/styles/base/_shared_variables.scss'
//     }));
// });

// this one is not working, works first run but then it's like it's caching the source
// gulp.task('variables2js', function() {
//   gulp.src('./assets/scripts/shared_variables_model.js')
//     .pipe(rename('shared_variables.js'))
//     .pipe(data(function(file) {
//       delete require.cache['./assets/json/shared_variables.json'];
//       return require('./assets/json/shared_variables.json');
//     }))
//     .pipe(json2js())
//     .pipe(gulp.dest('./assets/scripts/'));
// });

// gulp.task('vars2js', function() {
//   gulp.src('./assets/json/shared_variables.json')
//     .pipe(rename('shared_variables.js'))
//     .pipe(through.obj(function (file, enc, cb) {
//       // console.log('file',String(file.contents));
//       // small pipe to wrappe json data into a js variable
//       file.contents = new Buffer("edlp_vars = "+String(file.contents).trim()+";");
//       cb(null, file);
//     }))
//     .pipe(gulp.dest('./assets/scripts/'));
// });
