/*
 * Copyright (C) 2023  Yomitan Authors
 * Copyright (C) 2019-2022  Yomichan Authors
 *
 * 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 3 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 <https://www.gnu.org/licenses/>.
 */

import {FrameOffsetForwarder} from '../comm/frame-offset-forwarder.js';
import {generateId} from '../core.js';
import {yomitan} from '../yomitan.js';
import {PopupProxy} from './popup-proxy.js';
import {PopupWindow} from './popup-window.js';
import {Popup} from './popup.js';

/**
 * A class which is used to generate and manage popups.
 */
export class PopupFactory {
    /**
     * Creates a new instance.
     * @param {number} frameId The frame ID of the host frame.
     */
    constructor(frameId) {
        /** @type {number} */
        this._frameId = frameId;
        /** @type {FrameOffsetForwarder} */
        this._frameOffsetForwarder = new FrameOffsetForwarder(frameId);
        /** @type {Map<string, import('popup').PopupAny>} */
        this._popups = new Map();
        /** @type {Map<string, {popup: import('popup').PopupAny, token: string}[]>} */
        this._allPopupVisibilityTokenMap = new Map();
    }

    /**
     * Prepares the instance for use.
     */
    prepare() {
        this._frameOffsetForwarder.prepare();
        yomitan.crossFrame.registerHandlers([
            ['PopupFactory.getOrCreatePopup',     {async: true,  handler: this._onApiGetOrCreatePopup.bind(this)}],
            ['PopupFactory.setOptionsContext',    {async: true,  handler: this._onApiSetOptionsContext.bind(this)}],
            ['PopupFactory.hide',                 {async: true,  handler: this._onApiHide.bind(this)}],
            ['PopupFactory.isVisible',            {async: true,  handler: this._onApiIsVisibleAsync.bind(this)}],
            ['PopupFactory.setVisibleOverride',   {async: true,  handler: this._onApiSetVisibleOverride.bind(this)}],
            ['PopupFactory.clearVisibleOverride', {async: true,  handler: this._onApiClearVisibleOverride.bind(this)}],
            ['PopupFactory.containsPoint',        {async: true,  handler: this._onApiContainsPoint.bind(this)}],
            ['PopupFactory.showContent',          {async: true,  handler: this._onApiShowContent.bind(this)}],
            ['PopupFactory.setCustomCss',         {async: true,  handler: this._onApiSetCustomCss.bind(this)}],
            ['PopupFactory.clearAutoPlayTimer',   {async: true,  handler: this._onApiClearAutoPlayTimer.bind(this)}],
            ['PopupFactory.setContentScale',      {async: true,  handler: this._onApiSetContentScale.bind(this)}],
            ['PopupFactory.updateTheme',          {async: true,  handler: this._onApiUpdateTheme.bind(this)}],
            ['PopupFactory.setCustomOuterCss',    {async: true,  handler: this._onApiSetCustomOuterCss.bind(this)}],
            ['PopupFactory.getFrameSize',         {async: true,  handler: this._onApiGetFrameSize.bind(this)}],
            ['PopupFactory.setFrameSize',         {async: true,  handler: this._onApiSetFrameSize.bind(this)}]
        ]);
    }

    /**
     * Gets or creates a popup based on a set of parameters
     * @param {import('popup-factory').GetOrCreatePopupDetails} details Details about how to acquire the popup.
     * @returns {Promise<import('popup').PopupAny>}
     */
    async getOrCreatePopup({
        frameId=null,
        id=null,
        parentPopupId=null,
        depth=null,
        popupWindow=false,
        childrenSupported=false
    }) {
        // Find by existing id
        if (id !== null) {
            const popup = this._popups.get(id);
            if (typeof popup !== 'undefined') {
                return popup;
            }
        }

        // Find by existing parent id
        let parent = null;
        if (parentPopupId !== null) {
            parent = this._popups.get(parentPopupId);
            if (typeof parent !== 'undefined') {
                const popup = parent.child;
                if (popup !== null) {
                    return popup;
                }
            } else {
                parent = null;
            }
        }

        // Depth
        if (parent !== null) {
            if (depth !== null) {
                throw new Error('Depth cannot be set when parent exists');
            }
            depth = parent.depth + 1;
        } else if (depth === null) {
            depth = 0;
        }

        if (popupWindow) {
            // New unique id
            if (id === null) {
                id = generateId(16);
            }
            const popup = new PopupWindow({
                id,
                depth,
                frameId: this._frameId
            });
            this._popups.set(id, popup);
            return popup;
        } else if (frameId === this._frameId) {
            // New unique id
            if (id === null) {
                id = generateId(16);
            }
            const popup = new Popup({
                id,
                depth,
                frameId: this._frameId,
                childrenSupported
            });
            if (parent !== null) {
                if (parent.child !== null) {
                    throw new Error('Parent popup already has a child');
                }
                popup.parent = /** @type {Popup} */ (parent);
                parent.child = popup;
            }
            this._popups.set(id, popup);
            popup.prepare();
            return popup;
        } else {
            if (frameId === null) {
                throw new Error('Invalid frameId');
            }
            const useFrameOffsetForwarder = (parentPopupId === null);
            /** @type {{id: string, depth: number, frameId: number}} */
            const info = await yomitan.crossFrame.invoke(frameId, 'PopupFactory.getOrCreatePopup', /** @type {import('popup-factory').GetOrCreatePopupDetails} */ ({
                id,
                parentPopupId,
                frameId,
                childrenSupported
            }));
            id = info.id;
            const popup = new PopupProxy({
                id,
                depth: info.depth,
                frameId: info.frameId,
                frameOffsetForwarder: useFrameOffsetForwarder ? this._frameOffsetForwarder : null
            });
            this._popups.set(id, popup);
            return popup;
        }
    }

