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

    /**
     * @private
     */
    Ext.define('Rally.ui.gridboard.plugin.GridBoardCollectionAddRemoveExisting', {
        alias:'plugin.rallygridboardcollectionaddremoveexisting',
        extend:'Ext.AbstractPlugin',
        mixins: ['Rally.app.Scopeable'],
        requires: ['Deft.Promise', 'Rally.ui.Button'],

        init: function (gridboard) {
            this.callParent(arguments);

            if (this.isProjectEditor()) {
                gridboard.getHeader().getLeft().add({
                    xtype: 'rallybutton',
                    cls: 'secondary rly-small',
                    handler: function() {
                        this.onChooseClick().then({
                            success: function () {
                                Ext.create('Rally.ui.dialog.ArtifactCollectionChooserDialog', Ext.merge({
                                    collectionStoreToAddTo: this.collectionStoreToAddTo,
                                    listeners: {
                                        addsuccess: {
                                            fn: this._refreshGridStore,
                                            scope: this
                                        }
                                    }
                                }, this.chooserDialogConfig));
                            },
                            scope: this
                        });
                    },
                    margin: '3 9 0 7',
                    scope: this,
                    text: 'Choose'
                });
            }

            this.cmpOnRecordMenuCopy = gridboard.gridConfig && gridboard.gridConfig.onRecordMenuCopy;
            this.cmpOnRecordMenuRemove = gridboard.gridConfig && gridboard.gridConfig.onRecordMenuRemove;
            this.cmpRowActionColumnShouldBeRemovable = gridboard.gridConfig && gridboard.gridConfig.rowActionColumnConfig && gridboard.gridConfig.rowActionColumnConfig.menuOptions && gridboard.gridConfig.rowActionColumnConfig.menuOptions.shouldBeRemovable;
            _.merge(gridboard, {
                gridConfig: {
                    onRecordMenuCopy: Ext.bind(this._onRecordMenuCopy, this),
                    onRecordMenuRemove: Ext.bind(this._onRecordMenuRemove, this),
                    rowActionColumnConfig: {
                        menuOptions: {
                            collectionStore: this.collectionStoreToAddTo,
                            shouldBeRemovable: Ext.bind(this._rowActionColumnShouldBeRemovable, this)
                        }
                    }
                }
            });

            gridboard.on('recordcreate', this._refreshCollectionStore, this);
        },

        onChooseClick: function () {
            return Deft.Promise.when();
        },

        _onRecordMenuCopy: function() {
            if(Ext.isFunction(this.cmpOnRecordMenuCopy)) {
                this.cmpOnRecordMenuCopy.call(this.cmp);
            }

            this._refreshCollectionStore();
        },

        _onRecordMenuRemove: function() {
            if(Ext.isFunction(this.cmpOnRecordMenuRemove)) {
                this.cmpOnRecordMenuRemove.call(this.cmp);
            }

            this._refreshGridStore();
        },

        _refreshCollectionStore: function() {
            this.collectionStoreToAddTo.reload();
        },

        _refreshGridStore: function() {
            this.cmp.getGridOrBoard().refresh();
        },

        _rowActionColumnShouldBeRemovable: function(record) {
            var shouldBeRemovable = Ext.isFunction(this.cmpRowActionColumnShouldBeRemovable) ? this.cmpRowActionColumnShouldBeRemovable.call(this.cmp, record) : true;
            return shouldBeRemovable && !!this.collectionStoreToAddTo.findRecord('_ref', record.get('_ref'));
        }
    });
})();