'use strict';

function escape(string) {
    return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}

function parseQueryString(qs) {
    var params = {};
    var parts = qs.split('&');
    for (var i = 0; i < parts.length; i++) {
        var keyvalue = parts[i].split('=');
        var key = decodeURIComponent(keyvalue[0]);
        var value = decodeURIComponent(keyvalue[1]);
        params[key] = value;
    }
    return params;
}

function accountUrl() {
    var path = window.location.pathname;
    return path.split('/').slice(0, 3).join('/');
}

window.getFromInjector = function(service) {
    var html = document.querySelector("html");
    var injector = angular.element(html).injector();
    return injector.get(service);
};


function SwiftSimulator($httpBackend) {
    this.containers = [];
    this.objects = {};

    var prefix = escape(accountUrl() + '/');
    this.listRegex = new RegExp(prefix + '(.*?)' + escape('?') + '(.*)');
    var objRegex = new RegExp(prefix + '(.*?)' + escape('/') + '(.*)');

    $httpBackend.whenGET(accountUrl() + '?format=json')
        .respond(this.listContainers.bind(this));

    function deleteObject(method, url, data) {
        var match = url.match(objRegex);
        var container = match[1];
        var name = match[2];

        var objects = this.objects[container];
        if (objects == undefined) {
            return [404, 'Container "' + container + '" not found'];
        }

        for (var i = 0; i < objects.length; i++) {
            if (objects[i].name == name) {
                objects.splice(i, 1);
                return [204, null];
            }
        }
        return [404, 'Not Found'];
    }

    function putObject(method, url, data) {
        var match = url.match(objRegex);
        var container = match[1];
        var name = match[2];

        var objects = this.objects[container];
        if (objects == undefined) {
            return [404, 'Container "' + container + '" not found'];
        }

        var lastModified = data.lastModifiedDate.toISOString();
        var object = {name: name,
                      bytes: data.size,
                      'last_modified': lastModified,
                      'content_type': 'application/octet-stream',
                      hash: ''};
        // Remove object if it's already there
        for (var i = 0; i < objects.length; i++) {
            if (objects[i].name == name) {
                objects.splice(i, 1);
            }
        }
        objects.push(object);
        return [201, null];
    }

    $httpBackend.whenGET(this.listRegex)
        .respond(this.listObjects.bind(this));
    $httpBackend.whenDELETE(objRegex).respond(deleteObject.bind(this));
    $httpBackend.whenPUT(objRegex).respond(putObject.bind(this));
    $httpBackend.whenGET(/.*/).passThrough();
}

SwiftSimulator.prototype.listContainers = function(method, url, data) {
    return [200, this.containers];
};

SwiftSimulator.prototype.listObjects = function(method, url, data) {
    var defaults = {prefix: '', delimiter: null};
    var match = url.match(this.listRegex);
    var container = match[1];
    var qs = match[2];
    var params = angular.extend(defaults, parseQueryString(qs));
    var prefix = params.prefix;
    var delimiter = params.delimiter;
    var results = [];
    var subdirs = {};
    var objects = this.objects[container];
    if (objects == undefined) {
        return [404, 'Container "' + match[1] + '" not found'];
    }

    for (var i = 0; i < objects.length; i++) {
        var object = objects[i];
        var name = object.name;
        if (name.indexOf(prefix) == 0) {
            var idx = name.indexOf(delimiter, prefix.length);
            if (idx > -1) {
                var subdir = name.slice(0, idx + 1);
                if (!subdirs[subdir]) {
                    results.push({subdir: subdir});
                    subdirs[subdir] = true;
                }
            } else {
                results.push(object);
            }
        }
    }
    return [200, results];
};

SwiftSimulator.prototype.setContainers = function(containers) {
    this.containers = containers;
};

SwiftSimulator.prototype.setObjects = function(container, objects) {
    this.objects[container] = objects;
};

angular.module('swiftBrowserE2E', ['ngMockE2E'])
    .service('swiftSim', SwiftSimulator);

@@ -949,20 +949,21 @@
 ');%0A    
-var 
+this.
 objRegex
@@ -1130,557 +1130,8 @@
 );%0A%0A
-    function deleteObject(method, url, data) %7B%0A        var match = url.match(objRegex);%0A        var container = match%5B1%5D;%0A        var name = match%5B2%5D;%0A%0A        var objects = this.objects%5Bcontainer%5D;%0A        if (objects == undefined) %7B%0A            return %5B404, 'Container %22' + container + '%22 not found'%5D;%0A        %7D%0A%0A        for (var i = 0; i %3C objects.length; i++) %7B%0A            if (objects%5Bi%5D.name == name) %7B%0A                objects.splice(i, 1);%0A                return %5B204, null%5D;%0A            %7D%0A        %7D%0A        return %5B404, 'Not Found'%5D;%0A    %7D%0A%0A
     
@@ -1200,16 +1200,21 @@
 l.match(
+this.
 objRegex
@@ -2117,16 +2117,21 @@
 nDELETE(
+this.
 objRegex
@@ -2123,32 +2123,41 @@
 E(this.objRegex)
+%0A        
 .respond(deleteO
@@ -2149,16 +2149,21 @@
 respond(
+this.
 deleteOb
@@ -2205,16 +2205,21 @@
 whenPUT(
+this.
 objRegex
@@ -3513,32 +3513,550 @@
 , results%5D;%0A%7D;%0A%0A
+SwiftSimulator.prototype.deleteObject = function(method, url, data) %7B%0A    var match = url.match(this.objRegex);%0A    var container = match%5B1%5D;%0A    var name = match%5B2%5D;%0A%0A    var objects = this.objects%5Bcontainer%5D;%0A    if (objects == undefined) %7B%0A        return %5B404, 'Container %22' + container + '%22 not found'%5D;%0A    %7D%0A%0A    for (var i = 0; i %3C objects.length; i++) %7B%0A        if (objects%5Bi%5D.name == name) %7B%0A            objects.splice(i, 1);%0A            return %5B204, null%5D;%0A        %7D%0A    %7D%0A    return %5B404, 'Not Found'%5D;%0A%7D;%0A%0A
 SwiftSimulator.p

make deleteObject a method