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

/**
 * This class is the representation of a page.
 */
Ext.define(
	"Ametys.web.sitemap.Sitemap", 
	{
		config: {
			/**
			 * @cfg {String} id The unique id of the sitemap
			 */
			/**
			 * @method getId Get the #cfg-id
			 * @return {String} The id
			 */
			/** @ignore */
			id: null,
			/**
			 * @cfg {String} name The name of the sitemap
			 */
			/**
			 * @method getName Get the #cfg-name
			 * @return {String} The name
			 */
			/** @ignore */
			name: null,
			/**
			 * @cfg {String} siteName The site name
			 */
			/**
			 * @method getSiteName Get the #cfg-siteName
			 * @return {String} The site name
			 */
			/** @ignore */
			siteName: null,
			
            /**
             * @cfg {String} type The type of the page
             */
            /**
             * @method getType Get the #cfg-type
             * @return {String} The type
             */
            /** @ignore */
            type: null,
            
            /**
             * @cfg {String} template The template of the page. This is not null if the page is a 'container' page.
             */
            /**
             * @method getTemplate Get the #cfg-template
             * @return {String} The page template
             */
            /** @ignore */
            template: null,
            
            /**
             * @cfg {Object[]} zones List of the zones of on this page
             */
            /**
             * @method getZones Get the #cfg-zones
             * @return {Object[]} The zones
             */
            /** @ignore */
            zones: [],
            
			/**
			 * @method getIsModifiable Get the #cfg-isModifiable
			 * @return {boolean} The is modifiable
			 */
			/** @ignore */
			isModifiable: false,
			/**
			 * @cfg {String[]} rights List of the id of the rights the current user have on this page
			 */
			/**
			 * @method getRights Get the #cfg-rights
			 * @return {String[]} The rights
			 */
			/** @ignore */
			rights: []
		},
		
		
		/**
		 * Creates a page instance
		 * @param {Object} config See configuration doc.
		 */
		constructor: function (config)
		{
			this.initConfig(config);
			
			this._messageTarget = config.messageTarget || Ametys.message.MessageTarget.SITEMAP;
		},
		
		/**
		 * Determines if the current user has the specified right on the sitemap
		 * @param {String} rightId The identifier of the right to check
		 * @return {boolean} True or false
		 */
		hasRight: function(rightId)
		{
			return Ext.Array.contains(this._rights, rightId);
		},
		
		/**
		 * Get the page's properties
		 * @return {Object} The page's properties
		 */
		getProperties: function (initialProperty)
		{
			return Ext.apply ({
					id: this._id,
					name: this._name,
					siteName: this._siteName,
					isModifiable : this._isModifiable,
					rights: this._rights,
					type: this._type,
					template: this._template
				}, initialProperty
			);
		},
		
		/**
		 * Get the language code
		 * @return The language code of the sitemap
		 */
		getLang: function()
		{
		    return this.getName(); 
		},
		
        /**
         * Get the identifiers of contents of this page
         * @return {String[]} The contents' id.
         */
        getContentIds: function ()
        {
            var contentIds = [];
            Ext.Array.forEach (this._zones, function (zone) {
                var zoneitems = zone.zoneitems || [];
                
                Ext.Array.forEach (zoneitems, function (zoneitem) {
                    
                    if (zoneitem.content)
                    {
                        contentIds.push(zoneitem.content.id)
                    }
                    
                });
            });
            return contentIds;
        }
	}
);