/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2014 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 */
/*global define: true, graphite: true, Image: true, setTimeout: true, mixpanel: true*/

define(["jquery", 'serverAPI', 'controllers/AuthController', 'DWPSDExtractManager', 'ccweb.files.extract/controllers/SelectionController', 'dwconfig'], function ($, serverAPI, AuthController, DWPSDExtractManager, SelectionController, dwconfig) {
    "use strict";
    var apiString = 'api/v1/';
    var DWPSDExtractSelectionHandler = {
        currentSelectionLayer: null,
        
        updateSelection: function (newSelection) {
            this.currentSelection = newSelection;
            this.updateCssAsNeeded();
        },
        
        getUpdatedCSSForLayerModel: function (layerModel) {
            var curCSS = layerModel.getCSSAsString(true, 'css');
            var layerName = layerModel.get("layerName");

            // Create a dummy layer name in case it's empty
            if (!layerName) {
                layerName = "layer" + layerModel.get("layerId");
            }
            
            var codeHintInfo = {
                cssString: curCSS,
                imagePreviewName: DWPSDExtractManager.normalizeFileName(layerName)
            };
            return codeHintInfo;
        },

        getImageDataForLayerModel: function (layerModel) {
            var imageData = {
                bounds: {
                    top: 0,
                    bottom: 0,
                    left: 0,
                    right: 0
                },
                url: "",
                color: {
                    opacity: "",
                    red: "",
                    green: "",
                    blue: ""
                }
            },
                psdModel = SelectionController.getPSDModel();
            if (window.dwData) {
                var spriteSheet = layerModel.get("spriteSheet");
                if (!spriteSheet) {
                    return imageData;
                }
                if (spriteSheet.bounds) {
                    imageData.bounds = spriteSheet.bounds;
                    var path = "files",
                        pathSeparator = '/',
                        md5 = psdModel.get("md5"),
                        layerCompId = psdModel.get("layerCompId"),
                        sheetID = spriteSheet.sheetID || '',
                        jsonVersion = window.graphite.spritesheetJsonVersion,
                        welcomeExp = psdModel.get("welcomeExp");

                    if (!welcomeExp) {
                        jsonVersion = jsonVersion.replace(/\./g, "_"); //Replace all the '.'s with '_'
                        path = window.dwData.configPath + path + pathSeparator;
                        path +=  md5 + "/spritesheets/v_" + jsonVersion + pathSeparator +  "sheet" + sheetID + "_" + layerCompId + ".png";
                    } else {
                        path = window.dwData.programConfigPath + pathSeparator + dwconfig.WELCOME_EXP_PATH + pathSeparator;
                        path +=  md5 + "/sheet" + sheetID + "_" + layerCompId + ".png";
                    }

                    imageData.url = path;
                } else {
                    var data = {opacity: 0, red: 0, green: 0, blue: 0};
                    imageData.bounds = layerModel.get("bounds");
                    if (spriteSheet.color) {
                        data = spriteSheet.color;
                        data.opacity = 0;
                    }
                    if (spriteSheet.opacity) {
                        data.opacity = spriteSheet.opacity;
                    }
                    imageData.color = data;
                }
                return imageData;
            }
        },

        updateCssAsNeeded: function () {
            var codeHintInfo = {
                cssString: "",
                imagePreviewName: ""
            };
            var imageData = {
                bounds: {
                    top: 0,
                    bottom: 0,
                    left: 0,
                    right: 0
                },
                url: "",
                color: {
                    opacity: "",
                    red: "",
                    green: "",
                    blue: ""
                }
            };
            if (this.currentSelection && this.currentSelection.length === 1) {
                if (this.currentSelection[0]) {
                    codeHintInfo = this.getUpdatedCSSForLayerModel(this.currentSelection[0]);
                    imageData = this.getImageDataForLayerModel(this.currentSelection[0]);
                }
            }
            DWPSDExtractManager.updateCSSForSelection(codeHintInfo, imageData);
        }
        
    };

    return DWPSDExtractSelectionHandler;
});
