/** @class */
jscape.StorageManagementService = jscape.ManagementService.extend(
    /** @lends {jscape.StorageManagementService.prototype} */
{
    listFiles: function(query) {
        return this._request("GET", "/rest/v2/files").send(query)
                .then(function(data) { return $.map(data || [], jscape.FileInfo.fromJSON); });
    },

    pathInfo: function(path) {
        return this._request("POST", "/rest/v2/files/info").options({global: false}).send(path)
                        .then(function(data) { return jscape.FileInfo.fromJSON(data); });
    },

    changeDir: function(path) {
        return this._request("POST", "/rest/v2/files/cwd").send(path)
                .then(function(data) { return jscape.FileInfo.fromJSON(data); });
    },

    makeDir: function(path) {
        return this._request("POST", "/rest/v2/files/mkdir").send(path);
    },

    startUpload: function() {
        return this._request("POST", "/rest/v2/files/upload", false).send();
    },

    uploadFile: function(file, mode, sid, metadata) {
        return this._request("POST", "/rest/v2/files/upload/{id}").uri({id: sid}).options({timeout: 0}).sendFormMultipart({file: file, mode: mode, metadata: metadata}, true);
    },

    uploadState: function(sid) {
        return this._request("GET", "/rest/v2/files/upload/{id}").uri({id: sid}).send()
                .then(function(data) { return jscape.UploadSessionInfo.fromJSON(data); });
    },

    cancelUpload: function(sid) {
        return this._request("POST", "/rest/v2/files/upload/{id}/cancel").uri({id: sid}).send();
    },

    renameFile: function(source, target) {
        return this._request("POST", "/rest/v2/files/rename?path={source}").uri({"source": source}).send(target)
                .then(function(data) { return jscape.FileInfo.fromJSON(data); });
    },

    moveFile: function(sources, target) {
        return this._request("POST", "/rest/v2/files/move?target={target}").uri({"target": target}).send(sources);
    },

    deleteFile: function(paths) {
        return this._request("DELETE", "/rest/v2/files").send(paths);
    },

    downloadFile: function(path) {
        return this._request("GET", "/rest/v2/files/dload").sendDownloadAndCheck({path: path}, this._request("GET", "/rest/v2/files/dload/state"));
    },

    zipDownload: function(paths) {
        return this._request("POST", "/rest/v2/files/zip").sendDownloadAndCheck({path: paths}, this._request("GET", "/rest/v2/files/zip/state"));
    },

    composeEmailFile: function(paths) {
        return this._request("POST", "/rest/v2/files/compose-email").send(paths)
                .then(function(data) { return jscape.EmailFileData.fromJSON(data); });
    },

    testPasswordCompliance: function (username, password) {
        return this._request("POST", "/rest/compliance/test-pwd").sendSync({account: username, password: password});
    },

    emailFile: function(data) {
        return this._request("POST", "/rest/v2/files/email").send(data);
    },

    tagsFor: function(path) {
        return this._request("GET", "/rest/v2/files/tags").send({path: path})
                .then(function(data) { return typeof data == "object" && data.hasOwnProperty("tags") ? data.tags : []});
    },

    tagFile: function(path, tags) {
        return this._request("POST", "/rest/v2/files/tags").send({path: path, tags: tags});
    },

    tagCloud: function() {
        return this._request("GET", "/rest/v2/files/tags/cloud").send()
                .then(function(data) { return jscape.TagCloudInfo.fromJSON(data); });
    },

    getUploadForms: function() {
        return this._request("GET", "/rest/v2/files/upload-forms").send()
                .then(function(data) { return $.map(data || [], jscape.UploadForm.fromJSON); });
    },

    uploadForm: function(name) {
        return this._request("GET", "/rest/v2/files/upload-forms/{name}").uri({name: name}).send()
                .then(function(data) { return jscape.UploadForm.fromJSON(data); });
    },

    getContacts: function(query) {
        return this._request("GET", "/rest/v2/contacts").options({global: false}).send(query)
                .then(function(data) { return data && $.isArray(data.records) ? data.records : []; });
    },

    addContact: function(data) {
        return this._request("POST", "/rest/v2/contacts").send(data);
    }
});