var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
var bowerMain = require('bower-main');
var del = require('del');
var uglify = require('gulp-uglify');
var uglifyCSS = require('gulp-uglifycss');
var flatten = require('gulp-flatten');
var rename = require('gulp-rename');
var rename = require('gulp-rename');
var jsonMinify = require('gulp-json-minify');
var concat = require('gulp-concat');
var concatCss = require('gulp-concat-css');
var less = require('gulp-less');

var bowerDirectory = 'bower_components';
var directoryAssets = "assets/";
var directoryJS = directoryAssets + "js";
var directoryCSS = directoryAssets + "css";
var directoryImages = directoryAssets + "img";
var directoryFonts = directoryAssets + "fonts";

gulp.task('JS', ['clean'], function() {
    var JS = bowerMain('js', 'min.js');
    gulp.src(JS.minified)
        .pipe(gulp.dest(directoryJS));

    gulp.src(JS.minifiedNotFound)
        .pipe(uglify())
        .pipe(rename({
            suffix: ".min"
        }))
        .pipe(gulp.dest(directoryJS));
    
    gulp.start('srcJS');
});

gulp.task('srcJS', function() {
    gulp.src('src/js/*.js')
        .pipe(uglify())
        .pipe(rename({
            suffix: ".min"
        }))
        .pipe(gulp.dest(directoryJS));
});

gulp.task('CSS', ['clean'], function() {
    var CSS = bowerMain('css', 'min.css');
    gulp.src(CSS.minified)
        .pipe(gulp.dest(directoryCSS));

    gulp.src(CSS.minifiedNotFound)
        .pipe(uglifyCSS())
        .pipe(rename({
            suffix: ".min"
        }))
        .pipe(gulp.dest(directoryCSS));

    gulp.src(mainBowerFiles('**/*.less'))
        .pipe(less())
        .pipe(uglifyCSS())
        .pipe(rename({
            suffix: ".min"
        }))
        .pipe(gulp.dest(directoryCSS));

    gulp.start('srcCSS');
});

gulp.task('srcCSS', function() {
    gulp.src('src/css/*.css')
        .pipe(uglifyCSS())
        .pipe(rename({
            suffix: ".min"
        }))
        .pipe(gulp.dest(directoryCSS));

    gulp.src('src/css/themes/*.css')
        .pipe(concatCss('themes.min.css'))
        .pipe(uglifyCSS())
        .pipe(gulp.dest(directoryCSS));
});


gulp.task('images', ['clean'], function() {
    gulp.src(mainBowerFiles('**/*.{jpg,png,jpeg,gif}'))
        .pipe(flatten())
        .pipe(gulp.dest(directoryImages));

    gulp.start('srcImages');
});

gulp.task('srcImages', function() {
    gulp.src('src/images/*.{jpg,png,jpeg,gif}')
        .pipe(flatten())
        .pipe(gulp.dest(directoryImages));
});

gulp.task('fonts', ['clean'], function() {
    gulp.src(mainBowerFiles('**/*.{otf,eot,svg,ttf,woff,woff2}'))
        .pipe(flatten())
        .pipe(gulp.dest(directoryFonts));

    gulp.start('srcFonts');
});


gulp.task('srcFonts', function() {
    gulp.src('src/fonts/*.{otf,eot,svg,ttf,woff,woff2}')
        .pipe(flatten())
        .pipe(gulp.dest(directoryFonts));
});

gulp.task('ckeditor', ['clean'], function() {
    gulp.src([bowerDirectory + '/ckeditor/adapters/**/*'])
        .pipe(gulp.dest(directoryJS + "/adapters"));

    //gulp.src([bowerDirectory + '/ckeditor/plugins/**/*'])
    //    .pipe(gulp.dest(directoryJS + "/plugins"));

    gulp.src([bowerDirectory + '/ckeditor/lang/**/*'])
        .pipe(gulp.dest(directoryJS + "/lang"));

    gulp.src([bowerDirectory + '/ckeditor/skins/**/*'])
        .pipe(gulp.dest(directoryJS + "/skins"));

    gulp.src([bowerDirectory + '/ckeditor/ckeditor.js', bowerDirectory + '/ckeditor/styles.js'])
        .pipe(gulp.dest(directoryJS));
});

gulp.task('colorpicker', ['clean'], function() {
    gulp.src([bowerDirectory + '/mjolnic-bootstrap-colorpicker/dist/**/*.{jpg,png,jpeg}'])
        .pipe(flatten())
        .pipe(gulp.dest(directoryImages + '/bootstrap-colorpicker'));
});

gulp.task('i18n', ['clean'], function() {
    gulp.src([bowerDirectory + '/**/i18n/*.js', bowerDirectory + '/**/lang/*.js', 'src/js/i18n/**/*.js', '!' + bowerDirectory + '/**/src/**', '!' + bowerDirectory + '/ckeditor/**', '!src/js/i18n/datatables/**'])
        .pipe(rename(function(path) {
            if (path.basename.indexOf(".min") == -1) path.basename += ".min";
        }))
        .pipe(uglify())
        .pipe(flatten({
            includeParents: 1
        }))
        .pipe(gulp.dest(directoryJS + "/i18n"));
    
    gulp.src(['src/js/i18n/datatables/*.js'])
        .pipe(rename(function(path) {
            if (path.basename.indexOf(".min") == -1) path.basename += ".min";
        }))
        .pipe(jsonMinify())
        .pipe(gulp.dest(directoryJS + "/i18n/datatables"));

    gulp.src(['src/js/i18n/**/*.js', '!src/js/i18n/datatables/*'])
        .pipe(rename(function(path) {
            if (path.basename.indexOf(".min") == -1) path.basename += ".min";
        }))
        .pipe(uglify())
        .pipe(flatten({
            includeParents: 1
        }))
        .pipe(gulp.dest(directoryJS + "/i18n"));
});

gulp.task('clean', function() {
    return del([directoryJS, directoryCSS, directoryImages, directoryFonts]);
});

gulp.task('bower', ['clean'], function() {
    gulp.start('JS');
    gulp.start('CSS');
    gulp.start('images');
    gulp.start('fonts');
    gulp.start('other');
});

gulp.task('other', ['clean'], function() {
    gulp.start('ckeditor');
    gulp.start('colorpicker');
    gulp.start('i18n');
});

gulp.task('src', function() {
    gulp.start('srcJS');
    gulp.start('srcCSS');
    gulp.start('srcFonts');
    gulp.start('srcImages');
});


gulp.task('watch', function() {
  gulp.watch("src/**/*", ['src']);
});



gulp.task('src&watch', ['src', 'watch']);

gulp.task('default', ['clean', 'bower']);
