// include gulp
/**
 * @type {Gulp}
 */
var gulp = require('gulp');
var path = require('path');
var browserSync = require('browser-sync').create();

// include plugins
var sass = require('gulp-sass');
var cssnano = require('gulp-cssnano');
var sourcemaps = require('gulp-sourcemaps');
var rename = require('gulp-rename');
var jade = require('gulp-jade');
var jadephp = require('gulp-jade-php');
var autoprefixer = require('gulp-autoprefixer');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var plumber = require('gulp-plumber');

var assets_path = 'assets/';
var content_path = 'www/';
var template_path = 'www/';
var localhost = 'medgis.loc';

// compile sass and css files
gulp.task('sass', function () {
    return gulp.src([
        'assets/styles/all.scss'
    ])
        .pipe(sourcemaps.init())
        .pipe(plumber())
        .pipe(sass().on('error', sass.logError))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(path.join(content_path, 'css/')))
        .pipe(browserSync.reload({
            stream: true
        }));
});
gulp.task('css', ['sass'], function () {
    return gulp.src(path.join(content_path, 'css/all.css'))
        .pipe(plumber())
        .pipe(sass().on('error', sass.logError))
        .pipe(autoprefixer({
            browsers: ['last 50 versions'],
            cascade: false
        }))
        .pipe(cssnano())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(path.join(content_path, 'css/')))
        .pipe(browserSync.reload({
            stream: true
        }));
});

// compile pug for php files
gulp.task('views', function () {
    return gulp.src([
        'assets/views/**/*.jade',
        '!assets/views/layouts/*.jade'
    ])
        .pipe(plumber())
        .pipe(jadephp({
            pretty: '\t'
        }))
        .pipe(gulp.dest(path.join(template_path)))
        .pipe(browserSync.reload({
            stream: true
        }));
});

// concatenate and minify js
gulp.task('scripts', function () {
    return gulp.src([
        //'assets/scripts/jquery.js',
        'assets/scripts/lightslider.js',
        // 'assets/scripts/lightgallery.js',
        'assets/scripts/moment.js',
        'assets/scripts/daterangepicker.js',
        'assets/scripts/dotdotdot.js',
        //'assets/scripts/formstyler.js',
        'assets/scripts/regionfilter.js',
        'assets/scripts/specfilter.js',
        'assets/scripts/comments.js',
        'assets/scripts/main.js'
    ])
        .pipe(plumber())
        .pipe(concat('all.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(path.join(content_path, 'js/')))
        .pipe(browserSync.reload({
            stream: true
        }));
});

// browser-sync task and settings
gulp.task('browserSync', ['css', 'scripts', 'views'], function () {
    browserSync.init({
        proxy: "http://medgis.loc/web/assets/app/www/",
        port: 8080,
    });
});

gulp.task('browserSync2', ['css', 'scripts', 'views'], function () {
    browserSync.init({
        proxy: "http://medgis.loc/web/www/",
        port: 8081,
    });
});

// start watchers
gulp.task('watch', ['browserSync'], function () {
    gulp.watch('assets/styles/**/*.scss', ['css']);
    gulp.watch('assets/views/**/*.jade', ['views']);
    gulp.watch('assets/scripts/**/*.js', ['scripts']);
    gulp.watch('**/*.php', browserSync.reload);
});
gulp.task('watchru', ['browserSync2'], function () {
    gulp.watch('assets/styles/**/*.scss', ['css']);
    gulp.watch('assets/views/**/*.jade', ['views']);
    gulp.watch('assets/scripts/**/*.js', ['scripts']);
    gulp.watch('**/*.php', browserSync.reload);
});
gulp.task('watch-files', function () {
    gulp.watch('assets/styles/**/*.scss', ['css']);
    gulp.watch('assets/views/**/*.jade', ['views']);
    gulp.watch('assets/scripts/**/*.js', ['scripts']);
});