/*
 *  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 class is the representation of a content type.
 */
Ext.define('Ametys.cms.content.ContentType', {
    extend: 'Ext.data.Model', 
    
    fields: [ 
        { name: 'id', type: 'string'}, 
        { name: 'label', type: 'string'}, 
        { name: 'description', type: 'string'}, 
        { name: 'defaultTitle', type: 'string'}, 
        { name: 'iconGlyph', type: 'string'}, 
        { name: 'iconDecorator', type: 'string'}, 
        { name: 'iconSmall', type: 'string'}, 
        { name: 'iconMedium', type: 'string'}, 
        { name: 'iconLarge', type: 'string'}, 
        { name: 'right', type: 'string'},
        { name: 'rightEvaluated', type: 'boolean' },
        { name: 'isSimple', type: 'boolean'},
        { name: 'isMultilingual', type: 'boolean'},
        { name: 'isMixin', type: 'boolean' },
        { name: 'isPrivate', type: 'boolean'},
        { name: 'isAbstract', type: 'boolean'},
        { name: 'isReferenceTable', type: 'boolean'},
        { name: 'superTypes' },
        { name: 'tags' },
        { name: 'parentModelItemName', type: 'string' }
    ],

    /**
     * Get the supertypes
     * @return {Ext.util.Collection} The content types as a collection of Ametys.cms.content.ContentTypeModel
     */
    getParents: function()
    {
        var results = [];
        
        Ext.Array.each(this.getSuperTypes(), function(contentTypeId) {
            var contentType = Ametys.cms.content.ContentTypeDAO.getContentType(contentTypeId);
            results.push(contentType);
        });
        
        var colResults = Ext.create("Ext.util.Collection");
        colResults.add(results)
        return colResults;
    },
    
    /**
     * Get the supertypes, and their supertypes and so on
     * @return {Ext.util.Collection} The content types as a collection of Ametys.cms.content.ContentTypeModel
     */
    getAncestors: function()
    {
        var results = [];
        
        function _getAncestors(parentContentType)
        {
            parentContentType.getParents().each(function(contentType) {
                results.push(contentType);
                _getAncestors(contentType);
            });
        }
        
        _getAncestors(this);
        
        var colResults = Ext.create("Ext.util.Collection");
        colResults.add(results)
        return colResults;
    },
    
    /**
     * Get the supertypes, and their supertypes and so on... plus the current instance
     * @return {Ext.util.Collection} The content types as a collection of Ametys.cms.content.ContentTypeModel
     */
    getAncestorsOrSelf: function()
    {
        var ancestors = this.getAncestors();
        ancestors.add(this);
        return ancestors;
    },

    
    /**
     * Get the subtypes, and their subtypes and so on
     * @return {Ext.util.Collection} The content types as a collection of Ametys.cms.content.ContentTypeModel
     */
    getDescendants: function()
    {
        var results = [];
        
        function _getDescendants(parentContentType)
        {
            parentContentType.getChildren().each(function(contentType) {
                results.push(contentType);
                _getDescendants(contentType);
            });
        }
        
        _getDescendants(this);
        
        var colResults = Ext.create("Ext.util.Collection");
        colResults.add(results)
        return colResults;
    },
    
    /**
     * Get the subtypes, and their subtypes and so on... plus the current instance
     * @return {Ext.util.Collection} The content types as a collection of Ametys.cms.content.ContentTypeModel
     */
    getDescendantsOrSelf: function()
    {
        var descendants = this.getDescendants();
        descendants.add(this);
        return descendants;
    },
    
    /**
     * Get the direct children content types
     * @return {Ext.util.Collection} The content types as a collection of Ametys.cms.content.ContentTypeModel
     */
    getChildren: function()
    {
        var me = this;
        return Ametys.cms.content.ContentTypeDAO.getContentTypes().createFiltered(function(contentType) {
            return contentType.getSuperTypes().indexOf(me.getId()) != -1;
        });
    },
    
    /**
     * Get the id
     * @return {String} The identifier
     */
    getId: function()
    {
        return this.data.id;
    },
    
    /**
     * Get the label
     * @return {String} The label
     */
    getLabel: function()
    {
        return this.data.label;
    },
    
    /**
     * Get the description
     * @return {String} The description
     */
    getDescription: function()
    {
        return this.data.description;
    },

    /**
     * Get the default title for a new content of this type
     * @return {String} The default title
     */
    getDefaultTitle: function()
    {
        return this.data.defaultTitle;
    },

    /**
     * Get the glyph representing the type
     * @return {String} The glyph name
     */
    getIconGlyph: function()
    {
        return this.data.iconGlyph;
    },

    /**
     * Get the decorator to set on the glyph
     * @return {String} The name of the decorator
     */
    getIconDecorator: function()
    {
        return this.data.iconDecorator;
    },

    /**
     * Get the url of the small sized icon
     * @return {String} The url (including context path)
     */
    getIconSmall: function()
    {
        return this.data.iconSmall;
    },

    /**
     * Get the url of the medium sized icon
     * @return {String} The url (including context path)
     */
    getIconMedium: function()
    {
        return this.data.iconMedium;
    },

    /**
     * Get the url of the large sized icon
     * @return {String} The url (including context path)
     */
    getIconLarge: function()
    {
        return this.data.iconLarge;
    },

    /**
     * Get the identifier of the right required to create a content of such a type
     * @return {String} The right id
     */
    getRight: function()
    {
        return this.data.right;
    },
    
    /**
     * Has the user the given right?
     * @return {Boolean} true if the user can create content of such a type
     */
    hasRight: function()
    {
        return this.data.rightEvaluated;
    },

    /**
     * Is the content type simple?
     * @return {Boolean} True if the content type is simple. 
     */
    isSimple: function()
    {
        return this.data.isSimple;
    },

    /**
     * Is the content type multilingual?
     * @return {Boolean} True if the content type is multilingual. 
     */
    isMultilingual: function()
    {
        return this.data.isMultilingual;
    },

    /**
     * Is the content type private?
     * @return {Boolean} True if the content type is private. 
     */
    isPrivate: function()
    {
        return this.data.isPrivate;
    },

    /**
     * Is the content type abstract?
     * @return {Boolean} True if the content type is abstract. 
     */
    isAbstract: function()
    {
        return this.data.isAbstract;
    },

    /**
     * Is the content type mixin?
     * @return {Boolean} True if the content type is mixin. 
     */
    isMixin: function()
    {
        return this.data.isMixin;
    },

    /**
     * Is the content type a reference table ?
     * @return {Boolean} True if the content type is a reference table. 
     */
    isReferenceTable: function()
    {
        return this.data.isReferenceTable;
    },
    
    /**
     * Get the super types of the content type
     * @return {String[]} An array of identifiers
     */
    getSuperTypes: function()
    {
        return this.data.superTypes;
    },
    
    /**
     * Get the tags
     * @return {String[]} An array of tags
     */
    getTags: function()
    {
        return this.data.tags;
    },
    
    /**
     * Get the name of the parent metadata or null otherwise
     * @return {String} The metadata name
     */
    getParentMetadataName: function()
    {
        return this.data.parentModelItemName;
    }
});