// *****************************************************************************
// 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 { inject, injectable, named } from 'inversify';
import { Event, Emitter, ILogger } from '../../common';
import { CorePreferences } from '../../common/core-preferences';
import { ContributionProvider } from '../../common/contribution-provider';
import { FrontendApplicationContribution, OnWillStopAction } from '../frontend-application-contribution';
import { WindowService } from './window-service';
import { DEFAULT_WINDOW_HASH } from '../../common/window';
import { confirmExit } from '../dialogs';
import { StopReason } from '../../common/frontend-application-state';
import { FrontendApplication } from '../frontend-application';

@injectable()
export class DefaultWindowService implements WindowService, FrontendApplicationContribution {

    protected frontendApplication: FrontendApplication;
    protected allowVetoes = true;
    protected unloaded = false;

    protected onUnloadEmitter = new Emitter<void>();
    get onUnload(): Event<void> {
        return this.onUnloadEmitter.event;
    }

    @inject(CorePreferences)
    protected readonly corePreferences: CorePreferences;

    @inject(ContributionProvider)
    @named(FrontendApplicationContribution)
    protected readonly contributions: ContributionProvider<FrontendApplicationContribution>;

    @inject(ILogger) @named('core:DefaultWindowService')
    protected readonly logger: ILogger;

    onStart(app: FrontendApplication): void {
        this.frontendApplication = app;
        this.registerUnloadListeners();
    }

    openNewWindow(url: string): undefined {
        window.open(url, undefined, 'noopener');
        return undefined;
    }

    async openNewDefaultWindow(): Promise<number> {
        this.openNewWindow(`#${DEFAULT_WINDOW_HASH}`);
        return -1;
    }

    closeWindow(windowId: number): void {
        // No-op in browser-only mode
    }

    focus(): void {
        window.focus();
    }

    /**
     * Returns a list of actions that {@link FrontendApplicationContribution}s would like to take before shutdown
     * It is expected that this will succeed - i.e. return an empty array - at most once per session. If no vetoes are received
     * during any cycle, no further checks will be made. In that case, shutdown should proceed unconditionally.
     */
    protected collectContributionUnloadVetoes(): OnWillStopAction[] {
        const vetoes: OnWillStopAction[] = [];
        if (this.allowVetoes) {
            const shouldConfirmExit = this.corePreferences['application.confirmExit'];
            for (const contribution of this.contributions.getContributions()) {
                const veto = contribution.onWillStop?.(this.frontendApplication);
                if (veto && shouldConfirmExit !== 'never') { // Ignore vetoes if we should not prompt the user on exit.
                    if (OnWillStopAction.is(veto)) {
                        vetoes.push(veto);
                    } else {
                        vetoes.push({ reason: 'No reason given', action: () => false });
                    }
                }
            }
            vetoes.sort((a, b) => (a.priority ?? -Infinity) - (b.priority ?? -Infinity));
            if (vetoes.length === 0 && shouldConfirmExit === 'always') {
                vetoes.push({ reason: 'application.confirmExit preference', action: () => confirmExit() });
            }
            if (vetoes.length === 0) {
                this.allowVetoes = false;
            }
        }
        return vetoes;
    }

    /**
     * Implement the mechanism to detect unloading of the page.
     */
    protected registerUnloadListeners(): void {
        // If `beforeunload` is cancelled and the user stays, the page is not unloaded, so `pagehide` is not fired.
        window.addEventListener('beforeunload', event => this.handleBeforeUnloadEvent(event));
        this.registerPageHideListener();
        window.addEventListener('pageshow', event => this.handlePageShow(event));
    }

    /**
     * `pagehide` is used instead of the deprecated `unload` event, which Chrome may block
     * via permissions policy (https://developer.chrome.com/docs/web-platform/deprecating-unload),
     * silently skipping the handler and thus e.g. losing the layout.
     */
    protected registerPageHideListener(): void {
        window.addEventListener('pagehide', () => this.handlePageHide());
    }

    /**
     * `pagehide` also fires when the page enters the back/forward cache, in which case the frontend
     * has already shut down (state saved, connections closed). Reload to get a working application
     * again if the page is restored from the cache. That shutdown already happened, so the reload
     * must not be vetoed by the contributions.
     */
    protected handlePageShow(event: PageTransitionEvent): void {
        if (event.persisted) {
            this.setSafeToShutDown();
            this.reload();
        }
    }

    /**
     * Fires {@link onUnload} at most once per document: `pagehide` fires again for the reload that
     * recovers a page restored from the back/forward cache, but the application is already stopped.
     */
    protected handlePageHide(): void {
        if (!this.unloaded) {
            this.unloaded = true;
            this.onUnloadEmitter.fire();
        }
    }

    async isSafeToShutDown(stopReason: StopReason): Promise<boolean> {
        const vetoes = this.collectContributionUnloadVetoes();
        if (vetoes.length === 0) {
            return true;
        }
        const preparedValues = await Promise.all(vetoes.map(e => e.prepare?.(stopReason)));
        this.logger.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
        for (let i = 0; i < vetoes.length; i++) {
            try {
                const result = await vetoes[i].action(preparedValues[i], stopReason);
                if (!result) {
                    return false;
                }
            } catch (e) {
                this.logger.error(e);
            }
        }
        this.logger.debug('OnWillStop actions resolved; allowing shutdown');
        this.allowVetoes = false;
        return true;
    }

    setSafeToShutDown(): void {
        this.allowVetoes = false;
    }

    /**
     * Called when the `window` is about to `unload` its resources.
     * At this point, the `document` is still visible and the [`BeforeUnloadEvent`](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event)
     * event will be canceled if the return value of this method is `false`.
     *
     * In Electron, handleCloseRequestEvent is is run instead.
     */
    protected handleBeforeUnloadEvent(event: BeforeUnloadEvent): string | void {
        const vetoes = this.collectContributionUnloadVetoes();
        if (vetoes.length) {
            // In the browser, we don't call the functions because this has to finish in a single tick, so we treat any desired action as a veto.
            this.logger.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
            return this.preventUnload(event);
        }
        this.logger.debug('Shutdown will proceed.');
    }

    /**
     * Notify the browser that we do not want to unload.
     *
     * Notes:
     *  - Shows a confirmation popup in browsers.
     *  - Prevents the window from closing without confirmation in electron.
     *
     * @param event The beforeunload event
     */
    protected preventUnload(event: BeforeUnloadEvent): string | void {
        event.returnValue = '';
        event.preventDefault();
        return '';
    }

    reload(): void {
        this.isSafeToShutDown(StopReason.Reload).then(isSafe => {
            if (isSafe) {
                window.location.reload();
            }
        });
    }
}
