/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2013-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*/

define([
    'jquery',
    'underscore',
    'backbone',
    'ccweb.files.extract/views/popup/BasePopupView',
    'ccweb.files.extract/controllers/SelectionController',
    'ccweb.files.extract/models/ColorUsageModel',
    'ccweb.files.extract/utils/StyleUtil',
    'ccweb.files.extract/utils/TemplateUtil',
    'ccweb.files.extract/views/inspect/ContextMenuView',
    'text!ccweb.files.extract/views/templates/colorChipPopupTemplate.html',
    'text!ccweb.files.extract/views/templates/alphaListPopupTemplate.html',
    'ccweb.files.extract/Constants',
    'parfait/ColorChipPopupView'
], function ($, _, Backbone, BasePopupView, SelectionController, ColorUsageModel,
    StyleUtil, TemplateUtil, ContextMenuView, ColorChipPopupTemplate, AlphaListPopupTemplate, Constants, ColorChipPopupView) {
    'use strict';

    var DwColorChipPopupView = ColorChipPopupView.extend({
        resetContextMenu: function () {
            if (this.colorFormatMenu) {
                this.colorFormatMenu.off('show', this.handleColorFormatMenuShow, this);
                this.colorFormatMenu.off('selection', this.handleColorFormatSelectionChange, this);
                this.colorFormatMenu.remove();
                this.colorFormatMenu = null;
            }
        },
        
        handleColorFormatChange: function (newColorFormat) {
            this.updateColorString(newColorFormat);
            graphite.events.trigger('color-format', {formatSubEvent: [newColorFormat]});
        },
        
        createColorFormatMenu: function () {
            var colorFormatItems = [],
                colorFormatLabels = {};

            _.each(Constants.ColorFormat, function (details, key) {
                colorFormatLabels[key] = details;
                colorFormatItems.unshift(key);
            });
            
            this.resetContextMenu();
            this.colorFormatMenu = new ContextMenuView({
                name : 'colorFormat-selector',
                $toggle : this.$('div.color-format'),
                $after : this.$('div.color-format'),
                items : colorFormatItems,
                position : Constants.MenuPosition.BELOW
            });
            this.colorFormatMenu.labels = colorFormatLabels;
            this.$('.color-format').addClass('menu-active');
            this.colorFormatMenu.on('show', this.handleColorFormatMenuShow, this);
            this.colorFormatMenu.on('selection', this.handleColorFormatSelectionChange, this);
        },
        
        handleShowColor: function (params) {
            if (!this.colorUsage ||
                    !this.$el.is(':visible') ||
                    !StyleUtil.areUsageStylesEqual(params.style, this.colorUsage)) {
                this.colorUsage = params.style;
                this.currentAlpha = null;
                this.handleShow({sourceElement: params.sourceElement});
                this.updateColorString(Constants.ColorFormat[this.colorFormat]);
                this.initAlphaList();
                this.$el.find("textarea.topcoat-textarea").focus();
            }
        },
        
        remove: function () {
            this.removeEventListeners();
            this.resetContextMenu();
            BasePopupView.prototype.remove.apply(this, arguments);
        }

    });
    
    return DwColorChipPopupView;
    
});