import { mylog } from './sc-utilities.js';
// Bezug über Service
import { getMap } from './map-markers-co.js';

/**
 * AppShellCoordinator.js
 * Verwaltet die Events des statischen App-Shell-UI und leitet diese weiter.
 * Steuert die Sichtbarkeit globaler Elemente (Karte, Spinner, Controls).
 * @typedef {import('./app-shell-ui.js').AppShellUI} AppShellUI
 */
export class AppShellCoordinator {
    /** @type {AppShellUI} */
    #appShellUI;

    // Callbacks für den ApplicationCoordinator
    #onSearchTriggeredCallback = () => { };
    #onSettingsClickedCallback = () => { };
    #onAdminTriggeredCallback = () => { };


    /**
     * @param {AppShellUI} appShellUI - Die UI-Instanz, deren Events dieser Coordinator verwaltet.
     */
    constructor(appShellUI) {
        this.#appShellUI = appShellUI;
        this.#setupEventListeners();
        mylog("AppShellCoordinator: Initialisiert.");
    }


    /**
     * Richtet die globalen Event-Listener für die UI-Elemente ein.
     * @private
     */
    #setupEventListeners() {
        // 1. Suche-Trigger
        if (this.#appShellUI.searchTriggerElement) {
            this.#appShellUI.searchTriggerElement.addEventListener('click', () => {
                mylog("AppShellCoordinator: Search-Trigger geklickt.");
                this.#onSearchTriggeredCallback();
            });
        }

        // 2. Settings-Icon
        if (this.#appShellUI.settingsIconElement) {
            this.#appShellUI.settingsIconElement.addEventListener('click', (event) => {
                event.stopPropagation(); 
                mylog("AppShellCoordinator: Settings-Icon geklickt.");
                this.#onSettingsClickedCallback();
            });
        }
    }

    // ===============================================
    // 📢 PUBLISHER METHODEN
    // ===============================================

    onSearchTriggered(callback) {
        this.#onSearchTriggeredCallback = callback;
    }

    onSettingsClicked(callback) {
        this.#onSettingsClickedCallback = callback;
    }


    // ===============================================
    // ⚙️ STEUERUNGS-METHODEN
    // ===============================================

    /**
     * Macht die Karte und die globalen Steuerelemente sichtbar.
     */
    showMapAndControls() {
        if (this.#appShellUI.mapElement) {
            this.#appShellUI.mapElement.classList.remove('hidden');
        }
        if (this.#appShellUI.settingsIconElement) {
            this.#appShellUI.settingsIconElement.classList.remove('hidden');
        }
        if (this.#appShellUI.searchTriggerElement) {
            this.#appShellUI.searchTriggerElement.classList.remove('hidden');
        }

        // 💡 Minimalinvasive Anpassung: Map wird hier lokal via Service geholt
        const map = getMap();
        if (map) {
            setTimeout(() => {
                map.invalidateSize();
                mylog("AppShellCoordinator: Leaflet Map Größe neu validiert (invalidateSize).");
            }, 10);
        }

        mylog("AppShellCoordinator: Karte und Steuerelemente eingeblendet.");
    }

    /**
     * Macht den Lade-Spinner sichtbar.
     */
    showLoadingSpinner() {
        const spinner = document.getElementById('loading-spinner-content');
        if (spinner) {
            spinner.classList.remove('hidden');
            spinner.classList.remove('fade-out'); 
        }
    }

    /**
     * Blendet den Lade-Spinner aus (mit Transition).
     */
    hideLoadingSpinner() {
        const spinner = document.getElementById('loading-spinner-content');
        if (spinner) {
            spinner.classList.add('fade-out');
        }
    }

    /**
     * Aktualisiert den Text unter dem Lade-Spinner.
     * @param {string} text - Der anzuzeigende Text.
     */
    updateLoadingText(text) {
        const textElement = document.getElementById('estimated-time-display');
        if (textElement) {
            textElement.textContent = text;
        }
    }

    /**
     * Macht das Overlay zur Standort-Erlaubnis sichtbar.
     */
    showLocationPermissionOverlay() {
        const overlay = document.getElementById('location-permission-overlay');
        if (overlay) {
            overlay.classList.remove('hidden');
            overlay.style.opacity = 1;
            overlay.style.pointerEvents = 'auto';
        }
    }

    /**
     * Blendet das Overlay zur Standort-Erlaubnis aus.
     */
    hideLocationPermissionOverlay() {
        const overlay = document.getElementById('location-permission-overlay');
        if (overlay) {
            overlay.classList.add('hidden');
            overlay.style.pointerEvents = 'none';
        }
    }
}

/**
 * Hilfsfunktion zum Aktualisieren des Suchfeldes mit einer Adresse.
 */
export function updateSearchFieldWithAddress(address) {
    const placeholder = document.getElementById('search-address-placeholder-text');
    if (placeholder) {
        if (address) {
              placeholder.textContent = `${address.street || ''} ${address.houseNr || ''}, ${address.localDistrict || ''}`.trim();;
        } else {
            placeholder.textContent = 'Name oder Adresse';
        }
    }
}