// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { AppAction } from "./types";
import {
    getActiveTab,
    getTabByTitle,
    awaitPageLoad,
    awaitPageIncrementalUpdates,
} from "./tabManager";
import { getTabHTMLFragments, CompressionMode } from "./capture";

/**
 * Executes a browser action
 * @param action The action to execute
 * @returns Promise resolving to the result of the action
 */
export async function runBrowserAction(action: AppAction): Promise<any> {
    let responseObject = undefined;
    let confirmationMessage = "OK";
    const actionName =
        action.actionName ?? action.fullActionName?.split(".").at(-1);

    switch (actionName) {
        case "openTab": {
            if (action.parameters.url) {
                const tab = await chrome.tabs.create({
                    url: action.parameters.url,
                });

                await awaitPageLoad(tab);

                confirmationMessage = `Opened new tab to ${action.parameters.url}`;
            } else {
                if (action.parameters?.query) {
                    await chrome.search.query({
                        disposition: "NEW_TAB",
                        text: action.parameters.query,
                    });

                    confirmationMessage = `Opened new tab with query ${action.parameters.query}`;
                } else {
                    await chrome.tabs.create({});
                    confirmationMessage = "Opened new tab";
                }
            }

            break;
        }
        case "closeTab": {
            let targetTab: chrome.tabs.Tab | undefined = undefined;
            if (action.parameters?.title) {
                targetTab = await getTabByTitle(action.parameters.title);
            } else {
                targetTab = await getActiveTab();
            }

            if (targetTab && targetTab.id) {
                await chrome.tabs.remove(targetTab.id);
            }
            confirmationMessage = "Closed tab";
            break;
        }
        case "switchToTabByText": {
            const targetTab = await getTabByTitle(action.parameters.keywords);
            if (targetTab) {
                await chrome.tabs.update(targetTab.id!, {
                    active: true,
                });

                confirmationMessage = "Switched to tab";
            }

            break;
        }

        case "openFromHistory": {
            const targetTab = await getActiveTab();
            const historyItems = await chrome.history.search({
                text: action.parameters.keywords,
                maxResults: 1,
            });

            if (historyItems && historyItems.length > 0) {
                console.log(historyItems);
                if (targetTab?.id) {
                    chrome.tabs.update(targetTab.id, {
                        url: historyItems[0].url,
                    });
                } else {
                    chrome.tabs.create({
                        url: historyItems[0].url,
                    });
                }
            }

            break;
        }
        case "openFromBookmarks": {
            const OpenFromBookmarksItems = await chrome.bookmarks.search({
                query: action.parameters.keywords,
            });

            if (
                OpenFromBookmarksItems &&
                OpenFromBookmarksItems.length > 0 &&
                OpenFromBookmarksItems[0].url
            ) {
                console.log(OpenFromBookmarksItems);
                await chrome.tabs.create({
                    url: OpenFromBookmarksItems[0].url,
                });
            }

            break;
        }

        case "getHTML": {
            const targetTab = await getActiveTab();

            // Convert legacy fullHTML parameter to CompressionMode
            const compressionMode = action.parameters?.fullHTML
                ? CompressionMode.None
                : CompressionMode.Automation;
            responseObject = await getTabHTMLFragments(
                targetTab!,
                compressionMode,
                action.parameters?.downloadAsFile,
                action.parameters?.extractText,
                action.parameters?.useTimestampIds,
                action.parameters?.filterToReadingView,
                action.parameters?.keepMetaTags,
            );
            break;
        }
        case "awaitPageLoad": {
            const targetTab = await getActiveTab();
            await awaitPageLoad(targetTab!);
            await awaitPageIncrementalUpdates(targetTab!);
            responseObject = targetTab?.url;
            break;
        }
        case "clickOnElement": {
            const targetTab = await getActiveTab();
            const response = await chrome.tabs.sendMessage(targetTab?.id!, {
                type: "run_ui_event",
                action: action,
            });
            break;
        }
        case "enterTextInElement": {
            const targetTab = await getActiveTab();
            const response = await chrome.tabs.sendMessage(targetTab?.id!, {
                type: "run_ui_event",
                action: action,
            });
            break;
        }
        case "enterTextOnPage": {
            const targetTab = await getActiveTab();
            const response = await chrome.tabs.sendMessage(targetTab?.id!, {
                type: "run_ui_event",
                action: action,
            });
            break;
        }
        case "setDropdownValue": {
            const targetTab = await getActiveTab();
            const response = await chrome.tabs.sendMessage(targetTab?.id!, {
                type: "run_ui_event",
                action: action,
            });
            break;
        }
        case "getActionsForUrl": {
            // Enhanced action retrieval with ActionsStore support
            responseObject = await chrome.runtime.sendMessage({
                type: "getActionsForUrl",
                url: action.parameters.url,
                includeGlobal: action.parameters.includeGlobal,
                author: action.parameters.author,
            });
            break;
        }
        case "downloadContentWithBrowser": {
            const { handleDownloadContentWithBrowser } = await import(
                "./messageHandlers.js"
            );
            const result = await handleDownloadContentWithBrowser({
                url: action.parameters.url,
                options: action.parameters.options,
            });

            responseObject = result;
            confirmationMessage = result?.success
                ? "Content downloaded successfully"
                : "Content download failed";
            break;
        }
        case "processHtmlContent": {
            const { handleProcessHtmlContent } = await import(
                "./messageHandlers.js"
            );
            const result = await handleProcessHtmlContent({
                htmlContent: action.parameters.htmlContent,
                options: action.parameters.options,
            });

            responseObject = result;
            confirmationMessage = result?.success
                ? "HTML processed successfully"
                : "HTML processing failed";
            break;
        }

        default:
            throw new Error(`Unknown action: ${actionName}. `);
    }

    return {
        message: confirmationMessage,
        data: responseObject,
    };
}
