import { getJson } from './api.js';

export function createAiPanelController(deps) {
  function open() {
    var panel = document.getElementById('lh-ai-panel');
    if (!panel) return;
    panel.classList.remove('hidden');
    populateModelSelect();

    var editorType = deps.getEditorType() || 'article';
    var ctype = document.getElementById('lh-ai-ctype');
    if (ctype) ctype.value = editorType;
    updateOptions(editorType);

    getJson('/api/auth/me')
      .then(function(d) {
        var tab = document.getElementById('lh-ai-tab-webdav');
        if (tab) tab.style.display = (d.user && d.user.nextcloud_url) ? '' : 'none';
        if (d.user && d.user.webdav_learning_path) deps.setWebdavPath(d.user.webdav_learning_path || '/');
      });

    wireOptionControls();
    panel.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
  }

  function close() {
    var panel = document.getElementById('lh-ai-panel');
    if (panel) panel.classList.add('hidden');
  }

  function populateModelSelect() {
    var sel = document.getElementById('lh-ai-model');
    if (!sel || sel.options.length > 1) return;
    if (typeof window._buildModelOptions === 'function') window._buildModelOptions(sel);
  }

  function switchTab(tabName) {
    document.querySelectorAll('.lh-ai-tab').forEach(function(t) {
      t.classList.toggle('active', t.dataset.aitab === tabName);
    });
    document.querySelectorAll('.lh-ai-tabpanel').forEach(function(p) { p.classList.add('hidden'); });
    var tp = document.getElementById('lh-ai-tp-' + tabName);
    if (tp) tp.classList.remove('hidden');
    if (tabName === 'webdav' && !document.querySelector('#lh-ai-webdav-list .lh-webdav-item')) {
      deps.browseWebdav(deps.getWebdavPath());
    }
  }

  function updateOptions(type) {
    applyAiOptionState(type, {
      wordsField: document.getElementById('lh-ai-words-field'),
      slidesField: document.getElementById('lh-ai-slides-field'),
      quizRow: document.getElementById('lh-ai-quiz-row'),
      cardTitle: document.getElementById('lh-ai-quiz-card-title'),
      toggleLabel: document.getElementById('lh-ai-q-toggle-label'),
      toggleCb: document.getElementById('lh-ai-q-toggle'),
      countWrap: document.getElementById('lh-ai-q-count-wrap')
    });
  }

  function wireOptionControls() {
    var ctypeEl = document.getElementById('lh-ai-ctype');
    var toggleEl = document.getElementById('lh-ai-q-toggle');
    if (ctypeEl && !ctypeEl._wired) {
      ctypeEl.addEventListener('change', function() { updateOptions(ctypeEl.value); });
      ctypeEl._wired = true;
    }
    if (toggleEl && !toggleEl._wired) {
      toggleEl.addEventListener('change', function() {
        updateOptions((document.getElementById('lh-ai-ctype') || {}).value);
      });
      toggleEl._wired = true;
    }
  }

  return {
    close: close,
    open: open,
    switchTab: switchTab,
    updateOptions: updateOptions
  };
}

export function applyAiOptionState(type, els) {
  if (els.wordsField) els.wordsField.style.display = (type === 'quiz' || type === 'presentation') ? 'none' : 'flex';
  if (els.slidesField) els.slidesField.style.display = type === 'presentation' ? 'flex' : 'none';

  if (type === 'quiz') {
    if (els.quizRow) els.quizRow.style.display = 'block';
    if (els.cardTitle) els.cardTitle.textContent = 'Quiz Questions';
    if (els.toggleLabel) els.toggleLabel.style.display = 'none';
    if (els.toggleCb) { els.toggleCb.checked = true; els.toggleCb.disabled = true; }
    if (els.countWrap) els.countWrap.style.display = 'flex';
    return;
  }

  if (els.quizRow) els.quizRow.style.display = 'block';
  if (els.cardTitle) els.cardTitle.textContent = 'Quiz Questions (optional)';
  if (els.toggleLabel) els.toggleLabel.style.display = 'flex';
  if (els.toggleCb) els.toggleCb.disabled = false;
  var checked = els.toggleCb ? els.toggleCb.checked : false;
  if (els.countWrap) els.countWrap.style.display = checked ? 'flex' : 'none';
}
