(function() {
    var Ext = window.Ext4 || window.Ext;

    /**
     * @private
     * Utility class that provides information about Rally's data types.
     */
    Ext.define('Rally.util.TypeInfo', {
        requires: [],
        singleton: true,

        /*
         * Information about each artifact type
         *  - prefix:
         *  - shortTypeName:
         *  - pluralTypeName:
         *  - typeName:
         *  - detailSlugPrefix:
         *  - detail:
         *  - editor:
         *  - schemaType:
         *  - alias: the displayed type in the URL hash. E.g., userstory instead of hierarchicalrequirement.  This is
         *           needed for types that have an EDP.
         */
        typeInfos: {
            'hierarchicalrequirement' : {
                prefix              : 'ar',
                shortTypeName       : 'Story',
                pluralShortTypeName : 'Stories',
                typeName            : 'User Story',
                pluralTypeName      : 'User Stories',
                detailSlugPrefix    : '/project/detail',
                detail              : '/detail/ar',
                editor              : {
                    newPath         : 'ar/new.sp'
                },
                schemaType          : 'HierarchicalRequirement',
                alias               : 'userstory',
                icon                : 'icon-story'
            },
            'defect' : {
                prefix          : 'df',
                typeName        : 'Defect',
                detail          : '/detail/df',
                detailSlugPrefix: '/project/detail',
                editor          : {
                    newPath     : 'df/new.sp'
                },
                schemaType      : 'Defect',
                alias           : 'defect',
                icon            : 'icon-defect'
            },
            'defectsuite' : {
                prefix          : 'ds',
                typeName        : 'Defect Suite',
                detail          : '/detail/ds',
                detailSlugPrefix: '/project/detail',
                editor          : {
                    newPath     : 'ds/new.sp'
                },
                schemaType      : 'DefectSuite',
                icon            : 'icon-defect-suite'
            },
            'testcase' : {
                prefix          : 'tc',
                typeName        : 'Test Case',
                detail          : '/detail/tc',
                detailSlugPrefix: '/project/detail',
                schemaType      : 'TestCase',
                editor          : {
                    newPath     : 'tc/new.sp'
                },
                icon            : 'icon-test-case'
            },
            'testcaseresult' : {
                prefix          : 'tcr',
                typeName        : 'Test Case Result',
                detail          : '/detail/tcr',
                detailSlugPrefix: '/project/detail',
                schemaType      : 'TestCaseResult',
                editor          : {
                    newPath     : 'tcr/new.sp'
                }
            },
            'task' : {
                prefix          : 'tk',
                typeName        : 'Task',
                detail          : '/detail/tk',
                detailSlugPrefix: '/project/detail',
                schemaType      : 'Task',
                editor          : {
                    newPath     : 'tk/new.sp'
                },
                alias           : 'task',
                icon            : 'icon-task'
            },
            'testfolder' : {
                prefix          : 'tf',
                typeName        : 'Test Folder',
                detail          : null,
                detailSlugPrefix: '/project/detail',
                editor          : {
                    newPath     : 'tf/new.sp'
                },
                schemaType      : 'TestFolder',
                icon            : 'icon-folder'
            },
            'recyclebinentry' : {
                typeName        : 'Recycle Bin Entry',
                schemaType      : 'RecycleBinEntry'
            },
            'testset' : {
                prefix              : 'ts',
                typeName            : 'Test Set',
                pluralTypeName      : 'Test Sets',
                detail              : null,
                detailSlugPrefix    : '/project/detail',
                detailSlugSuffix    : '/run',
                editor              : {
                    newPath         : 'ts/new.sp'
                },
                schemaType          : 'TestSet',
                icon                : 'icon-test-set'
            },
            'project' : {
                prefix          : 'pj',
                typeName        : 'Project',
                detail          : '/detail',
                detailSlugPrefix: '/setup/detail',
                schemaType      : 'Project',
                editor          : {
                    newPath     : 'pj/new.sp'
                }
            },
            'workspace' : {
                prefix          : 'ws',
                detailSlugPrefix: '/setup/detail',
                editor          : {
                    newPath         : 'ws/new.sp'
                },
                schemaType: 'Workspace'
            },
            'subscription' : {
                prefix          : 'subscription',
                detailSlugPrefix: '/setup/detail',
                schemaType: 'Subscription'
            },
            'adminsubscription' : {
                prefix          : 'admin/sp',
                detailSlugPrefix: '/admin/detail'
            },
            'projectuser' : {
                prefix          : 'user',
                detail          : '/detail/user',
                detailSlugPrefix: '/setup/detail',
                alias           : 'user'
            },
            'changeset' : {
                prefix          : 'changeset',
                detail          : null
            },
            'build' : {
                prefix          : 'build',
                detail          : null
            },
            'change' : {
                prefix          : 'change',
                detail          : null
            },
            'user' : {
                typeName        : 'User',
                prefix          : 'user',
                detail          : '/detail/user',
                detailSlugPrefix: '/setup/detail',
                editor          : {
                    newPath     : 'user/new.sp'
                },
                schemaType      : 'User'
            },
            'adminuser' : {
                typeName        : 'User',
                prefix          : 'admin/user',
                editor          : {
                    newPath     : 'admin/user/new.sp'
                }
            },
            'release' : {
                prefix          : 'rl',
                detailSlugPrefix: '/project/detail',
                detail          : '/detail/release',
                editor          : {
                    newPath     : 'rl/new.sp'
                },
                schemaType      : 'Release'
            },
            'iteration' : {
                prefix          : 'it',
                detailSlugPrefix: '/project/detail',
                detail          : '/detail/iteration',
                editor          : {
                    newPath     : 'it/new.sp'
                },
                schemaType      : 'Iteration'
            },
            'webtab' : {
                prefix          : 'wt',
                detail          : null,
                editor          : {
                    newPath     : 'wt/new.sp'
                }
            },
            'program' : {
                prefix          : 'pg',
                editor          : {
                    newPath     : 'pg/new.sp'
                }
            },
            'attribute' : {
                prefix          : 'at',
                editor          : {
                    newPath     : 'at/new.sp'
                }
            },
            'scopedattributedefinition' : {
                prefix          : 'at',
                editor          : {
                    newPath     : 'at/new.sp'
                },
                alias           : 'weblinkdefinition'
            },
            'attributedefinition' : {
                prefix          : 'at',
                editor          : {
                    newPath     : 'at/new.sp'
                },
                alias           : 'weblinkdefinition'
            },
            'customview' : {
                prefix          : 'view',
                editor          : {
                    newPath     : 'view/new.sp'
                }
            },
            'usernotificationfilter' : {
                prefix          : 'un',
                editor          : {
                    newPath     : 'un/new.sp'
                }
            },
            'portfolioitem' : {
                icon    : 'icon-portfolio',
                prefix  : 'pi',
                schemaType : 'PortfolioItem'
            },
            'milestone' : {
                icon    : 'icon-milestone',
                typeName: 'Milestone',
                alias   : 'milestone'
            },
            'deliverygroup' : {
                icon    : 'icon-delivery-group',
                typeName: 'DeliveryGroup',
                alias   : 'deliverygroup'
            },
            'expertise' : {
                icon    : 'icon-expertise',
                typeName: 'Expertise',
                alias   : 'expertise'
            },
            'expertisedemand' : {
                icon    : 'icon-expertise',
                typeName: 'ExpertiseDemand',
                alias   : 'expertisedemand'
            },
            'expertisecapacity' : {
                icon    : 'icon-expertise',
                typeName: 'ExpertiseCapacity',
                alias   : 'expertisecapacity'
            }

        },
        // portfolioitem/<pi_type> e.g. portfolioitem/feature can be passed to this to get the portfolioitem key.
        normalizeTypeName: function(type) {
            return type.toLowerCase().replace(/ /g, '').replace(/\/.+$/, '');
        },

        getTypeInfoByName: function(name) {
            var formattedName = name.toLowerCase().replace(/ /g, ''); // we can't just call normalizeTypeName because there is code in alm that depends on the current behavior. :(
            var returnVal = this.typeInfos[formattedName] || this.getTypeInfoByAttr('alias', formattedName);
            return returnVal || this.buildDefaultTypeInfo(name, formattedName);
        },

        buildDefaultTypeInfo: function(name, formattedName) {
            return {
                prefix : formattedName,
                typeName : name,
                editor : {
                    newPath : formattedName + '/new.sp'
                },
                schemaType: formattedName,
                alias: formattedName
            };
        },

        getTypeInfoByAttr: function(attr, value) {
            var info = null,
                typeInfos = this.typeInfos;
            for (var typeName in typeInfos) {
                if(typeInfos.hasOwnProperty(typeName)){
                    var type = typeInfos[typeName];
                    if(type[attr] === value){
                        info = type;
                        break;
                    }
                }
            }
            return info;
        }
    });
})();