/* global define */

define([
    'underscore',
    'marionette',
    '../core'
], function(_, Marionette, c) {


    // Provides a set of actions for manipulating a ContextModel object
    var ContextActions = Marionette.ItemView.extend({
        template: 'context/actions',

        ui: {
            removeAll: '[data-action=remove]',
            count: '[data-target=count]',
            units: '[data-target=units]',
            loading: '[data-target=loading-message]',
            refresh: '[data-action=refresh]'
        },

        events: {
            'click @ui.removeAll': 'clickRemoveAll',
            'click @ui.refresh': 'refreshCount'
        },

        modelEvents: {
            'request': 'showLoad',
            'sync': 'enableRefreshButton'
        },

        collectionEvents: {
            'add remove reset': 'renderRemoveAll'
        },

        initialize: function() {
            this.model.stats.on('sync', this.showCount, this);
        },

        serializeData: function() {
            var attrs = _.clone(this.model.attributes);

            attrs.count = this.model.stats.get('count');
            attrs.prettyCount = c.utils.prettyNumber(
                attrs.count, c.config.get('threshold'));

            return attrs;
        },

        clickRemoveAll: function() {
            this.collection.unapply();
        },

        onRender: function() {
            this.renderRemoveAll();
        },

        showLoad: function() {
            if (c.config.get('distinctCountAutoRefresh')) {
                this.ui.count.hide();
                this.ui.units.hide();
                this.ui.loading.show();
            }
        },

        showCount: function() {
            this.ui.count.show();
            this.ui.units.show();
            this.ui.loading.hide();
            this.render();
        },

        enableRefreshButton: function() {
            this.ui.refresh.show();
            this.$el.addClass('muted');
        },

        hideRefreshButton: function() {
            this.ui.refresh.hide();
            this.ui.loading.hide();
        },

        renderRemoveAll: function() {
            // Required filters cannot be removed, so filter them out
            var models = this.collection.reject(function(model) {
                return model.get('required') === true;
            });

            this.ui.removeAll.prop('disabled', !models.length);
        },

        refreshCount:function() {
            this.model.stats.manualFetch();
            this.$el.removeClass('muted');
            this.ui.refresh.hide();
            this.ui.count.hide();
            this.ui.units.hide();
            this.ui.loading.show();
        }
    });


    return {
        ContextActions: ContextActions
    };

});

@@ -1901,124 +1901,105 @@
     
-this.ui.refresh.show();%0A            this.$el.addClass('muted');%0A        %7D,%0A%0A        hideRefreshButton: function() %7B%0A
+if (!c.config.get('distinctCountAutoRefresh')) %7B%0A                this.$el.addClass('muted');%0A    
     
@@ -2014,36 +2014,36 @@
 this.ui.refresh.
-hide
+show
 ();%0A            
@@ -2042,39 +2042,17 @@
         
-this.ui.loading.hide();
+%7D
 %0A       

Fix logic for showing an out-of-date distinct count