/*
 *  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 singleton the object class associated to the file type 'resource' in {@link Ametys.form.widget.File} widget.
 * It is used to pick up a file in the resources explorer.
 * See {@link Ametys.form.widget.File} widget.
 */
Ext.define('Ametys.cms.widget.File.Resource', {
	extend: 'Ametys.form.widget.File.FileSource',
	
	statics: {
		/**
		 * @property {String} SOURCE The file source which belongs to this class
		 * @readonly
		 * @static
		 */
		SOURCE: 'resource',
		/**
		 * @property {String} TYPE The file type which belongs to this class
		 * @readonly
		 * @static
		 */
		TYPE: 'explorer',
		
		/**
		 * The filters configuration
		 * @private
		 */
		filters: {
			none: {
				buttonText: "",
				buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_FILE_BTN}}",
				buttonIconCls: 'ametysicon-document9 decorator-ametysicon-world91',
				
				menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_FILE}}",
				menuItemIconCls: 'ametysicon-document9 decorator-ametysicon-world91',
				
				dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_FILE}}",
				dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_FILE_HINT}}",
				
				filter: null
			},
			
			image: {
				buttonText: "",
				buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_IMAGE_BTN}}",
				buttonIconCls: 'ametysicon-image2 decorator-ametysicon-world91',
				
				menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_IMAGE}}",
				menuItemIconCls: 'ametysicon-image2 decorator-ametysicon-world91',
				
				dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_IMAGE}}",
				dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_IMAGE_HINT}}",
				
				filter: Ametys.explorer.tree.ExplorerTree.IMAGE_FILTER
			},
			
			multimedia: {
				buttonText: "",
				buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_MULTIMEDIA_BTN}}",
				buttonIconCls: 'ametysicon-movie16 decorator-ametysicon-world91',
				
				menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_MULTIMEDIA}}",
				menuItemIconCls: 'ametysicon-movie16 decorator-ametysicon-world91',
				
				dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_MULTIMEDIA}}",
				dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_MULTIMEDIA_HINT}}",
				
				filter: Ametys.explorer.tree.ExplorerTree.MULTIMEDIA_FILTER
			},
			
			video: {
				buttonText: "",
				buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_VIDEO_BTN}}",
				buttonIconCls: 'ametysicon-movie16 decorator-ametysicon-world91',
				
				menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_VIDEO}}",
				menuItemIconCls: 'ametysicon-movie16 decorator-ametysicon-world91',
				
				dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_VIDEO}}",
				dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_VIDEO_HINT}}",
				
				filter: Ametys.explorer.tree.ExplorerTree.VIDEO_FILTER
			},
			
			flash: {
				buttonText: "",
				buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_FLASH_BTN}}",
				buttonIconCls: 'ametysicon-flash decorator-ametysicon-world91',
				
				menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_FLASH}}",
				menuItemIconCls: 'ametysicon-flash decorator-ametysicon-world91',
				
				dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_FLASH}}",
				dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_FLASH_HINT}}",
				
				filter: Ametys.explorer.tree.ExplorerTree.FLASH_FILTER
			},
			
			audio: {
				buttonText: "",
				buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_SOUND_BTN}}",
				buttonIconCls: 'ametysicon-file-extension-generic-music decorator-ametysicon-world91',
				
				menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_SOUND}}",
				menuItemIconCls: 'ametysicon-file-extension-generic-music decorator-ametysicon-world91',
				
				dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_SOUND}}",
				dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_SOUND_HINT}}",
				
				filter: Ametys.explorer.tree.ExplorerTree.SOUND_FILTER
			},
            
            pdf: {
                buttonText: "",
                buttonTooltip: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_PDF_BTN}}",
                buttonIconCls: 'ametysicon-file-extension-pdf decorator-ametysicon-world91',
                
                menuItemText: "{{i18n WIDGET_RESOURCES_PICKER_EXPLORER_PDF}}",
                menuItemIconCls: 'ametysicon-file-extension-pdf decorator-ametysicon-world91',
                
                dialogTitle: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_PDF}}",
                dialogHint: "{{i18n WIDGET_RESOURCES_PICKER_INSERT_SHARED_PDF_HINT}}",
                
                filter: Ametys.explorer.tree.ExplorerTree.PDF_FILTER
            }
		}
	},
	
	getFileType: function ()
	{
		return this.self.TYPE;
	},
	
	getBtnConfig: function (config, filter)
	{
		filter = filter || 'none';
		
		return {
			text: config.buttonText || this.self.filters[filter].buttonText, 
            tooltip: config.buttonTooltip || this.self.filters[filter].buttonTooltip, 
            iconCls: config.buttonIconCls || this.self.filters[filter].buttonIconCls,
            source: this.self.SOURCE
        };
	},
	
	getMenuItemConfig: function (config, filter)
	{
		filter = filter || 'none';
		
        if (!this.self.filters[filter])
        {
            this.getLogger().error("The filter '" + filter + "' does not exists. Switching to 'none'.")
            filter = 'none';
        }
        
		return {
			text: this.self.filters[filter].menuItemText, 
	   		iconCls: this.self.filters[filter].menuItemIconCls, 
	   		source: this.self.SOURCE
		}
	},
	
	/**
	 * @inheritdoc
     * Opens a dialog to choose a resource from the resources' explorer.
     */
	handler: function (config, filter, allowedExtensions, callback)
	{
		filter = filter || 'none';
		
		/*icon, title, helpmessage, callback, filter, currentId*/
		Ametys.cms.uihelper.ChooseResource.open({
		    iconCls: config.buttonIconCls || this.self.filters[filter].buttonIconCls,
		    title: config.dialogTitle || this.self.filters[filter].dialogTitle, 
		    helpmessage: config.dialogHint || this.self.filters[filter].dialogHint,
            allowDragAndDrop: config.allowDragAndDrop,
		    callback: callback,
		    filter: this.self.filters[filter].filter,
            allowedExtensions: allowedExtensions
		});
	}
});

Ametys.form.widget.File.registerFileSource (Ametys.cms.widget.File.Resource.SOURCE, Ext.create('Ametys.cms.widget.File.Resource', {}));