/*
 *  Copyright 2015 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 class is the abstract class for cards of create page wizard {@link Ametys.plugins.web.page.AddPageWizard}
 * 
 * Creates your own card by inheriting this one and define at least the following methods: #createPanel, #apply, #getUI.
 * 
 * 		Ext.define("My.Card", {
 * 			extend: "Ametys.plugins.web.page.AddPageWizard.Card",
 * 
 * 			createPanel: function() {
 * 				this._cardPanel Ext.create("Ext.panel.Panel", { html: "My card" });
 * 				return this._cardPanel;
 * 			},
 * 
 * 			apply: function(fullModel, callback) {
 * 				// TODO
 * 			},
 * 
 * 			getUI: function ()
 * 			{
 * 				return this._cardPanel;
 * 			}
 * 		});
 * 
 */
Ext.define("Ametys.plugins.web.page.AddPageWizard.Card", 
	{
		/**
		 * @private
		 * @property {Object} _model The card model
		 */
	
		/**
		 * @cfg {Ametys.window.DialogBox} box The box container
		 */
        /**
         * @property {Ametys.window.DialogBox}  box See {@cfg-box}.
         */
        box: null,
        
        /**
         * @property {String} name Name of the card.
         */
        name: null,
	
		constructor: function(config)
		{
			this._model = {};
			this.box = config.box;
		},
		
		/**
		 * Creates the card panel
		 * @return {Ext.Component} the created card panel
		 */
		createPanel: function ()
		{
			throw new Error("Method #createPanel is not implemented in " + this.self.getName());
		},
		
        /**
         * When the wizard is over, each card will be called, in the wizard order, with the full final model to really do the work
         * @param {Object} fullModel The whole cards model
         * @param {Function} callback The function to call after the apply is over, to go the next step
         */
		apply: function (fullModel, callback)
		{
			throw new Error("Method #apply is not implemented in " + this.self.getName());
		},
		
        /**
         * Get the cart panel (created in #createPanel)
         * @return {Ext.Component} The card panel
         */
		getUI: function ()
		{
			throw new Error("Method #getUI is not implemented in " + this.self.getName());
		},
		
		/**
		 * Adapt the UI to reflect the model
         * @param {Function} callback Callback function to call after UI ends to be updated
         * @param {boolean} callback.success true if the update was a success
		 */
		updateUI: function(callback)
		{
		},

        /**
         * Determines if this card screen can be leaved
         * @return {Boolean} true if model is ok
         */
        isModelOk: function(fullModel)
        {
            return true;
        },

        /**
         * Determines if the full process can be terminated now (card can be leaved, and process finished)
         * @return {Boolean} true if process is ok
         */
        isProcessOk: function(fullModel)
        {
            return true;
        },
		
		/**
		 * Determines if this card is available
		 * @param {Object} fullModel the full model
		 * @return {Boolean} true if this card is ok
		 */
		isAvailable: function(fullModel)
		{
			return true;
		},
        
        /**
         * Determines if this card should be displayed
         * @return {Boolean} true if this card is can be displayed
         */
        shouldDisplayCard: function(fullModel)
        {
            var displayCardInConf = this.displayCardInConf();
            var available = fullModel != null ? this.isAvailable(fullModel) : true;
            return displayCardInConf && available;
        },
        
        /**
         * Determines if this card should be displayed (according to the conf only, not to the model and card code)
         * @return {Boolean} true if this card is can be displayed
         */
        displayCardInConf: function()
        {
            var displayCardInConf = true;
            if (this.name != null)
            {
                displayCardInConf = Ametys.plugins.web.page.AddPageWizard.getInitialConfig('show-card-' + this.name);
            }
            return displayCardInConf;
        },

		/**
		 * Action to execute when entering on card
		 */
		onEnter: function()
		{
		},

		/**
		 * Action to execute when leaving card
		 */
		onLeave: function()
		{
		},
		
        /**
         * Get the model of the card
         * @return {Object} The local model
         */
		getModel: function()
		{
			return this._model;
		},
		
		/**
		 * Reset model
		 * @param {Function} callback called after reset model. This callback needs to be called.
         */
		resetModel: function(callback)
		{
			this._model = {};
			
			callback();
		},
		
		/**
		 * Update model
		 */
		updateModel: function()
		{
		},

        /**
         * @private
         * Return Check if object is true or "true"
         * @param {Boolean/String} inputObject object to check
         * @return {Boolean} true if object is true or "true", false otherwise
         */
        _isTrue: function(inputObject)
        {
            if (inputObject === true || inputObject == 'true')
            {
                return true;
            }
            else
            {
                return false;
            }
        }
	}
);