/**
 * Weather.IM Live - Main Entry Point
 */

// ExtJS 6 loaded via script tag in HTML (UMD module, not ES module compatible)

// Import OpenLayers 8
import 'ol/ol.css';

// Import configuration
import './config.js';
import { Application } from './app-state.js';

// Import Strophe.js from npm package and set up plugins
import { Strophe } from 'strophe.js';
import './lib/strophe-disco-plugin.js';


// Import chat components
import './chat/ChatComponents.js';

import './map/MapPanel.js';

// Import XMPP handlers
import './xmpp/handlers.js';

// Import dialog windows
import './dialogs/Dialogs.js';

// Import UI components
import './ui/core-components.js';
import './ui/additional-windows.js';

// Import utilities
import './utils/grid-utilities.js';

// Import core application control
import './core/app-control.js';

// Import event handlers
import './events/event-handlers.js';

console.log('Weather.IM Live ES module loaded');

const iconBaseUrl = `${import.meta.env.BASE_URL}icons/`;
const rootStyle = document.documentElement.style;
rootStyle.setProperty('--icon-available', `url('${iconBaseUrl}available.png')`);
rootStyle.setProperty('--icon-person', `url('${iconBaseUrl}person.png')`);
rootStyle.setProperty('--icon-away', `url('${iconBaseUrl}away.png')`);
rootStyle.setProperty('--icon-chat', `url('${iconBaseUrl}chat.png')`);
rootStyle.setProperty('--icon-owner', `url('${iconBaseUrl}owner.png')`);
rootStyle.setProperty('--icon-admin', `url('${iconBaseUrl}admin.png')`);
rootStyle.setProperty('--icon-participant', `url('${iconBaseUrl}participant.png')`);
rootStyle.setProperty('--icon-new-tab', `url('${iconBaseUrl}new_tab.gif')`);
rootStyle.setProperty('--icon-typing', `url('${iconBaseUrl}typing.png')`);

// Wait for all global dependencies to be available before initializing
function initializeApp() {
  if (typeof Ext === 'undefined') {
    console.warn('ExtJS not loaded yet, retrying...');
    setTimeout(initializeApp, 50);
    return;
  }

  // OpenLayers 8 is loaded via ES modules, no window check needed
  // GeoExt has been removed - we're using pure OpenLayers 8 + ExtJS 6

  console.log('All dependencies loaded, initializing app...');

  // Configure Strophe logging
  Strophe.log = function(level, msg) {
    if (Application.log) {
      Application.log(msg);
    }
  };

  // Initialize audio mute state
  Application.audioMuted = false;

  // ExtJS configuration
  Ext.BLANK_IMAGE_URL = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

  Ext.onReady(function() {
    console.log('Ext.onReady fired');

    try {
      // Cleanup on page close
      addEventListener('beforeunload', () => {
        if (typeof Application.XMPPConn !== 'undefined') {
          Application.XMPPConn.flush();
          Application.XMPPConn.disconnect();
        }
      });

      Ext.QuickTips.init();

      console.log('Creating LiveViewport...');
      const urlParams = new URLSearchParams(window.location.search);
      (new Application.LiveViewport({
        renderTo: Ext.getBody(),
        enableMap: !urlParams.has('nomap')
      })).show();
      console.log('LiveViewport created and shown');

      // Start ServiceGuard task
      if (Application.ServiceGuard) {
        Ext.TaskManager.start(Application.ServiceGuard);
      }
      // MapTask is started in doStuff() after map is ready

      console.log('Application initialization complete');
    } catch (e) {
      console.error('Error during initialization:', e);
    }
  });
}

// Start initialization when module loads
initializeApp();
