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

    /**
     * Create a popover to display the test cases associated with a specified record.
     * Generally this class will not be instantiated directly but instead will be obtained
     * via the Rally.ui.popover.PopoverFactory#bake method.
     *
     *     Rally.ui.popover.PopoverFactory.bake({
     *         field: 'TestCase'
     *     });
     */
    Ext.define('Rally.ui.popover.TestCasePopover', {
        alias: 'widget.rallytestcasepopover',
        extend: 'Rally.ui.popover.ListViewPopover',
        requires: [
            'Rally.ui.dialog.plugins.ArtifactTestFolderFilter',
            'Rally.ui.grid.RowActionColumn',
            'Rally.ui.popover.PopoverFactory',
            'Rally.util.Ref'
        ],

        id: 'testcase-popover',
        cls: 'testcase-popover',
        title: 'Test Cases',
        titleIconCls: 'icon-test-case',
        pageSize: 10,
        width: 825,
        
        constructor: function(config) {
            var record = config.record;
            var columnCfgs = [
                {
                    dataIndex: 'FormattedID',
                    width: 90
                },
                {
                    dataIndex: 'Name',
                    flex: 1
                },
                {
                    dataIndex: 'Owner',
                    width: 80
                },
                {
                    dataIndex: 'Type',
                    width: 80
                },
                {
                    dataIndex: 'LastVerdict',
                    width: 90
                },
                {
                    dataIndex: 'LastBuild',
                    width: 80
                },
                {
                    dataIndex: 'LastRun',
                    width: 80
                }
            ];

            var testCasesStore = record.getCollection('TestCases', {
                limit: this.pageSize,
                pageSize: this.pageSize,
                context: { project: null },
                listeners: {
                    load: {
                        fn: function() {
                            if (Rally.BrowserTest) {
                                Rally.BrowserTest.publishComponentReady(this);
                            }
                        },
                        single: true
                    },
                    scope: this
                },
                fetch: ['LastResult'],
                autoLoad: true
            });

            var gridConfig = {
                store: testCasesStore,
                showRowActionsColumn: false,
                enableRanking: true,
                columnCfgs: columnCfgs
            };
            var modelTypeListViewConfig = {
                gridConfig: gridConfig,
                model: Ext.identityFn('TestCase')
            };

            if (record.isUpdatable()) {
                columnCfgs.unshift({
                    xtype: 'rallyrowactioncolumn',
                    menuOptions: {
                        associatedRecord: record,
                        testCasesStore: testCasesStore
                    },
                    scope: this
                });
            }

            if (record.self.typePath === 'testset') {
                gridConfig.store = Ext.create('Rally.data.wsapi.artifact.Store', {
                    url: Rally.environment.getServer().getWsapiUrl() + Rally.util.Ref.getRelativeUri(config.record) + '/ScheduledTestCases',
                    models: [Ext.identityFn('TestCase')],
                    fetch: ['FormattedID', 'Name', 'Type', 'LastResult', 'Owner', 'Build', 'LastBuild', 'Verdict', 'LastVerdict', 'Date', 'LastRun', 'TestCase', 'DragAndDropRank'],
                    context: config.context,
                    record: record,
                    sorters: [],
                    filters: [],
                    autoLoad: true,
                    pageSize: 10,
                    alwaysSortedByRankAsc: true
                });
                Ext.merge(gridConfig, {
                    bulkEditConfig: {
                        store: testCasesStore,
                        showTag: false,
                        showParent: false,
                        showEdit: false,
                        showRemove: true
                    },
                    enableBulkEdit: true,
                    sortableColumns: false
                });

                modelTypeListViewConfig.addExistingConfig = {
                    chooserDialogConfig: {
                        collectionStoreToAddTo: testCasesStore,
                        plugins: [
                            'rallyartifacttestfolderfilter',
                            {
                                ptype: 'rallyartifactprojectfilter',
                                fieldLabel: ' '
                            }
                        ],
                        storeConfig: {
                            context: config.context,
                            filters: [{
                                property: 'TestSets',
                                operator: '!contains',
                                value: Rally.util.Ref.getRelativeUri(testCasesStore.record)
                            }]
                        },
                        title: 'Choose Test Cases'
                    }
                };
            } else {
                _.assign(modelTypeListViewConfig, {
                    addNewConfig: {
                        ignoredRequiredFields: ['ObjectID', 'FormattedID', 'Name', 'DefectStatus', 'Method', 'Type', 'CreationDate', 'LastUpdateDate', 'Project']
                    },
                    parentProperty: 'WorkProduct'
                });
            }

            config.listViewConfig = Ext.merge(modelTypeListViewConfig, config.listViewConfig);

            this.callParent(arguments);
        }
    });
})();