'use strict';

/*
 * Module for handling various file-system related tasks
 */

angular.module('filehandler', [])
.factory('fileFactory', function ($window) {
    return {
        createDataDirIfNotExist: function () {
            var rejectErr = function (e) {
                reject(e);
            };

            if ($window.cordova) {
                return new Promise(function (resolve, reject) {

                    xwalk.experimental.native_file_system.requestNativeFileSystem('dcim', function (fs) {
                        fs.root.getDirectory('/dcim/soar-calls', { create: true }, function () {
                            resolve();
                        })
                    }, rejectErr)
                });
            }
        },
        emptyCallDataDir: function () {
            var self = this;

            return new Promise(function (resolve, reject) {
                var rejectErr = function (e) {
                    reject(e);
                };

                if ($window.cordova) {

                    xwalk.experimental.native_file_system.requestNativeFileSystem('dcim', function (fs) {
                        fs.root.getDirectory('/dcim/soar-calls', { create: false }, function (dir) {
                            dir.removeRecursively(function () {
                                return self.createDataDirIfNotExist();
                            }, rejectErr)
                        })
                    }, rejectErr);
                } else {
                    resolve('Dummy resolve');
                }
            });
        },
        writeToFile: function (opts) {
            return new Promise(function (resolve, reject) {
                var rejectErr = function (e) {
                    reject(e);
                };

                if ($window.cordova) {
                    xwalk.experimental.native_file_system.requestNativeFileSystem('dcim',
                    function (fs) {
                    fs.root.getFile('/dcim/soar-calls/' + opts.fileName, { create: true },
                           function (entry) {
                                entry.createWriter(function (writer) {
                                    writer.write(opts.data);
                                    resolve();
                                }, rejectErr)
                           }, rejectErr)
                    }, rejectErr);
                } else {
                    resolve('Dummy resolve');
                }
            });
        }
    };
});


@@ -228,94 +228,8 @@
     
-var rejectErr = function (e) %7B%0A                reject(e);%0A            %7D;%0A%0A            
 if (
@@ -310,16 +310,125 @@
 eject) %7B
+%0A                    var rejectErr = function (e) %7B%0A                        reject(e);%0A                    %7D;
 %0A%0A      

Move promise reject back to correct scope again