/* 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 init */

const ExtensionUtils = imports.misc.extensionUtils;

const Main = imports.ui.main;

const Me = ExtensionUtils.getCurrentExtension();

const { ActivitiesButton } = Me.imports.activities;
const { ApplicationsMenuButton } = Me.imports.applications;
const { Dash } = Me.imports.dash;
const { DateMenuButton } = Me.imports.dateMenu;
const { DropDownArrow } = Me.imports.dropDownArrow;
const { FavoritesMenuButton } = Me.imports.favorites;
const { KeyboardMenuButton } = Me.imports.keyboard;
const { PlacesMenuButton } = Me.imports.places;
const { ShowDesktopButton } = Me.imports.showDesktop;
const { SystemMenuButton } = Me.imports.systemMenu;
const { TrashMenuButton } = Me.imports.recycleBin;

class Extension {
    constructor(metadata) {
        ExtensionUtils.initTranslations();
        this._uuid = metadata.uuid;
        this._settings = ExtensionUtils.getSettings();
    }

    _onDisable(indicator) {
        if (indicator) {
            indicator.destroy();
            indicator = null;
        }
    }

    _onEnable() {
        this._activities = new ActivitiesButton(this._settings);
        this._dateMenu = new DateMenuButton(this._settings);
        this._keyboard = new KeyboardMenuButton(this._settings);
        this._systemMenu = new SystemMenuButton(this._uuid, this._settings);
        this._dropDownArrow = new DropDownArrow(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), 3, 'left');
        this._showDesktop = Main.panel.addToStatusArea('showDesktop', new ShowDesktopButton(), 4, 'left');
        let pos = Main.sessionMode.panel.right.indexOf('aggregateMenu');
        this._trashMenu = Main.panel.addToStatusArea('trashMenu', new TrashMenuButton(), pos, 'right');
    }

    enable() {
        if (Main.layoutManager._startingUp) {
            this._startupComplete = Main.layoutManager.connect('startup-complete', () => {
                this._onEnable();
                Main.layoutManager.disconnect(this._startupComplete);
            });
        } else {
            this._onEnable();
        }
    }

    disable() {
        this._onDisable(this._trashMenu);
        this._onDisable(this._showDesktop);
        this._onDisable(this._favoritesMenu);
        this._onDisable(this._placesMenu);
        this._onDisable(this._applicationsMenu);
        this._onDisable(this._dash);
        this._onDisable(this._dropDownArrow);
        this._onDisable(this._systemMenu);
        this._onDisable(this._keyboard);
        this._onDisable(this._dateMenu);
        this._onDisable(this._activities);
    }
}

function init(metadata) {
    return new Extension(metadata);
}
