/*
 *  Copyright 2010 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Web-specific content search tool.
 */
Ext.define('Ametys.plugins.web.search.ContentSearchTool', {
    extend: 'Ametys.plugins.cms.search.ContentSearchTool',
    
    statics:
    {
        /**
         * Convert a site value for model.
         * @param {String/Object/Object[]} value If it is an object, it will return the 'site' property.  
         * @param {String} value.name The site name
         * @param {Ext.data.Model} record The record
         * @param {String} dataIndex The model data index
         * @return {String/String[]} The site name(s) only
         */
        convertSite: function(value, record, dataIndex)
        {
            var convertedValue = value,
                properties = {};
            record.data[dataIndex + '_site'] = properties;
            
            function fillProperties(obj)
            {
                properties[obj.name] = obj;
            }
            
            if (Ext.isArray(value))
            {
                Ext.Array.forEach(value, fillProperties);
                convertedValue = Ext.Array.map(value, function(item) {return item.name;});
            }
            else if (Ext.isObject(value) && !Ext.Object.isEmpty(value))
            {
                fillProperties(value);
                convertedValue = value.name
            }
            else
            {
            	convertedValue = "";
            }
            return convertedValue;
        },
        
        /**
         * Render a site in the results grid.
         * @param {Object} value The data value
         * @param {Object} metaData A collection of metadata about the current cell
         * @param {Ext.data.Model} record The record
         * @param {Number} rowIndex The index of the current row
         * @param {Number} colIndex The index of the current column
         * @param {Ext.data.Store} store The data store
         * @param {Ext.view.View} view The current view
         * @param {String} dataIndex The data index of the column
         */
        renderSite: function(value, metaData, record, rowIndex, colIndex, store, view, dataIndex)
        {
        	if (Ext.isEmpty(value))
		    {
		        return '';
		    }
            
            dataIndex = dataIndex || rowIndex; // When used by grouping feature, data index is the 4th arguments
        	
            var properties = record.get(dataIndex + '_site');
            value = Ext.Array.from(value);
            
            if (properties)
            {
                var mappedProperties = Ext.Array.map(value, function(val) {return properties[val];});
                return Ext.Array.map(mappedProperties, function(property) { 
                    return property.title + ' (' + property.name + ')';
                }).join('<br/>');
            }
            else
            {
                return value.join('<br/>');
            }
        },
        
        /**
         * Convert a shared content for model.
         * @param {String/Object[]} value If it is an object, it will return the 'shared' property.  
         * @param {String} value.isShared true if the content is a shared content.
         * @param {Object} value.originalSite The properties of original site
         * @param {Ext.data.Model} record The record
         * @param {String} dataIndex The model data index
         * @return {String} The language code only
         */
        convertSharedContent: function(value, record, dataIndex)
        {
            if (Ext.isObject(value))
            {
                if (value.originalSite)
                {
                    record.data[dataIndex + '_originalSite'] = value.originalSite;
                }
                value = value.isShared;
            }
            return value;
        },
        
        /**
         * Render the shared status of a content in the results grid.
         * @param {Object} value The data value
         * @param {Object} metaData A collection of metadata about the current cell
         * @param {Ext.data.Model} record The record
         * @param {Number} rowIndex The index of the current row
         * @param {Number} colIndex The index of the current column
         * @param {Ext.data.Store} store The data store
         * @param {Ext.view.View} view The current view
         * @param {String} dataIndex The data index of the column
         */
        renderSharedContent: function(value, metaData, record, rowIndex, colIndex, store, view, dataIndex)
        {
        	var isShared = Ext.isBoolean(value) ? value : value == 'true';
			if (isShared)
			{
                var originalSite = record.get(dataIndex + '_originalSite');
                if (originalSite == null || Ext.isEmpty(originalSite.name))
                {
                	return '<span class="a-grid-glyph ametysicon-check34" title="' + "{{i18n plugin.core-ui:PLUGINS_CORE_UI_GRID_COLUMN_BOOLEAN_YES}}" + '"></span>';
                }
                else
                {
                	var sharedSite = originalSite.title + ' (' + originalSite.name + ')';
                	return '<span class="a-grid-glyph ametysicon-share40" title="' + "{{i18n UITOOL_SEARCH_CONTENT_SHARED_FROM_SITE}}" + '"></span>' + sharedSite;
                }
			}
			else
			{
				return "";
			}
        }
    }
});