// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { injectable, inject, postConstruct } from 'inversify';
import { NewWindowOptions } from '../../common/window';
import { DefaultWindowService } from '../../browser/window/default-window-service';
import { ElectronMainWindowService } from '../../electron-common/electron-main-window-service';
import { ElectronWindowPreferences, PREF_WINDOW_ZOOM_LEVEL } from '../../electron-common/electron-window-preferences';
import { ConnectionCloseService } from '../../common/messaging/connection-management';
import { FrontendIdProvider } from '../../browser/messaging/frontend-id-provider';
import { WindowReloadOptions } from '../../browser/window/window-service';
import { Listener, ListenerList } from '../../common/listener';

@injectable()
export class ElectronWindowService extends DefaultWindowService {

    /**
     * Lock to prevent multiple parallel executions of the `beforeunload` listener.
     */
    protected isUnloading: boolean = false;

    /**
     * Close the window right away when `true`, else check if we can unload.
     */
    protected closeOnUnload: boolean = false;

    @inject(FrontendIdProvider)
    protected readonly frontendIdProvider: FrontendIdProvider;

    @inject(ElectronMainWindowService)
    protected readonly delegate: ElectronMainWindowService;

    @inject(ElectronWindowPreferences)
    protected readonly electronWindowPreferences: ElectronWindowPreferences;

    @inject(ConnectionCloseService)
    protected readonly connectionCloseService: ConnectionCloseService;

    protected readonly onWillShutDownListeners = new ListenerList<void, Promise<void>>();
    readonly onWillShutDown: Listener.Registration<void, Promise<void>> = this.onWillShutDownListeners.registration;

    override openNewWindow(url: string, { external }: NewWindowOptions = {}): undefined {
        this.delegate.openNewWindow(url, { external });
        return undefined;
    }

    override async openNewDefaultWindow(params?: WindowReloadOptions): Promise<number> {
        return this.delegate.openNewDefaultWindow(params?.search);
    }

    override closeWindow(windowId: number): void {
        this.delegate.closeWindow(windowId);
    }

    override focus(): void {
        window.electronTheiaCore.focusWindow();
    }
    @postConstruct()
    protected init(): void {
        // Update the default zoom level on startup when the preferences event is fired.
        this.electronWindowPreferences.onPreferenceChanged(e => {
            if (e.preferenceName === PREF_WINDOW_ZOOM_LEVEL) {
                this.updateWindowZoomLevel();
            }
        });
        window.electronTheiaCore.onAboutToClose(() => {
            this.connectionCloseService.markForClose(this.frontendIdProvider.getId());
        });
    }

    protected override registerUnloadListeners(): void {
        window.electronTheiaCore.setCloseRequestHandler(async reason => {
            const willShutDown = await this.isSafeToShutDown(reason);
            if (willShutDown) {
                await Listener.awaitAll(undefined, this.onWillShutDownListeners);
            }
            return willShutDown;
        });
        // Intentionally not calling `super`: in Electron the close request handler above takes the role of
        // `beforeunload`, and there is no back/forward cache, so only the `pagehide` listener is shared.
        this.registerPageHideListener();
    }

    /**
     * Updates the window zoom level based on the preference value.
     */
    protected async updateWindowZoomLevel(): Promise<void> {
        const preferredZoomLevel = this.electronWindowPreferences[PREF_WINDOW_ZOOM_LEVEL];
        if (await window.electronTheiaCore.getZoomLevel() !== preferredZoomLevel) {
            window.electronTheiaCore.setZoomLevel(preferredZoomLevel);
        }
    }

    override reload(params?: WindowReloadOptions): void {
        if (params) {
            const newLocation = new URL(location.href);
            if (params.search) {
                const query = Object.entries(params.search).map(([name, value]) => `${name}=${value}`).join('&');
                newLocation.search = query;
            }
            if (params.hash) {
                newLocation.hash = '#' + params.hash;
            }
            window.electronTheiaCore.requestReload(newLocation.toString());
        } else {
            window.electronTheiaCore.requestReload();
        }
    }
}
