// file: kb_demo_app/frontend/ts/main.ts

import * as bootstrap from "bootstrap";
import "simplebar";
import ResizeObserver from "resize-observer-polyfill";
import { invoke } from "@tauri-apps/api/core";
import { debug, takeoverConsole } from "@fltsci/tauri-plugin-tracing";

(window as Window & typeof globalThis & { bootstrap?: typeof bootstrap }).bootstrap = bootstrap;
(window as Window & typeof globalThis & { ResizeObserver?: typeof ResizeObserver }).ResizeObserver = ResizeObserver;

async function openDemoWsWindow(): Promise<void> {
    try {
        await invoke("open_demo_ws_window");
    } catch (error) {
        console.error("open_demo_ws_window failed:", error);
    }
}
async function openDemoHttpWindow(): Promise<void> {
    try {
        await invoke("open_demo_http_window");
    } catch (error) {
        console.error("open_demo_http_window failed:", error);
    }
}

async function openDemoWsManagerWindow(): Promise<void> {
    try {
        await invoke("open_demo_ws_manager_window");
    } catch (error) {
        console.error("open_demo_ws_manager_window failed:", error);
    }
}


async function openDemo3Window(): Promise<void> {
    try {
        await invoke("open_demo3_window");
    } catch (error) {
        console.error("open_demo3_window failed:", error);
    }
}


async function openDemo3oldWindow(): Promise<void> {
    try {
        await invoke("open_demo3old_window");
    } catch (error) {
        console.error("open_demo3old_window failed:", error);
    }
}

async function openDemoPipelineWindow(): Promise<void> {
    try {
        await invoke("open_demo_pipeline_window");
    } catch (error) {
        console.error("open_demo_pipeline_window failed:", error);
    }
}

async function openDemoPipeline2Window(): Promise<void> {
    try {
        await invoke("open_demo_pipeline2_window");
    } catch (error) {
        console.error("open_demo_pipeline2_window2 failed:", error);
    }
}
document.addEventListener("DOMContentLoaded", async () => {
    void takeoverConsole();

    debug("main window loaded");

    const sidebarToggle = document.querySelector<HTMLButtonElement>('#sidebarToggle');
    if (sidebarToggle) {
        // restaurer l’état depuis localStorage
        if (localStorage.getItem('sidebar-toggle') === 'true') {
            document.body.classList.add('sidenav-toggled');
        }

        sidebarToggle.addEventListener('click', (event) => {
            event.preventDefault();
            document.body.classList.toggle('sidenav-toggled');
            localStorage.setItem('sidebar-toggle', document.body.classList.contains('sidenav-toggled') ? 'true' : 'false');
        });
    }

    const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
    Array.from(tooltipTriggerList).map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
    const toastElList = document.querySelectorAll('.toast');
    Array.from(toastElList).map(toastEl => new bootstrap.Toast(toastEl));
    const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]');
    Array.from(popoverTriggerList).map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));

    const gobackto = location.pathname + location.search;

    document.querySelectorAll<HTMLAnchorElement>('a[data-setlang]').forEach((a) => {
        const href = a.getAttribute("href");
        if (!href) return; // pas de href => on ignore

        const url = new URL(href, location.origin);
        url.searchParams.set("gobackto", gobackto);

        // conserve une URL relative (path + query)
        a.setAttribute("href", url.pathname + "?" + url.searchParams.toString());
    });

    const openDemoWsButton = document.querySelector<HTMLButtonElement>("#openDemoWsButton");
    const openDemoWsButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemoWsButtonSecondary");

    const openDemoHttpButton = document.querySelector<HTMLButtonElement>("#openDemoHttpButton");
    const openDemoHttpButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemoHttpButtonSecondary");
    const openDemoWsManagerButton = document.querySelector<HTMLButtonElement>("#openDemoWsManagerButton");
    const openDemoWsManagerButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemoWsManagerButtonSecondary");
    const openDemo3Button = document.querySelector<HTMLButtonElement>("#openDemo3Button");
    const openDemo3ButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemo3ButtonSecondary");
    const openDemo3oldButton = document.querySelector<HTMLButtonElement>("#openDemo3oldButton");
    const openDemo3oldButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemo3oldButtonSecondary");
    const openDemoPipelineButton = document.querySelector<HTMLButtonElement>("#openDemoPipelineButton");
    const openDemoPipelineButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemoPipelineButtonSecondary");
    const openDemoPipeline2Button = document.querySelector<HTMLButtonElement>("#openDemoPipeline2Button");
    const openDemoPipeline2ButtonSecondary = document.querySelector<HTMLButtonElement>("#openDemoPipeline2ButtonSecondary");

    if (openDemoWsButton) {
        openDemoWsButton.addEventListener("click", () => {
            void openDemoWsWindow();
        });
    }

    if (openDemoWsButtonSecondary) {
        openDemoWsButtonSecondary.addEventListener("click", () => {
            void openDemoWsWindow();
        });
    }

    if (openDemoHttpButton) {
        openDemoHttpButton.addEventListener("click", () => {
            void openDemoHttpWindow();
        });
    }

    if (openDemoHttpButtonSecondary) {
        openDemoHttpButtonSecondary.addEventListener("click", () => {
            void openDemoHttpWindow();
        });
    }

    if (openDemoWsManagerButton) {
        openDemoWsManagerButton.addEventListener("click", () => {
            void openDemoWsManagerWindow();
        });
    }

    if (openDemoWsManagerButtonSecondary) {
        openDemoWsManagerButtonSecondary.addEventListener("click", () => {
            void openDemoWsManagerWindow();
        });
    }

    if (openDemo3Button) {
        openDemo3Button.addEventListener("click", () => {
            void openDemo3Window();
        });
    }

    if (openDemo3ButtonSecondary) {
        openDemo3ButtonSecondary.addEventListener("click", () => {
            void openDemo3Window();
        });
    }

    if (openDemo3oldButton) {
        openDemo3oldButton.addEventListener("click", () => {
            void openDemo3oldWindow();
        });
    }

    if (openDemo3oldButtonSecondary) {
        openDemo3oldButtonSecondary.addEventListener("click", () => {
            void openDemo3oldWindow();
        });
    }

    if (openDemoPipelineButton) {
        openDemoPipelineButton.addEventListener("click", () => {
            void openDemoPipelineWindow();
        });
    }

    if (openDemoPipelineButtonSecondary) {
        openDemoPipelineButtonSecondary.addEventListener("click", () => {
            void openDemoPipelineWindow();
        });
    }

    if (openDemoPipeline2Button) {
        openDemoPipeline2Button.addEventListener("click", () => {
            void openDemoPipeline2Window();
        });
    }

    if (openDemoPipeline2ButtonSecondary) {
        openDemoPipeline2ButtonSecondary.addEventListener("click", () => {
            void openDemoPipeline2Window();
        });
    }

});