/* global require, console, module */

var Varnish = require('node-varnish'),
    Q = require('q'),
    _port = 6082,
    _secret,
    _host;

function _clean(host, port, secret) {
    'use strict';

    var _defer = Q.defer(),
        client;

    _host = host;
    _port = port;
    _secret = secret;

    client = new Varnish.VarnishClient(_host, _port, _secret);

    // console.log('Varnish...');
    // console.log(_host, _port, _secret);

    client.on('ready', function () {
        client.run_cmd('ban req.url ~ /', function () {
            console.log('Varnish...clear');
            _defer.resolve();
            client.close();
        });
    });

    client.on('error', function (err) {
        console.log('Error in clearing Varnish cache ' + err);
        _defer.resolve();
    });

    return _defer.promise;
}

module.exports = {
  clean: _clean
};
