/**
 * Main Gallery Application
 * Orchestrates all the different managers and functionality
 */
import { ThemeManager } from './theme-manager.js';
import { NavigationManager } from './navigation-manager.js';
import { SearchManager } from './search-manager.js';
import { RecentPlotsManager } from './recent-plots-manager.js';
import { ComparisonManager } from './comparison-manager.js';
import { StatsManager } from './stats-manager.js';
import { KeyboardManager } from './keyboard-manager.js';
import { ViewManager } from './view-manager.js';
import { SortManager } from './sort-manager.js';
import { Utils } from './utils.js';

/**
 * Main Gallery Application Class
 */
export class GalleryApp {
  constructor(config = {}) {
    // Configuration from backend template variables
    this.SEARCH_DEBOUNCE_MS = config.searchDebounceMs || 300;
    this.MAX_RECENT_PLOTS = config.maxRecentPlots || 20;
    this.stats = config.stats || null;
    
    // Initialize managers
    this.themeManager = new ThemeManager();
    this.navigationManager = new NavigationManager();
    this.searchManager = new SearchManager(this.SEARCH_DEBOUNCE_MS);
    this.recentPlotsManager = new RecentPlotsManager(this.MAX_RECENT_PLOTS);
    this.comparisonManager = new ComparisonManager();
    this.statsManager = new StatsManager();
    this.viewManager = new ViewManager();
    this.sortManager = new SortManager();
    this.keyboardManager = new KeyboardManager(this);
    this.utils = Utils;
    
    // Set global references for backward compatibility
    window.themeManager = this.themeManager;
    window.searchManager = this.searchManager;
    window.recentPlotsManager = this.recentPlotsManager;
    window.comparisonManager = this.comparisonManager;
    window.viewManager = this.viewManager;
    window.sortManager = this.sortManager;
    window.utils = this.utils;
    
    this.init();
  }

  /**
   * Initialize the gallery application
   */
  init() {
    this.navigationManager.buildBreadcrumb();
    this.navigationManager.buildFolderTree();
    
    // Update stats with backend data if available
    if (this.stats) {
      this.statsManager.updateWithBackendStats(this.stats);
    }
    
    // Handle URL-based thumbnail highlighting
    Utils.handleThumbnailHighlight();
  }

  // Backward compatibility methods
  toggleTheme() { this.themeManager.toggle(); }
  toggleSidebar() { this.recentPlotsManager.toggleSidebar(); }
  toggleCompareMode() { this.comparisonManager.toggleCompareMode(); }
  closeComparison() { this.comparisonManager.closeComparison(); }
  selectPlotForComparison(slot) { this.comparisonManager.selectPlotForComparison(slot); }
  replacePlot(slot) { this.comparisonManager.replacePlot(slot); }
}
