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

/**
 * Singleton class to lock or unlock contents
 * @private
 */
Ext.define('Ametys.plugins.cms.content.actions.UnlockOrLockContentAction', {
	singleton: true,
	
	/**
	 * Action function to be called by the controller.
	 * Will lock or unlock the contents registered by the controller according the pressed state.
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	act: function(controller)
	{
		var lock = controller.isPressed();
		
		if (lock) 
		{
			var contents = controller._currentLockedOwnerContentsId;
			lock = 'unlock';
		}
		else
		{
			var contents = controller._currentUnlockedContentsId;
			lock='lock';
		}
		
		var messageTargetId = controller.getMatchingTargets().length > 0 ? controller.getMatchingTargets()[0].getId() : null;
		Ametys.data.ServerComm.callMethod({
			role: 'org.ametys.cms.lock.LockContentManager',
			methodName: 'unlockOrLock',
			parameters: [contents, lock],
			callback: {
				scope: this,
				handler: this._actCb,
				arguments: {messageTargetId: messageTargetId}
			},
			errorMessage: {
				msg: "{{i18n CONTENT_LOCK_ERROR}}",
				category: 'Ametys.plugins.cms.content.actions.UnlockOrLockContentAction'
			}
		});
	},
	
	/**
	 * Callback function called after #act is processed.
	 * Fires Ametys.message.Message#LOCK_CHANGED message for locked or unlocked contents
	 * @param {Object[]} response the server response
	 * @param {String} response.mode the mode ( unlock or lock )
	 * @param {String[]} response.unlocked-contents-id the ids of the unlocked contents
	 * @param {String[]} response.unlocked-contents the unlocked contents
	 * @param {String[]} response.still-locked-contents the contents that are still locked
	 * @param {String[]} response.fail-locked-contents the contents that failed to be locked
	 * @param {String[]} response.locked-contents-id the ids of the locked contents
	 * @param {String[]} response.locked-contents the locked contents
	 * @param {String[]} response.already-locked-contents the contents already locked
	 * @param {String[]} response.fail-unlocked-contents the contents that failed to be unlocked
	 * @param {Object[]} args the callback arguments
	 * @param {String} args.messageTargetId the message target id
	 * @private
 	*/
	_actCb: function (response, args)
	{

		var unlockMode = response['mode'] == 'unlock';
		if (unlockMode)
		{
			var unlockedContents = response['unlocked-contents'];
			var stillLockedContents = response['still-locked-contents'];
			var failLockedContents = response['fail-unlocked-contents'];
			
			var msg = "";
			if (stillLockedContents != "" || failLockedContents != "")
			{
				if (unlockedContents != "")
				{
					msg += "{{i18n CONTENT_UNLOCK_ACTION_UNLOCKED_CONTENTS}}" + unlockedContents;
				}
				
				if (stillLockedContents != "")
				{
					if (msg != '')
					{
						msg + "<br/><br/>";
					}
					msg += "{{i18n CONTENT_UNLOCK_ACTION_STILL_LOCKED_CONTENTS}}" + stillLockedContents;
				}
				
				if (failLockedContents != "")
				{
					if (msg != '')
					{
						msg + "<br/><br/>";
					}
					msg += "{{i18n CONTENT_UNLOCK_ACTION_FAILED_CONTENTS}}" + failLockedContents;
				}
				
				Ametys.log.ErrorDialog.display({
					title: "{{i18n CONTENT_UNLOCK_ACTION}}",
					text: msg,
					details: "",
					category: "Ametys.plugins.cms.content.controller.LockController.act"
				});
			}
			
			var unlockedContentIds = response['unlocked-contents-id'].split(',');
			if (unlockedContentIds.length > 0)
			{
				Ext.create("Ametys.message.Message", {
					type: Ametys.message.Message.LOCK_CHANGED,
					
					targets: {
						id: args.messageTargetId || Ametys.message.MessageTarget.CONTENT,
						parameters: { ids: unlockedContentIds }
					}
				});
			}
		}
		else
		{
			var lockedContents = response['locked-contents'];
			var alreadyLockedContents = response['already-locked-contents'];
			var failUnlockedContents = response['fail-locked-contents'];
			
			if (alreadyLockedContents != "" || failUnlockedContents != "")
			{
				var msg = "";
				if (lockedContents != "")
				{
					msg += "{{i18n CONTENT_LOCK_ACTION_LOCKED_CONTENTS}}"  + lockedContents;
				}
				
				if (alreadyLockedContents != "")
				{
					if (msg != '')
					{
						msg + "<br/><br/>";
					}
					msg += "{{i18n CONTENT_LOCK_ACTION_ALREADY_LOCKED_CONTENTS}}" + alreadyLockedContents;
				}
				
				if (failUnlockedContents != "")
				{
					if (msg != '')
					{
						msg + "<br/><br/>";
					}
					msg += "{{i18n CONTENT_LOCK_ACTION_FAILED_CONTENTS}}" + failUnlockedContents;
				}
				
				Ametys.log.ErrorDialog.display({
					title: "{{i18n CONTENT_LOCK_ACTION}}",
					text: msg,
					details: "",
					category: "Ametys.plugins.cms.content.controller.LockController.act"
				});
			}
			
			var lockedContentIds = response["locked-contents-id"].split(',');
			if (lockedContentIds.length > 0)
			{
				Ext.create("Ametys.message.Message", {
					type: Ametys.message.Message.LOCK_CHANGED,
					
					targets: {
						id: args.messageTargetId || Ametys.message.MessageTarget.CONTENT,
						parameters: { ids: lockedContentIds }
					}
				});
			}
		}
	}
});