    /**
     * Force all popups to have a specific visibility value.
     * @param {boolean} value Whether or not the popups should be visible.
     * @param {number} priority The priority of the override.
     * @returns {Promise<import('core').TokenString>} A token which can be passed to clearAllVisibleOverride.
     * @throws An exception is thrown if any popup fails to have its visibiltiy overridden.
     */
    async setAllVisibleOverride(value, priority) {
        const promises = [];
        for (const popup of this._popups.values()) {
            const promise = this._setPopupVisibleOverrideReturnTuple(popup, value, priority);
            promises.push(promise);
        }

        /** @type {undefined|unknown} */
        let error = void 0;
        /** @type {{popup: import('popup').PopupAny, token: string}[]} */
        const results = [];
        for (const promise of promises) {
            try {
                const {popup, token} = await promise;
                if (token !== null) {
                    results.push({popup, token});
                }
            } catch (e) {
                if (typeof error === 'undefined') {
                    error = new Error(`Failed to set popup visibility override: ${e}`);
                }
            }
        }

        if (typeof error === 'undefined') {
            const token = generateId(16);
            this._allPopupVisibilityTokenMap.set(token, results);
            return token;
        }

        // Revert on error
        await this._revertPopupVisibilityOverrides(results);
        throw error;
    }

    /**
     * @param {import('popup').PopupAny} popup
     * @param {boolean} value
     * @param {number} priority
     * @returns {Promise<{popup: import('popup').PopupAny, token: ?string}>}
     */
    async _setPopupVisibleOverrideReturnTuple(popup, value, priority) {
        const token = await popup.setVisibleOverride(value, priority);
        return {popup, token};
    }

    /**
     * Clears a visibility override that was generated by `setAllVisibleOverride`.
     * @param {import('core').TokenString} token The token returned from `setAllVisibleOverride`.
     * @returns {Promise<boolean>} `true` if the override existed and was removed, `false` otherwise.
     */
    async clearAllVisibleOverride(token) {
        const results = this._allPopupVisibilityTokenMap.get(token);
        if (typeof results === 'undefined') { return false; }

        this._allPopupVisibilityTokenMap.delete(token);
        await this._revertPopupVisibilityOverrides(results);
        return true;
    }

    // API message handlers

    /**
     * @param {import('popup-factory').GetOrCreatePopupDetails} details
     * @returns {Promise<{id: string, depth: number, frameId: number}>}
     */
    async _onApiGetOrCreatePopup(details) {
        const popup = await this.getOrCreatePopup(details);
        return {
            id: popup.id,
            depth: popup.depth,
            frameId: popup.frameId
        };
    }

    /**
     * @param {{id: string, optionsContext: import('settings').OptionsContext}} params
     */
    async _onApiSetOptionsContext({id, optionsContext}) {
        const popup = this._getPopup(id);
        await popup.setOptionsContext(optionsContext);
    }

    /**
     * @param {{id: string, changeFocus: boolean}} params
     */
    async _onApiHide({id, changeFocus}) {
        const popup = this._getPopup(id);
        await popup.hide(changeFocus);
    }

    /**
     * @param {{id: string}} params
     * @returns {Promise<boolean>}
     */
    async _onApiIsVisibleAsync({id}) {
        const popup = this._getPopup(id);
        return await popup.isVisible();
    }

