'use strict';

var fs = require('fs'),
    path = require('path'),
    rmdir = require('rimraf');

module.exports = {
    cleanDownloadFolder: function() {
        var downloadFolderPath;

        // deferred = protractor.promise.defer();

        downloadFolderPath = path.resolve(__dirname, '../downloads');

        console.log('Removing files in folder', downloadFolderPath);

        fs
            .readdirSync(downloadFolderPath)
            .map(function(file) {
                return path.resolve(downloadFolderPath, file);
            })
            .filter(function(file) {
                return !fs.statSync(file).isDirectory();
            })
            .forEach(function(file) {
                fs.unlinkSync(file);
            });
        console.log('File removed...');
        // deferred.resolve({});
        // return deferred.promise;
    },
    cleanScreenshotsFolder: function() {
        var screenshotsFolder;

        // deferred = protractor.promise.defer();

        screenshotsFolder = path.resolve(__dirname, '../screenshots');

        console.log('Removing files in folder', screenshotsFolder);

        fs
            .readdirSync(screenshotsFolder)
            .map(function(file) {
                return path.resolve(screenshotsFolder, file);
            })
            .forEach(function(file) {
                if (fs.statSync(file).isDirectory()) {
                    console.log('Removing folder...', file);
                    // return fs.rmdirSync(file);
                    return rmdir(file, function() {});
                }
                console.log('Removing file...', file);
                fs.unlinkSync(file);
            });
        console.log('File removed...');
        // deferred.resolve({});
        // return deferred.promise;
    },
    removeBrowserLogFile: function() {

        var screenshotsFolder;

        screenshotsFolder = path.resolve(__dirname, '../browser-output.log');
        fs.unlinkSync(screenshotsFolder);
        console.log('File removed...');

    }

};