/* 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-3.0-or-later
 */

import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';

import { SnapFinderSearchProvider } from './snapFinderSearchProvider.js';

const SEARCH_PROVIDERS_SCHEMA = 'org.gnome.desktop.search-providers';

export default class SnapdSearchProviderExtension extends Extension {
    enable() {
        const { snapProtoHandler } = SnapFinderSearchProvider;
        if (!snapProtoHandler) {
            log('No snap protocol handler, skip Snap search provider');
            return;
        }

        const {searchController} = this;
        if (!searchController) {
            log('No search controller available, skip Snap search provider');
            return;
        }

        this.snapFinder = new SnapFinderSearchProvider();
        this._registerProvider(this.snapFinder, searchController);
        this._monitorSearchSettings(searchController);
    }

    disable() {
        if (!this.snapFinder)
            return;

        this._searchSettings.disconnectObject(this);
        delete this._searchSettings;

        this._unregisterProvider(this.snapFinder);
        delete this.snapFinder;
    }

    _registerProvider(provider, searchController) {
        searchController.addProvider(provider);
        provider.init();
    }

    _unregisterProvider(provider) {
        this.searchController?.removeProvider(provider);
        provider.destroy();
    }

    _monitorSearchSettings(searchController) {
        const settings = searchController._searchResults?._searchSettings ??
            new Gio.Settings({schema_id: SEARCH_PROVIDERS_SCHEMA});
        this._searchSettings = settings;

        const reloadSettingsSignals = [
            'changed::disabled',
            'changed::enabled',
            'changed::disable-external',
            'changed::sort-order',
        ];
        const connectArgs = reloadSettingsSignals.flatMap(signal =>
            [signal, () => this._ensureProviderRegistered(), GObject.ConnectFlags.AFTER]);
        settings.connectObject(...connectArgs, this);
    }

    _ensureProviderRegistered() {
        const {searchController} = this;
        const searchResults = searchController?._searchResults;
        if (!searchResults)
            return;

        if (searchResults._providers.includes(this.snapFinder))
            return;

        this._registerProvider(this.snapFinder, searchController);
    }

    get searchController() {
        if (!Main.sessionMode.hasOverview)
            return null;

        return Main.overview?.searchController ?? null;
    }
}
