odoo.define("project_report.GraphController", function(require) {
    var GraphController = require("web.GraphController");

    GraphController.include({

        init: function(parent, model, renderer, params) {
            this._super.apply(this, arguments);
        },

      /**
     * Makes sure that the buttons in the control panel matches the current
     * state (so, correct active buttons and stuff like that).
     *
     * @override
     */
    updateButtons: function () {
        if (!this.$buttons) {
            return;
        }
        var state = this.model.get();
        this.$buttons.find('.o_graph_button').removeClass('active');
        this.$buttons
            .find('.o_graph_button[data-mode="' + state.mode + '"]')
            .addClass('active');
        this.$buttons
            .find('.o_graph_button[data-mode="stack"]')
            .data('stacked', state.stacked)
            .toggleClass('active', state.stacked)
            .toggleClass('o_hidden', state.mode !== 'bar' && state.mode !== 'horizontalBar');
        this.$buttons
            .find('.o_graph_button[data-order]')
            .toggleClass('o_hidden', state.mode === 'pie' || !!Object.keys(state.timeRanges).length)
            .filter('.o_graph_button[data-order="' + state.orderBy + '"]')
            .toggleClass('active', !!state.orderBy);

        if (this.withButtons) {
            this._attachDropdownComponents();
        }
    },

    //--------------------------------------------------------------------------
    // Handlers
    //--------------------------------------------------------------------------

    /**
     * Do what need to be done when a button from the control panel is clicked.
     *
     * @private
     * @param {MouseEvent} ev
     */
    _onButtonClick: function (ev) {
        var $target = $(ev.target);
        if ($target.hasClass('o_graph_button')) {
            if (_.contains(['bar', 'horizontalBar', 'line', 'pie'], $target.data('mode'))) {
                this.update({ mode: $target.data('mode') });
            } else if ($target.data('mode') === 'stack') {
                this.update({ stacked: !$target.data('stacked') });
            } else if (['asc', 'desc'].includes($target.data('order'))) {
                const order = $target.data('order');
                const state = this.model.get();
                this.update({ orderBy: state.orderBy === order ? false : order });
            }
        }
    },

    });
});
