/// class PD.Manage.Services.ResourcesService

PD.Manage.Services.ResourcesService = Class.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    addFiles: function (folderId, files, fileMode, success, error) {

        var that = this;
        var additions = [];

        var addFile = function (folderId, file) {
            return new RSVP.Promise(function (resolve, reject) {
                that.addFile(folderId, file, fileMode, resolve, function (e) {
                    
                    var errs = e.responseJSON != null ? e.responseJSON : [],
                        err = {};

                    if (errs) {
                        err = _.first(errs);
                    }

                    e.errorHandled = true;

                    reject({
                        code: err.code || 0,
                        file: file
                    });

                });
            });
        };

        var handleAddFileErrors = function (results) {

            var rejected = _.filter(results, function (result) {
                return result.state === "rejected";
            });

            rejected = _.map(rejected, function (item) {
                return item.reason;
            });

            var fulfilled = _.filter(results, function (result) {
                return result.state === "fulfilled";
            });
            
            fulfilled = _.map(fulfilled, function (item) {
                return item.value;
            });

            if (rejected.length > 0) {

                var unique = _.uniqBy(rejected, false, function (item) {
                    return item.code;
                });

                var uniqueCodes = _.map(unique, function (item) {
                    return item.code;
                });

                error({
                    folderId: folderId,
                    failed: rejected,
                    succeeded: fulfilled,
                    errorCodes: uniqueCodes
                });

            }
            else {
                success(fulfilled);
            }
            
        };
        
        _.each(files, function (file, index) {
            additions.push(addFile(folderId, file));
        });

        RSVP.allSettled(additions)
            .then(handleAddFileErrors);

    },

    deleteResources: function (ids, success, error) {

        if (!_.isArray(ids)) {
            ids = [ids];
        }

        PD.ajax({
            type: "DELETE",
            url: PD.Path.resolve("~/Manage/Resources/DeleteResources"),
            data: {
                ids: JSON.stringify(ids)
            },
            success: function (e) {
                if (success) {
                    success(e);
                }
            },
            error: error
        });

    },

    addFolder: function (parentId, name, success, error) {

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/Manage/Resources/AddFolder"),
            data: {
                name: uuid.v4()
            },
            success: function (e) {
                if (success) {
                    success(e);
                }
            },
            error: error
        });

    },

    getFolders: function (parentId, success, error) {

        PD.ajax({
            type: "GET",
            url: PD.Path.resolve("~/Manage/Resources/GetFolders"),
            data: {
                parentId: parentId
            },
            success: function (e) {
                if (success) {
                    success(e);
                }
            },
            error: error
        });

    },

    getPackage: function (folderId, success, error) {

        PD.Web.Utilities.DownloadManager.download("~/Manage/Resources/GetPackage", {
            folderId: folderId
        });

    },

    addPackage: function (uploadId, folderId, success, error) {

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/Manage/Resources/AddPackage"),
            data: {
                uploadId: uploadId,
                folderId: folderId
            },
            success: success,
            error: error
        });

    },

    moveResources: function (destinationId, sourceIds, success, error) {

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/Manage/Resources/MoveResources"),
            data: {
                destinationId: destinationId,
                sourceIds: JSON.stringify(sourceIds)
            },
            success: success,
            error: error
        });

    },

    saveFile: function (fileId, fileContent, success, error) {

        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/Manage/Resources/SaveFile"),
            data: {
                id: fileId,
                content: fileContent
            },
            success: function (e) {
                if (success) {
                    success(e);
                }
            },
            error: error
        });

    },

    addFile: function (folderId, file, fileMode, success, error) {
        
        PD.ajax({
            type: "POST",
            url: PD.Path.resolve("~/Manage/Resources/AddFile"),
            data: {
                uploadId: file.target_name,
                fileMode: fileMode,
                file: JSON.stringify({
                    name: file.name,
                    parentId: folderId
                })
            },
            success: success,
            error: error
        });

    }

});