/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, bitwise: true */
/*global define: true, graphite: true, Image: true, window: true*/

define([
    "jquery",
    "underscore",
    "backbone",
    'ccweb.files.extract/models/PSDModel',
    'ccweb.files.extract/Constants',
    "parfait/AssetController"
], function ($, _, Backbone, PSDModel, Constants, AssetController) {
    "use strict";
    var DwAssetController = $.extend(AssetController, {
        getAsset: function (assetId, md5, layerCompId, successCallback, errorCallback, context, path, welcomeExp) {
            var localContent = context ? context.localContent : false, //not removed as it will be reused later.
                psdModel = new PSDModel({id: assetId,  md5: md5, layerCompId: layerCompId, path: path, welcomeExp: welcomeExp}),
				errorCallbackWrapper = function (response, err) {
                    var status = response.status;
                    if (err) {
                        if (err.errorCode === Constants.WorkerErrorCodes.GRAPHITE_REJECTED) {
                            status = 422; // 'Unprocessable entity' was the nearest I could find :-)
                        }
                        if (err.errorCode === Constants.WorkerErrorCodes.GRAPHITE_PROCESSING) {
                            status = 500;
                        }
                    }
                    if (status >= 300) {
                        psdModel.set('errorInfo', err);
                        psdModel.set('status', status);
                    }
                    errorCallback.apply(context, [response, err]);
                };

            if (path) {
                graphite.serverAPI.loadPsdModelFromPath(psdModel, path, successCallback, errorCallbackWrapper, context);
            }
            return psdModel;
        }
    });
    return DwAssetController;
});
