'use strict';

// ════════════════════════════════════════════════════════════════════════════
// confirmSend.js — Sendebestätigungs-Dialog
// Nutzt shared.js für alle gemeinsamen Funktionen.
// Profile und Resize-Algorithmen werden aus der dialog-init-Antwort geladen.
// ════════════════════════════════════════════════════════════════════════════

const params = new URLSearchParams(location.search);
const tabId = parseInt(params.get('tabId'), 10);

PPP_SHARED.tabId = tabId;
PPP_SHARED.images = [];
PPP_SHARED.profiles = [];
PPP_SHARED.defaults = {};
PPP_SHARED.cardElements = new Map();
PPP_SHARED.profileSettings = {};

async function init() {
  PPP_SHARED.showLoading('Bilder werden geladen...');

  try {
    const result = await PPP_SHARED.sendMessage({ type: 'dialog-init', tabId });

    if (!result || !result.images || result.images.length === 0) {
      await PPP_SHARED.sendMessage({ type: 'dialog-done', tabId, action: 'send' });
      return;
    }

    PPP_SHARED.images = result.images;
    PPP_SHARED.profiles = result.profiles || [];
    PPP_SHARED.defaults = result.defaults || {};
    PPP_SHARED.profileSettings = result.profileSettings || {};

    PPP_SHARED.resizeAlgorithms = result.resizeAlgorithms || [];
    PPP_SHARED.populateResizeAlgorithms(
      document.getElementById('resizeAlgorithm'),
      result.resizeAlgorithm
    );

    // ── Profil-Dropdowns befüllen (aus Popup-TabState übernommen) ──
    PPP_SHARED.populateProfileDropdowns(PPP_SHARED.profiles, PPP_SHARED.defaults);

    const list = PPP_SHARED.$('image-list');
    list.innerHTML = '';

    for (const img of PPP_SHARED.images) {
      const card = PPP_SHARED.renderCard(img);
      list.appendChild(card);
      PPP_SHARED.cardElements.set(img.id, card);
    }

    // ── Ausnahme einblenden: Button zeigen, wenn excluded-Bilder existieren ──
    if (result && result.excludedCount > 0) {
      PPP_SHARED.setupExcludedButton(result.excludedCount, document.getElementById('settings-bar'));
    }

    PPP_SHARED.updateSummary();
  } catch (err) {
    PPP_SHARED.$('empty-state').textContent = 'Fehler: ' + err.message;
    PPP_SHARED.$('empty-state').style.display = 'block';
  } finally {
    PPP_SHARED.hideLoading();
  }
}

document.getElementById('resizeAlgorithm').addEventListener('change', () => {
  PPP_SHARED.sendMessage({
    type: 'set-tab-state',
    tabId,
    state: { resizeAlgorithm: document.getElementById('resizeAlgorithm').value },
  });
});

// ── Globale Profil-Dropdowns: Änderung speichern + Karten synchronisieren ──
['attachProfile', 'inlineProfile', 'quoteProfile'].forEach((id) => {
  const el = document.getElementById(id);
  el.addEventListener('change', () => {
    PPP_SHARED.sendMessage({
      type: 'set-tab-state',
      tabId,
      state: { [id]: el.value },
    });
    const category = id.replace('Profile', '');
    PPP_SHARED.syncCardProfiles(category, el.value);
  });
});

PPP_SHARED.$('btn-cancel').addEventListener('click', async () => {
  await PPP_SHARED.sendMessage({ type: 'dialog-done', tabId, action: 'cancel' });
});

PPP_SHARED.$('btn-send').addEventListener('click', async () => {
  PPP_SHARED.showLoading('Senden wird vorbereitet...');
  await PPP_SHARED.sendMessage({ type: 'dialog-done', tabId, action: 'send' });
});

PPP_SHARED.$('btn-apply-all').addEventListener('click', async () => {
  await PPP_SHARED.compressAll();
  PPP_SHARED.showLoading('Senden wird vorbereitet...');
  await PPP_SHARED.sendMessage({ type: 'dialog-done', tabId, action: 'send' });
});

init();
