import { TradingDashboard, refreshTA, openAIAnalysis } from './ui/chart.js';
import { restoreSidebarState, toggleSidebar, initSidebarTabs, restoreSidebarTabState } from './ui/sidebar.js';
import { 
    initIndicatorPanel,
    getActiveIndicators,
    setActiveIndicators,
    drawIndicatorsOnChart,
    addIndicator,
    removeIndicatorById
} from './ui/indicators-panel-new.js';
import { initStrategyPanel } from './ui/strategy-panel.js';
import { IndicatorRegistry } from './indicators/index.js';
import { TimezoneConfig } from './config/timezone.js';

window.dashboard = null;

window.toggleSidebar = toggleSidebar;
window.refreshTA = refreshTA;
window.openAIAnalysis = openAIAnalysis;
window.TimezoneConfig = TimezoneConfig;
window.renderIndicatorList = function() {
    // This function is no longer needed for sidebar indicators
};

// Export init function for global access
window.initIndicatorPanel = initIndicatorPanel;
window.addIndicator = addIndicator;
window.toggleIndicator = addIndicator;
window.drawIndicatorsOnChart = drawIndicatorsOnChart;
window.updateIndicatorCandles = drawIndicatorsOnChart;

window.IndicatorRegistry = IndicatorRegistry;

document.addEventListener('DOMContentLoaded', async () => {
    // Attach toggle sidebar event listeners
    const toggleBtn = document.getElementById('sidebarToggleBtn');
    if (toggleBtn) {
        toggleBtn.addEventListener('click', toggleSidebar);
    }
    
    // Mobile menu button
    const mobileMenuBtn = document.getElementById('mobileMenuBtn');
    if (mobileMenuBtn) {
        mobileMenuBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            const sidebar = document.getElementById('rightSidebar');
            if (sidebar) {
                sidebar.classList.toggle('collapsed');
            }
        });
    }

    // Initialize timezone selector
    const timezoneSelect = document.getElementById('timezoneSelect');
    const settingsPopup = document.getElementById('settingsPopup');
    const settingsBtn = document.getElementById('btnSettings');
    
    if (timezoneSelect) {
        timezoneSelect.value = TimezoneConfig.getTimezone();
        timezoneSelect.addEventListener('change', (e) => {
            TimezoneConfig.setTimezone(e.target.value);
            settingsPopup.classList.remove('show');
            // Redraw chart and indicators
            if (window.dashboard) {
                window.drawIndicatorsOnChart?.();
            }
        });
    }
    
    // Toggle settings popup
    if (settingsBtn && settingsPopup) {
        settingsBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            settingsPopup.classList.toggle('show');
        });
        
        settingsPopup.addEventListener('click', (e) => {
            e.stopPropagation();
        });
        
        document.addEventListener('click', () => {
            settingsPopup.classList.remove('show');
        });
    }

    window.dashboard = new TradingDashboard();
    restoreSidebarState();
    restoreSidebarTabState();
    initSidebarTabs();

    // Initialize panels
    window.initIndicatorPanel();
    initStrategyPanel();
});
