/**
 * @fileoverview bs-gen-co.js
 * Koordiniert das Bottom Sheet. 
 * Hält den fachlichen State (currentGid) und reichert UI-Events mit Kontext an.
 */

import { updateBsForPoi } from './bs-poi-co.js';
import { updateBsForCluster } from './bs-cluster-co.js';
import { handlePoilessSelectionBs } from './bs-poiless-co.js';

import { showToast, mylog } from './utilities.js';
import { addDirectionHint } from './navigation-co.js';


class BottomSheetCoordinator {
    constructor() {
        this.ui = null;
        this.backend = null;
        this.mapState = null;
        this.appEvents = null;

        // DER ZENTRALE STATE
        this.currentGid = null;
        this.currentUpdateUI = null;
        this.currentAddUI = null;
    }

    /**
     * Initialisiert den Coordinator und registriert die globalen Event-Listener.
     */
    init(bottomSheetUI, backendService, mapState, appEvents, insertUI, updateUI) {
        if (this.ui) return;

        this.ui = bottomSheetUI;
        this.backend = backendService;
        this.mapState = mapState;
        this.appEvents = appEvents;

        // UI-Referenzen fest im State verankern
        this.currentAddUI = insertUI;
        this.currentUpdateUI = updateUI;

        if (!this.appEvents) {
            console.error("BottomSheetCoordinator: Kritischer Fehler - Event-Bus nicht gefunden!");
            return;
        }

        this._setupEventListeners();
        mylog("BottomSheetCoordinator: Initialisiert.");
    }

    _setupEventListeners() {



    }


    handleBsUpdate(clickedObject, addButtonUI, updatePoiUI) {
        mylog(`DEBUG 2: handleBsUpdate START gid=${clickedObject?.options?.gid}`);
        if (!this.ui) return;

        if (typeof this.ui.setPhoto === 'function') {
            this.ui.setPhoto(null);
        }

        if (addButtonUI) this.currentAddUI = addButtonUI;
        if (updatePoiUI) this.currentUpdateUI = updatePoiUI;

        this.currentGid = clickedObject?.options?.gid || null;
        this.ui.setClickedObject(clickedObject);

        const allParams = {
            bottomSheetUI: this.ui,
            backendService: this.backend,
            mapState: this.mapState,
            insertPoiUI: this.currentAddUI,
            updatePoiUI: this.currentUpdateUI,
            addDirectionHint
        };

        if (clickedObject && typeof clickedObject.getAllChildMarkers === 'function') {
            updateBsForCluster(clickedObject, this.ui, this.mapState.activeMarkerOrClusterGID, addDirectionHint);
            this.ui.open(clickedObject);
        }
        else if (this.currentGid && this.currentGid !== 'new') {
            updateBsForPoi(this.currentGid, allParams);
            this.ui.open();
        }
        else if (clickedObject && typeof clickedObject.getLatLng === 'function') {
            handlePoilessSelectionBs(clickedObject, this.ui, this.backend, addDirectionHint);
            this.ui.open();
        }
    }

}

const instance = new BottomSheetCoordinator();


export { instance as BottomSheetCoordinatorInstance };