    /**
     * @param {{id: string, value: boolean, priority: number}} params
     * @returns {Promise<?import('core').TokenString>}
     */
    async _onApiSetVisibleOverride({id, value, priority}) {
        const popup = this._getPopup(id);
        return await popup.setVisibleOverride(value, priority);
    }

    /**
     * @param {{id: string, token: import('core').TokenString}} params
     * @returns {Promise<boolean>}
     */
    async _onApiClearVisibleOverride({id, token}) {
        const popup = this._getPopup(id);
        return await popup.clearVisibleOverride(token);
    }

    /**
     * @param {{id: string, x: number, y: number}} params
     * @returns {Promise<boolean>}
     */
    async _onApiContainsPoint({id, x, y}) {
        const popup = this._getPopup(id);
        const offset = this._getPopupOffset(popup);
        x += offset.x;
        y += offset.y;
        return await popup.containsPoint(x, y);
    }

    /**
     * @param {{id: string, details: import('popup').ContentDetails, displayDetails: ?import('display').ContentDetails}} params
     * @returns {Promise<void>}
     */
    async _onApiShowContent({id, details, displayDetails}) {
        const popup = this._getPopup(id);
        if (!this._popupCanShow(popup)) { return; }

        const offset = this._getPopupOffset(popup);
        const {sourceRects} = details;
        for (const sourceRect of sourceRects) {
            sourceRect.left += offset.x;
            sourceRect.top += offset.y;
            sourceRect.right += offset.x;
            sourceRect.bottom += offset.y;
        }

        return await popup.showContent(details, displayDetails);
    }

    /**
     * @param {{id: string, css: string}} params
     * @returns {Promise<void>}
     */
    async _onApiSetCustomCss({id, css}) {
        const popup = this._getPopup(id);
        await popup.setCustomCss(css);
    }

    /**
     * @param {{id: string}} params
     * @returns {Promise<void>}
     */
    async _onApiClearAutoPlayTimer({id}) {
        const popup = this._getPopup(id);
        await popup.clearAutoPlayTimer();
    }

    /**
     * @param {{id: string, scale: number}} params
     * @returns {Promise<void>}
     */
    async _onApiSetContentScale({id, scale}) {
        const popup = this._getPopup(id);
        await popup.setContentScale(scale);
    }

    /**
     * @param {{id: string}} params
     * @returns {Promise<void>}
     */
    async _onApiUpdateTheme({id}) {
        const popup = this._getPopup(id);
        await popup.updateTheme();
    }

    /**
     * @param {{id: string, css: string, useWebExtensionApi: boolean}} params
     * @returns {Promise<void>}
     */
    async _onApiSetCustomOuterCss({id, css, useWebExtensionApi}) {
        const popup = this._getPopup(id);
        await popup.setCustomOuterCss(css, useWebExtensionApi);
    }

    /**
     * @param {{id: string}} params
     * @returns {Promise<import('popup').ValidSize>}
     */
    async _onApiGetFrameSize({id}) {
        const popup = this._getPopup(id);
        return await popup.getFrameSize();
    }

    /**
     * @param {{id: string, width: number, height: number}} params
     * @returns {Promise<boolean>}
     */
    async _onApiSetFrameSize({id, width, height}) {
        const popup = this._getPopup(id);
        return await popup.setFrameSize(width, height);
    }

    // Private functions

    /**
     * @param {string} id
     * @returns {import('popup').PopupAny}
     * @throws {Error}
     */
    _getPopup(id) {
        const popup = this._popups.get(id);
        if (typeof popup === 'undefined') {
            throw new Error(`Invalid popup ID ${id}`);
        }
        return popup;
    }

    /**
     * @param {import('popup').PopupAny} popup
     * @returns {{x: number, y: number}}
     */
    _getPopupOffset(popup) {
        const {parent} = popup;
        if (parent !== null) {
            const popupRect = parent.getFrameRect();
            if (popupRect.valid) {
                return {x: popupRect.left, y: popupRect.top};
            }
        }
        return {x: 0, y: 0};
    }

    /**
     * @param {import('popup').PopupAny} popup
     * @returns {boolean}
     */
    _popupCanShow(popup) {
        const parent = popup.parent;
        return parent === null || parent.isVisibleSync();
    }

    /**
     * @param {{popup: import('popup').PopupAny, token: string}[]} overrides
     * @returns {Promise<boolean[]>}
     */
    async _revertPopupVisibilityOverrides(overrides) {
        const promises = [];
        for (const value of overrides) {
            if (value === null) { continue; }
            const {popup, token} = value;
            const promise = popup.clearVisibleOverride(token)
                .then(
                    (v) => v,
                    () => false
                );
            promises.push(promise);
        }
        return await Promise.all(promises);
    }
}