// UI Extension for Aladin - Custom buttons, controls, etc.
(function () {
    'use strict';

    const createUIExtension = (aladinInstance) => {
        return {
            customButtons: new Map(),

            initialize: async function () {
            },

            // Add custom Aladin button with positioning
            addAladinButton: function (options) {
                if (!window.A || !this.aladin) {
                    console.error('Aladin not available or instance not provided');
                    return null;
                }

                const config = {
                    content: options.content || 'Button',
                    classList: options.classList || [],
                    tooltip: options.tooltip || options.title || null,
                    position: options.position || 'top-right',
                    onClick: options.onClick || null
                };

                try {
                    let btn = A.button({
                        content: config.content,
                        classList: config.classList,
                        tooltip: config.tooltip,
                        action: () => {
                            if (config.onClick && config.onClick.invokeMethodAsync) {
                                // .NET callback
                                config.onClick.invokeMethodAsync('CycleFovAsync');
                            } else {
                            }
                        }
                    });

                    this.aladin.addUI(btn);

                    // Apply custom positioning
                    setTimeout(() => {
                        const btnElement = btn._element || btn.element;
                        if (btnElement) {
                            this.applyButtonPosition(btnElement, config.position);
                            this.applyButtonStyling(btnElement);
                        }
                    }, 100);

                    // Store button reference
                    const buttonId = config.classList.join('-') || 'custom-btn-' + Date.now();
                    this.customButtons.set(buttonId, btn);

                    return btn;
                } catch (error) {
                    console.error('Error creating Aladin button:', error);
                    return null;
                }
            },

            applyButtonPosition: function (btnElement, position) {
                btnElement.style.position = 'absolute';

                switch (position) {
                    case 'bottom-left':
                        btnElement.style.bottom = '6px';
                        btnElement.style.left = '10px';
                        btnElement.style.top = 'auto';
                        btnElement.style.right = 'auto';
                        break;
                    case 'bottom-right':
                        btnElement.style.bottom = '10px';
                        btnElement.style.right = '10px';
                        btnElement.style.top = 'auto';
                        btnElement.style.left = 'auto';
                        break;
                    case 'top-left':
                        btnElement.style.top = '10px';
                        btnElement.style.left = '10px';
                        btnElement.style.bottom = 'auto';
                        btnElement.style.right = 'auto';
                        break;
                    default: // top-right
                        btnElement.style.top = '10px';
                        btnElement.style.right = '10px';
                        btnElement.style.bottom = 'auto';
                        btnElement.style.left = 'auto';
                        break;
                }
            },

            applyButtonStyling: function (btnElement) {
                btnElement.style.zIndex = '1000';
                btnElement.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
                btnElement.style.color = 'white';
                btnElement.style.border = '1px solid #ccc';
                btnElement.style.borderRadius = '4px';
                btnElement.style.padding = '6px 12px';
                btnElement.style.fontSize = '12px';
                btnElement.style.cursor = 'pointer';
            },

            updateAladinButtonText: function (selector, newText) {
                try {
                    const btnElement = document.querySelector(selector);
                    if (btnElement) {
                        btnElement.textContent = newText;
                    }
                } catch (error) {
                    console.error('Error updating button text:', error);
                }
            },

            // Remove custom button
            removeButton: function (buttonId) {
                if (this.customButtons.has(buttonId)) {
                    const btn = this.customButtons.get(buttonId);
                    // Remove from Aladin UI if possible
                    this.customButtons.delete(buttonId);
                    return true;
                }
                return false;
            }
        };
    };

    // Register the extension
    if (window.AladinExtensions) {
        window.AladinExtensions.registerExtension('ui-controls', createUIExtension);
    } else {
        console.error('AladinExtensions not found. Make sure aladin-core.js is loaded first.');
    }

})();
