/*

 *  Copyright 2013 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.
 */

/**
 * This tool a search tool to search and edit reference table
 * @private
 */
Ext.define('Ametys.plugins.cms.content.tool.ReferenceTableEditionTool', {
	extend: "Ametys.plugins.cms.search.ContentSearchTool",
	
	/**
	 * @property {String} contentTypeId (required) The id of simple content type
	 */
	contentTypeId: null,
	
	/**
	 * @property {String} contentType The simple content type
	 */
	contentType: null,
	
	/**
	 * @property {Number} [workflowInitActionId=2] The workflow action to use when creating a new content in the grid
	 */
	workflowInitActionId: 1,
	
	/**
	 * @property {Number} [workflowEditActionId=2] The workflow action to use when editing the grid
	 */
	workflowEditActionId: 2,
	
	/**
	 * @property {String} [workflowName="reference-table"] The workflow name of simple contents in the grid
	 */
	workflowName: 'reference-table',
	
	/**
	 * @property {String} [contentLanguage="fr"] The language of simple contents in the grid
	 */
	contentLanguage: 'fr',
	
	/**
	 * @property {Number} _preventShowOutOfDate Counter to prevent #showOutOfDate when a content has been created
	 * @private
	 */
	_preventShowOutOfDate: 0,
	
	contentSearchTabCompatible: undefined,
	
	constructor: function(config)
	{
		this.callParent(arguments);
		
		this.allowAdditionalExtensions = false;
        
		Ametys.message.MessageBus.on(Ametys.message.Message.CREATED, this._onContentCreated, this);
        Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this._onContentModified, this);
	},
	
	/**
	 * @inheritdoc
	 */
	getMessageTargetRole: function()
	{
		return Ametys.message.MessageTarget.REFERENCE_TABLE_CONTENT;
	},
	
	/**
	 * @inheritdoc
	 */
	_createResultGrid: function ()
	{
		this.store = this.createStore();
		
		return Ext.create("Ametys.cms.content.EditContentsGrid", { 
			store: this.store,
			
			region: 'center',
			
			stateful: true,
			stateId: this.self.getName() + "$grid",
			
			columns: [],
			
			messageTarget: this.getMessageTargetRole(),
			
			selModel : {
				mode: 'MULTI'
			},
			
			viewConfig: {
				loadingText: "{{i18n plugin.cms:UITOOL_SEARCH_WAITING_MESSAGE}}",
                getRowClass: Ext.bind(this._getRowClass, this)
			},
			
			listeners: {
				'outofdate': Ext.bind(this.onOutOfDate, this, [false], false),
				'dirtychange': Ext.bind(this.onDirtyChange, this),
			    'itemdblclick': this.openContent
			}
		});
	},
    
    /**
     * @private
     * Determine a row CSS class depending upon the record
     * @param {Ext.data.Model} record The record
     * @return {String} The CSS classname to apply
     */
    _getRowClass: function(record)
    {
        let hasInvalidValue = false;
        
        this.grid.getColumns().forEach(function (column) {
            if (!Ametys.plugins.cms.search.SearchGridHelper.isValidValue(column, record))
            {
                hasInvalidValue = true;
                return;
            }
        });
        
        return hasInvalidValue ? "row-error" : "";
    },
	
	/**
	 * Listener when contents have been created
	 * Will add a new row to the grid and start edit the last inserted row if it was a utomatic creation
	 * @param {Ametys.message.Message} message The creation message.
	 * @private
	 */
	_onContentCreated: function (message)
	{
        var me = this;
		var targets = message.getTargets(function (target) {return Ametys.message.MessageTarget.REFERENCE_TABLE_CONTENT == target.getId() && Ext.Array.contains(target.getParameters().types, me.contentTypeId); });
		
		if (targets.length > 0)
		{
            if (message.getParameters() && message.getParameters().auto)
            {
                this.grid.getView().unmask();
	            this._preventShowOutOfDate += 1;
	            
	            var contents = [];
	            for (var i=0; i < targets.length; i++)
	            {
	                contents.push(targets[i].getParameters().content);
	            }
	            
	            this.addContents (contents);
            }
            else
            {
                this.showOutOfDate(false);
            }
		}
	},
    
    /**
     * Listener when contents have been modified
     * @param {Ametys.message.Message} message The creation message.
     * @private
     */
    _onContentModified: function (message)
    {
        var me = this;
        var targets = message.getTargets(function (target) {return Ametys.message.MessageTarget.REFERENCE_TABLE_CONTENT == target.getId() && Ext.Array.contains(target.getParameters().types, me.contentTypeId); });
        
        if (targets.length > 0)
        {
            this.showOutOfDate(false);
        }
    },
	
	/**
	 * @private
	 * Listener when the grid fired a "outofdate" event.
	 * Will set the tool in "out of date" mode if except if #_preventShowOutOfDate is not equal to 0.
	 */
	onOutOfDate: function ()
	{
		if (this._preventShowOutOfDate == 0)
		{
			this.showOutOfDate (false);
		}
		else
		{
			this._preventShowOutOfDate -= 1;
		}
	},
	
	/**
	 * Add the new contents to the grid
	 * @param {Ametys.cms.content.Content[]} contents The contents to add
	 */
	addContents: function (contents)
	{
		if (contents != null && contents.length > 0)
		{
			var records = [];
			for (var i=0; i < contents.length; i++)
			{
				var content = contents[i];
                
                var titleVariants = content.getTitleVariants();
				records.push(Ext.create(this._modelName, {
					id: content.getId(),
					title: titleVariants != null ? titleVariants : content.getTitle(),
                    'icon-glyph': this.contentType.getIconGlyph(),
					'icon-small': this.contentType.getIconSmall(),
					contributor :  content.getLastContributor(),
                    lastModified: content.getLastModified(),
                    contentLanguage: content.getLang()
				}));
			}
			
			var r = this.store.add(records);
			if (r.length > 0)
			{
				// Start edit the last inserted records
				Ext.defer (this._deferStartEdit, 200, this, [r[r.length - 1]]);
			}
		}
	},
	
	/**
	 * @private
	 * Listener when the grid fired a "itemdblclick" event.
	 * Will open the content in order to edit it if it's not simple. Do nothing otherwise.
	 */
	openContent: function (view, record)
	{
		// Open content if not simple
		if (!record.data.isSimple)
		{
            var toolParams = {'content-message-type': Ametys.message.MessageTarget.REFERENCE_TABLE_CONTENT}
			Ametys.cms.content.EditContentsGrid.openContent (record, {toolParams: toolParams});
		}
		
		// Otherwise do nothing
	},
	
	/**
	 * Start edit the record first cell
	 * @param {Ext.data.Model} record The record to edit
	 * @private
	 */
	_deferStartEdit: function (record)
	{
		this.grid.editingPlugin.startEdit(record, 0);
	},
	
	setParams: function(params)
	{
        this.callParent(arguments);
        
		this.workflowName = this.getParams().workflowName || this.workflowName;
		this.contentLanguage =  this.getParams().contentLanguage || this.contentLanguage;
		this.workflowInitActionId =  this.getParams().workflowInitActionId || this.workflowInitActionId;
		this.workflowEditActionId =  this.getParams().workflowEditActionId || this.workflowEditActionId;
		this.contentTypeId = this.getParams().contentType;
		
		this.contentType = Ametys.cms.content.ContentTypeDAO.getContentType (this.contentTypeId);
		
		this.setTitle(this.contentType.getLabel());
		this.setDescription(this.contentType.getDescription());
        
        if (this.contentType.getIconGlyph())
        {
            this.setGlyphIcon(this.contentType.getIconGlyph());
            this.setIconDecorator(this.contentType.getIconDecorator());
        }
        else
        {
            this.setGlyphIcon(null); // Cancel default icon
            this.setIconDecorator(null);
            this.setSmallIcon(this.contentType.getIconSmall());
			this.setMediumIcon(this.contentType.getIconMedium());
			this.setLargeIcon(this.contentType.getIconLarge());
        }
    },
	
    getEncodedSortersForExport: function()
    {
        return Ext.encode([{property: 'title', direction: 'ASC'}]);
    }
});