'use-strict';

//////////////////////////////
// Requires
//////////////////////////////
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var gulp = require('gulp');
var gulpif = require('gulp-if');
var jshint = require('gulp-jshint');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var stylish = require('jshint-stylish');

//////////////////////////////
// Variables
//////////////////////////////

var dirs = {
  'images': 'dev/images/*.{png,jpg,jpeg}',
  'markdown': 'dev/patterns/**/*.md',
  'sass': {
    'main': 'dev/dev.scss',
    'patterns': 'dev/patterns/**/*.scss',
    'lint': [
      'dev/patterns/**/*.scss',
      'dev/dev.scss',
      '!dev/*.css'
    ]
  },
  'js': {
    'lint': [
      'Gulpfile.js',
      '*.json',
      'dev/dev.js',
      'dev/patterns/**/package.json'
    ]
  },
  'html': {
    'reload': [
      'dev/index.html',
      'dev/patterns/**/html/*.html'
    ]
  }
};

//////////////////////////////
// BrowserSync
//////////////////////////////

gulp.task('browser-sync', function() {
  browserSync.init({
    logPrefix: "Pattern Library",
    server: {
      baseDir: "./dev"
    }
  });
});

//////////////////////////////
// HTML Tasks
//////////////////////////////

gulp.task('html:reload', function() {
  gulp.watch(dirs.html.reload).on('change', browserSync.reload);
});

//////////////////////////////
// JavaScript Tasks
//////////////////////////////

gulp.task('jshint', function() {
  gulp.src(dirs.js.lint)
    .pipe(plumber())
    .pipe(jshint())
    .pipe(jshint.reporter(stylish));
});

gulp.task('jshint:watch', function() {
  gulp.watch(dirs.js.lint, ['jshint']);
});

gulp.task('js:reload', function() {
  gulp.watch(dirs.js.lint).on('change', browserSync.reload);
});


//////////////////////////////
// Sass Tasks
//////////////////////////////

gulp.task('sass', function() {
  gulp.src(dirs.sass.main)
    .pipe(sass().on('error', sass.logError))
    .pipe(autoprefixer({
      browsers: ['> 1%', 'last 2 versions']
    }))
    .pipe(gulp.dest('dev'))
    .pipe(browserSync.stream());
});

gulp.task('sass:watch', function() {
  gulp.watch([dirs.sass.main, dirs.sass.patterns], ['sass']);
});

//////////////////////////////
// Build Tasks
//////////////////////////////

gulp.task('build:dist', function() {
  gulp.src([dirs.sass.patterns, dirs.markdown])
    .pipe(gulp.dest('dist/patterns'));

  gulp.src(dirs.sass.main)
    .pipe(rename('_pattern-library.scss'))
    .pipe(gulp.dest('dist'));

  gulp.src(dirs.images)
    .pipe(gulp.dest('dist/images'));
});

//////////////////////////////
// Running Tasks
//////////////////////////////

gulp.task('build', ['sass', 'build:dist']);

gulp.task('watch', ['sass:watch', 'jshint:watch', 'html:reload', 'js:reload']);

gulp.task('default', ['browser-sync', 'build', 'watch']);

@@ -508,16 +508,34 @@
 //////%0A%0A
+var env = 'dev';%0A%0A
 var dirs
@@ -2170,16 +2170,205 @@
 ream());
+%0A%0A  gulp.src(%5Bdirs.sass.patterns, dirs.markdown%5D)%0A    .pipe(gulp.dest('dist/patterns'));%0A%0A  gulp.src(dirs.sass.main)%0A    .pipe(rename('_pattern-library.scss'))%0A    .pipe(gulp.dest('dist'));
 %0A%7D);%0A%0Agu
@@ -2507,13 +2507,13 @@
 %0A// 
-Build
+Image
  Tas
@@ -2558,26 +2558,21 @@
 p.task('
-build:dist
+image
 ', funct
@@ -2583,197 +2583,8 @@
 ) %7B%0A
-  gulp.src(%5Bdirs.sass.patterns, dirs.markdown%5D)%0A    .pipe(gulp.dest('dist/patterns'));%0A%0A  gulp.src(dirs.sass.main)%0A    .pipe(rename('_pattern-library.scss'))%0A    .pipe(gulp.dest('dist'));%0A%0A
   gu
@@ -2758,18 +2758,13 @@
 ', '
-build:dist
+image
 '%5D);

Move all sass globs to sass task