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

/**
 * Class to handle web link
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Links.WebLinkHandler', {

	extend: "Ametys.cms.editor.LinkHandler",
	
	edit: function (href)
	{
		Ametys.plugins.cms.editor.Links._doInsertWebLink(href);
	},
	
	getTypeName: function()
	{
		return "{{i18n CONTENT_EDITION_LINK_VIEW_WEB_LABEL}}";
	},

	getTitle: function(href)
	{
		return href;
	},
	
	getDescription: function(href)
	{
		return "{{i18n CONTENT_EDITION_LINK_VIEW_WEB_DESCRIPTION}}" + href;
	}
});

/**
 * Class to handle mail link
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Links.MailLinkHandler', {

	extend: "Ametys.cms.editor.LinkHandler",
	
	edit: function (href)
	{
		Ametys.plugins.cms.editor.Links._doInsertMailtoLink(href)
	},
	
	getTypeName: function()
	{
		return "{{i18n CONTENT_EDITION_LINK_MAILTO_LABEL}}";
	},

	getTitle: function(href)
	{
		return href.substring('mailto:'.length);
	},
	
	getDescription: function(href)
	{
		return "{{i18n CONTENT_EDITION_LINK_MAILTO_DESCRIPTION}}" + href.substring('mailto:'.length);
	}
});

/**
 * Class to handle phone link
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Links.PhoneLinkHandler', {

    extend: "Ametys.cms.editor.LinkHandler",
    
    edit: function (href)
    {
        Ametys.plugins.cms.editor.Links._doInsertPhoneLink(href)
    },
    
    getTypeName: function()
    {
        return "{{i18n CONTENT_EDITION_LINK_PHONE_LABEL}}";
    },

    getTitle: function(href)
    {
        return href.substring('tel:'.length);
    },
    
    getDescription: function(href)
    {
        return "{{i18n CONTENT_EDITION_LINK_PHONE_DESCRIPTION}}" + href.substring('tel:'.length);
    }
});

/**
 * Class to handle resource link
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Links.ResourceLinkHandler', {

	extend: "Ametys.cms.editor.LinkHandler",
	
	edit: function (href)
	{
		Ametys.plugins.cms.editor.Links._doInsertResourcesLink(href);
	},
	
	getTypeName: function()
	{
		return "{{i18n CONTENT_EDITION_LINK_VIEW_SHAREDFILE_LABEL}}";
	},

	getTitle: function(href)
	{
		var response = Ametys.data.ServerComm.send({
			plugin: 'cms',
			url: 'resource/info',
			parameters: {id: href}, 
			priority: Ametys.data.ServerComm.PRIORITY_SYNCHRONOUS 
		});
		
		if (response == null || response.getAttribute("code") == "500" || response.getAttribute("code") == "404" || Ext.dom.Query.selectNode('resources > resource', response) == null)
		{
			return "{{i18n CONTENT_EDITION_LINK_VIEW_SHAREDFILE_ERROR}}";
		}
		
		return Ext.dom.Query.selectNode('resources > resource', response).getAttribute("name");
	},
	
	getDescription: function(href)
	{
		var response = Ametys.data.ServerComm.send({
			plugin: 'cms',
			url: 'resource/info',
			parameters: {id: href}, 
			priority: Ametys.data.ServerComm.PRIORITY_SYNCHRONOUS
		});
		
		if (response == null || response.getAttribute("code") == "500" || response.getAttribute("code") == "404" || Ext.dom.Query.selectNode('resources > resource', response) == null)
		{
			return "{{i18n CONTENT_EDITION_LINK_VIEW_SHAREDFILE_ERROR_DESC}}";
		}
		
		return "{{i18n CONTENT_EDITION_LINK_VIEW_SHAREDFILE_DESCRIPTION}}" + Ext.dom.Query.selectNode('resources > resource', response).getAttribute("path");
	}
});

/**
 * Class to handle an link to a content attachment.
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Links.AttachmentLinkHandler', {
	extend: 'Ametys.cms.editor.LinkHandler',
	
	edit: function (href)
	{
        // No message bus from FO edition
		var contentTarget = Ametys.message.MessageBus && Ametys.message.MessageBus.getCurrentSelectionMessage() != null ? Ametys.message.MessageBus.getCurrentSelectionMessage().getTarget(Ametys.message.MessageTarget.CONTENT) : tinyMCE.activeEditor.getParam('contentInfo').contentId;
		if (contentTarget)
		{
			Ametys.plugins.cms.editor.Links._doInsertAttachmentLink(contentTarget, href);
		}
	},
	
	getTypeName: function()
	{
		return "{{i18n CONTENT_EDITION_LINK_VIEW_ATTACHMENT_LABEL}}";
	},

	getTitle: function(href)
	{
		var response = Ametys.data.ServerComm.send({
			plugin: 'cms',
			url: 'resource/info',
			parameters: {id: href}, 
			priority: Ametys.data.ServerComm.PRIORITY_SYNCHRONOUS
		});
		
		if (response == null || response.getAttribute("code") == "500" || response.getAttribute("code") == "404" || Ext.dom.Query.selectNode('resources > resource', response) == null)
		{
			return "{{i18n CONTENT_EDITION_LINK_VIEW_ATTACHMENT_ERROR}}";
		}
		
		return Ext.dom.Query.selectValue('resources/resource/@name', response);
	},
	
	getDescription: function(href)
	{
		var response = Ametys.data.ServerComm.send({
			plugin: 'cms',
			url: 'resource/info',
			parameters: {id: href}, 
			priority: Ametys.data.ServerComm.PRIORITY_SYNCHRONOUS 
		});
		
		if (response == null || response.getAttribute("code") == "500" || response.getAttribute("code") == "404" || Ext.dom.Query.selectNode('resources > resource', response) == null)
		{
			return "{{i18n CONTENT_EDITION_LINK_VIEW_ATTACHMENT_ERROR_DESC}}";
		}
		
		return "{{i18n CONTENT_EDITION_LINK_VIEW_ATTACHMENT_DESCRIPTION}}" + Ext.dom.Query.selectValue('resources/resource/@path', response);
	}
});