'use strict';

var gulp = require('gulp');
var bSync = require('browser-sync');
var del = require('del');
var gulpLoadPlugins = require('gulp-load-plugins');

var plugins = gulpLoadPlugins();

var config = {
    dev: "src",
    prod: "build"
}
gulp.task('styles', function() {
  return gulp.src(config.dev +'/styles/**/*.scss')
    .pipe(plugins.changed(config.dev +'/css'))
    .pipe(plugins.plumber())
    .pipe(plugins.sass().on('error', plugins.sass.logError))
    .pipe(plugins.autoprefixer({
        browsers: ['last 2 versions'],
        cascade: false
      }))
    .pipe(plugins.minifyCss())
    .pipe(gulp.dest(config.dev +'/css'))
    .pipe(gulp.dest(config.prod +'/css'))
    .pipe(bSync.stream());

});

gulp.task('scripts', function(){
  return gulp.src('src/scripts/**/*.js')

    .pipe(gulp.dest('build/scripts'));
});

gulp.task('bower', function(){
  return gulp.src('src/bower_components/**/*')

    .pipe(gulp.dest('build/bower_components'));
});

gulp.task('php', ['styles'], function(){
  return gulp.src(config.dev +'/**/*.php')

    .pipe(gulp.dest(config.prod));
});

gulp.task('images', function(){
  return gulp.src(config.dev +'/images/**/*.{jpg,png,svg,gif,webp,ico}')

    .pipe(gulp.dest(config.prod +'/images'));
});

gulp.task('extras', function(){
  return gulp.src([
      config.dev +'/*.{png,txt,css,htaccess}',
      '!src/*.html'
    ], {
        dot: true
      }).pipe(gulp.dest('build'));
});

gulp.task('clean', del.bind(null, [config.dev +'/css', config.prod]));

gulp.task('browser-sync', function(){

  bSync.init({
    port: 3000,
    proxy: 'localhost/wbw-wordpress'
    
  });

  gulp.watch([
    config.dev +'/**/*.php',
    config.dev +'/styles/**/*.scss',
    config.dev +'/scripts/**/*.js',
    config.dev +'/images/**/*'
  ]).on('change', bSync.reload);

});

gulp.task('serve', ['styles', 'browser-sync'], function(){
  gulp.watch(config.dev +"/styles/**/*.scss", ['styles'])

});

gulp.task('build', ['styles', 'scripts', 'php', 'bower', 'images', 'extras'], function(){
  return gulp.src(config.prod +'/**/*')

});

gulp.task('default', ['clean'], function() {
  gulp.start('build');
});