/* extension.js
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/* exported PanelManagerExtension */

import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';

import * as Main from 'resource:///org/gnome/shell/ui/main.js';

import { ActivitiesButton } from './activities.js';
import { ApplicationsMenuButton } from './applications.js';
import { Dash } from './dash.js';
import { DateMenuButton } from './dateMenu.js';
import { FavoritesMenuButton } from './favorites.js';
import { PlacesMenuButton } from './places.js';
import { ShowDesktopButton } from './showDesktop.js';
import { SystemMenuButton } from './systemMenu.js';
import { TrashMenuButton } from './recycleBin.js';

export default class PanelManagerExtension extends Extension {
    _onEnable() {
        this._activities = new ActivitiesButton(this._settings);
        this._dateMenu = new DateMenuButton(this._settings);
        this._systemMenu = new SystemMenuButton(this.uuid, this._settings);
        this._dash = new Dash(this._settings);
        this._applicationsMenu = Main.panel.addToStatusArea('applicationsMenu', new ApplicationsMenuButton(this._settings), 1, 'left');
        this._placesMenu = Main.panel.addToStatusArea('placesMenu', new PlacesMenuButton(this._settings), 2, 'left');
        this._favoritesMenu = Main.panel.addToStatusArea('favoritesMenu', new FavoritesMenuButton(this._settings, this.path), 3, 'left');
        this._showDesktop = Main.panel.addToStatusArea('showDesktop', new ShowDesktopButton(), 4, 'left');
        let pos = Main.sessionMode.panel.right.indexOf('quickSettings');
        this._trashMenu = Main.panel.addToStatusArea('trashMenu', new TrashMenuButton(), pos, 'right');
    }

    enable() {
        this._settings = this.getSettings();
        if (Main.layoutManager._startingUp) {
            Main.layoutManager.connectObject('startup-complete', () => {
                this._onEnable();
                Main.layoutManager.disconnectObject(this);
            }, this);
        } else {
            this._onEnable();
        }
    }

    disable() {
        this._trashMenu?.destroy();
        this._trashMenu = null;
        this._showDesktop?.destroy();
        this._showDesktop = null;
        this._favoritesMenu?.destroy();
        this._favoritesMenu = null;
        this._placesMenu?.destroy();
        this._placesMenu = null;
        this._applicationsMenu?.destroy();
        this._applicationsMenu = null;
        this._dash?.destroy();
        this._dash = null;
        this._systemMenu?.destroy();
        this._systemMenu = null;
        this._dateMenu?.destroy();
        this._dateMenu = null;
        this._activities?.destroy();
        this._activities = null;
        this._settings = null;
    }
}
