'use strict';

var path = require('path'),
    q = require('q'),
    bs = require("browser-sync").create('Mufasa'),
    timon = require('zazu-timon'),
    shell = require('shelljs'),
    bsConfig = require(path.join(__dirname, '../bs-config.js'));

// init tmp directory tree
shell.mkdir('-p', './tmp/assets/css');

bsConfig.files.push(
    {
        match: ["app/*.html"],
        fn: function (event, file) {
            bs.reload(file);
        }
    }
);

bsConfig.files.push({
    match: ["app/**/*.less"],
    fn: function (event, file) {
        timon.less(__dirname + '/../app/assets/less/app.less', 'app.css', __dirname + '/../tmp').then(function () {
            bs.reload(file);
        }, function (err) {
            console.error(err);
        });
    }
});

bsConfig.files.push({
    match: ['app/**/*.js'],
    fn: function (event, file) {
        timon.browserify(__dirname + '/../app/src/app.js', 'app.js', __dirname + '/../tmp').then(function () {
            bs.reload(file);
        }, function (err) {
            console.error(err);
        });
    },
    options: {
        ignored: '*.txt'
    }
});

q.all([
    timon.less(__dirname + '/../app/assets/less/app.less', 'app.css', __dirname + '/../tmp'),
    timon.browserify(__dirname + '/../app/src/app.js', 'app.js', __dirname + '/../tmp')
]).then(function () {
    console.log('Generated app.css and app.js');

    bs.init(bsConfig);
});

