import React, { useState, useEffect, useCallback, useRef } from 'react';
import { useCurrentInstance } from '../../../hooks/useCurrentInstance';
import * as teamsbotApi from '../../../api/teamsbotApi';
import type { TeamsbotConfig, ConfigUpdateRequest, VoiceOption } from '../../../api/teamsbotApi';
import { FaPlay, FaSpinner } from 'react-icons/fa';
import styles from './Teamsbot.module.css';

/** Format voice name for display: "de-DE-Wavenet-A" -> "Wavenet A" + gender */
function _formatVoiceName(voice: VoiceOption): string {
  const parts = voice.name.split('-');
  const type = parts[2] || '';   // Wavenet, Neural2, Standard, etc.
  const variant = parts[3] || '';
  const gender = voice.ssmlGender === 'FEMALE' ? 'Frau' : voice.ssmlGender === 'MALE' ? 'Mann' : '';
  return `${gender} ${variant} (${type})`.trim();
}

/**
 * TeamsbotSettingsView - Bot configuration for a feature instance.
 */
export const TeamsbotSettingsView: React.FC = () => {
  const { instance } = useCurrentInstance();
  const instanceId = instance?.id || '';

  const [_config, setConfig] = useState<TeamsbotConfig | null>(null);
  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [testingVoice, setTestingVoice] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [successMsg, setSuccessMsg] = useState<string | null>(null);
  const audioRef = useRef<HTMLAudioElement | null>(null);

  // Form state
  const [formData, setFormData] = useState<ConfigUpdateRequest>({});

  // Dynamic voice data from Google TTS API
  const [languages, setLanguages] = useState<string[]>([]);
  const [voices, setVoices] = useState<VoiceOption[]>([]);
  const [loadingVoices, setLoadingVoices] = useState(false);


  const _loadConfig = useCallback(async () => {
    if (!instanceId) return;
    try {
      setLoading(true);
      // Load per-user settings (merged with instance defaults)
      const [settingsResult, languagesResult] = await Promise.all([
        teamsbotApi.getUserSettings(instanceId),
        teamsbotApi.fetchLanguages(),
      ]);
      const effectiveConfig = settingsResult.effectiveConfig;
      setConfig(effectiveConfig);
      setFormData(effectiveConfig);
      setLanguages(languagesResult);
      // Load voices for the current language
      const lang = effectiveConfig.language || 'de-DE';
      const voicesResult = await teamsbotApi.fetchVoices(lang);
      setVoices(voicesResult);
      setError(null);
    } catch (err: any) {
      setError(err.message || 'Fehler beim Laden der Konfiguration');
    } finally {
      setLoading(false);
    }
  }, [instanceId]);

  useEffect(() => {
    _loadConfig();
  }, [_loadConfig]);

  const _handleSave = async () => {
    if (!instanceId) return;
    setSaving(true);
    setError(null);
    setSuccessMsg(null);

    try {
      // Save as per-user settings (not instance config)
      const result = await teamsbotApi.updateUserSettings(instanceId, formData);
      setConfig(result.effectiveConfig);
      setFormData(result.effectiveConfig);
      setSuccessMsg('Einstellungen gespeichert');
      setTimeout(() => setSuccessMsg(null), 3000);
    } catch (err: any) {
      setError(err.message || 'Fehler beim Speichern');
    } finally {
      setSaving(false);
    }
  };

  const _updateField = (field: keyof ConfigUpdateRequest, value: any) => {
    setFormData(prev => ({ ...prev, [field]: value }));
  };

  const _handleLanguageChange = async (language: string) => {
    _updateField('language', language);
    // Load voices for the new language dynamically
    setLoadingVoices(true);
    try {
      const voicesResult = await teamsbotApi.fetchVoices(language);
      setVoices(voicesResult);
      // Auto-select first voice if current voice doesn't match
      if (voicesResult.length > 0 && !voicesResult.find(v => v.name === formData.voiceId)) {
        _updateField('voiceId', voicesResult[0].name);
      }
    } catch {
      setVoices([]);
    } finally {
      setLoadingVoices(false);
    }
  };

  const _handleTestVoice = async () => {
    if (!instanceId) return;
    setTestingVoice(true);
    try {
      const language = formData.language || 'de-DE';
      const botName = formData.botName || 'AI Assistant';
      const result = await teamsbotApi.testVoice(instanceId, botName, language, formData.voiceId);
      
      if (result.success && result.audio) {
        // Play audio from base64
        const audioBlob = new Blob(
          [Uint8Array.from(atob(result.audio), c => c.charCodeAt(0))],
          { type: 'audio/mp3' }
        );
        const audioUrl = URL.createObjectURL(audioBlob);
        if (audioRef.current) {
          audioRef.current.pause();
        }
        const audio = new Audio(audioUrl);
        audioRef.current = audio;
        audio.play();
        audio.onended = () => URL.revokeObjectURL(audioUrl);
      } else {
        setError(result.error || 'Stimmtest fehlgeschlagen');
        setTimeout(() => setError(null), 3000);
      }
    } catch (err: any) {
      setError(err.message || 'Stimmtest fehlgeschlagen');
      setTimeout(() => setError(null), 3000);
    } finally {
      setTestingVoice(false);
    }
  };

  if (loading) return <div className={styles.loading}>Lade Konfiguration...</div>;

  return (
    <div className={styles.settingsContainer}>
      <div className={styles.settingsCard}>
        <h3 className={styles.cardTitle}>Bot-Einstellungen</h3>

        {error && <div className={styles.errorBanner}>{error}</div>}
        {successMsg && <div className={styles.successBanner}>{successMsg}</div>}

        {/* Bot Identity */}
        <div className={styles.settingsSection}>
          <h4 className={styles.sectionTitle}>Bot-Identitaet</h4>

          <div className={styles.formGroup}>
            <label className={styles.label}>Bot-Name</label>
            <input
              type="text"
              className={styles.input}
              value={formData.botName || ''}
              onChange={(e) => _updateField('botName', e.target.value)}
              placeholder="AI Assistant"
            />
            <span className={styles.hint}>
              Default-Name fuer den Bot im Meeting. Falls keiner angegeben, wird der Name des System-Bots verwendet (z.B. "Nyla Larsson").
            </span>
          </div>
        </div>

        {/* AI Behavior */}
        <div className={styles.settingsSection}>
          <h4 className={styles.sectionTitle}>AI-Verhalten</h4>

          <div className={styles.formGroup}>
            <label className={styles.label}>System-Prompt</label>
            <textarea
              className={styles.textarea}
              value={formData.aiSystemPrompt || ''}
              onChange={(e) => _updateField('aiSystemPrompt', e.target.value)}
              rows={6}
              placeholder="Beschreibe, wie sich der Bot im Meeting verhalten soll..."
            />
            <span className={styles.hint}>Instruktionen fuer den AI-Bot: Wann und wie soll er antworten?</span>
          </div>

          <div className={styles.formGroup}>
            <label className={styles.label}>Antwort-Modus</label>
            <select
              className={styles.select}
              value={formData.responseMode || 'auto'}
              onChange={(e) => _updateField('responseMode', e.target.value)}
            >
              <option value="auto">Automatisch - AI entscheidet selbst</option>
              <option value="manual">Manuell - Antworten muessen bestaetigt werden</option>
              <option value="transcribeOnly">Nur Transkription - Keine AI-Antworten</option>
            </select>
          </div>

          <div className={styles.formGroup}>
            <label className={styles.label}>Antwort-Kanal</label>
            <select
              className={styles.select}
              value={formData.responseChannel || 'voice'}
              onChange={(e) => _updateField('responseChannel', e.target.value)}
            >
              <option value="voice">Nur Sprache - Bot antwortet per Audio</option>
              <option value="chat">Nur Chat - Bot antwortet per Textnachricht</option>
              <option value="both">Sprache + Chat - Bot antwortet per Audio und Text</option>
            </select>
            <span className={styles.hint}>Wie soll der Bot im Meeting antworten?</span>
          </div>

          <div className={styles.formGroup}>
            <label className={styles.label}>Transfer-Modus</label>
            <select
              className={styles.select}
              value={formData.transferMode || 'auto'}
              onChange={(e) => _updateField('transferMode', e.target.value)}
            >
              <option value="auto">Automatisch - System waehlt basierend auf Join-Modus</option>
              <option value="caption">Captions - Teams Live-Captions (nur Englisch verfuegbar)</option>
              <option value="audio">Audio-Stream - Echtzeit-Spracherkennung ueber Gateway (alle Sprachen)</option>
            </select>
            <span className={styles.hint}>
              Empfehlung: Audio-Stream fuer nicht-englische Meetings verwenden. Teams Live-Captions sind nur auf Englisch verfuegbar und oft ungenau.
            </span>
          </div>
        </div>

        {/* Voice Settings */}
        <div className={styles.settingsSection}>
          <h4 className={styles.sectionTitle}>Sprach-Einstellungen</h4>

          <div className={styles.formGroup}>
            <label className={styles.label}>Sprache</label>
            <select
              className={styles.select}
              value={formData.language || 'de-DE'}
              onChange={(e) => _handleLanguageChange(e.target.value)}
            >
              {languages.length > 0 ? (
                languages.map((langCode, idx) => (
                  <option key={`${langCode}-${idx}`} value={langCode}>{langCode}</option>
                ))
              ) : (
                <>
                  <option value="de-DE">Deutsch (Deutschland)</option>
                  <option value="en-US">English (US)</option>
                  <option value="fr-FR">Francais</option>
                </>
              )}
            </select>
            <span className={styles.hint}>Sprache fuer Captions und Sprachausgabe ({languages.length} Sprachen verfuegbar)</span>
          </div>

          <div className={styles.formGroup}>
            <label className={styles.label}>Stimme</label>
            <div style={{ display: 'flex', gap: '8px', alignItems: 'center' }}>
              <select
                className={styles.select}
                style={{ flex: 1 }}
                value={formData.voiceId || ''}
                onChange={(e) => _updateField('voiceId', e.target.value)}
                disabled={loadingVoices}
              >
                <option value="">Standard-Stimme</option>
                {voices.map(voice => (
                  <option key={voice.name} value={voice.name}>
                    {_formatVoiceName(voice)} - {voice.name}
                  </option>
                ))}
              </select>
              <button
                className={styles.testButton || styles.saveButton}
                onClick={_handleTestVoice}
                disabled={testingVoice || loadingVoices}
                title="Stimme testen"
                style={{ minWidth: '44px', padding: '8px 12px', display: 'flex', alignItems: 'center', gap: '6px' }}
              >
                {testingVoice ? <FaSpinner className={styles.spinner} /> : <FaPlay />}
                Test
              </button>
            </div>
            <span className={styles.hint}>
              {loadingVoices ? 'Lade Stimmen...' : `${voices.length} Stimmen verfuegbar - klicke Test fuer Vorschau`}
            </span>
          </div>
        </div>

        {/* Advanced Settings */}
        <div className={styles.settingsSection}>
          <h4 className={styles.sectionTitle}>Erweiterte Einstellungen</h4>

          <div className={styles.formRow}>
            <div className={styles.formGroup}>
              <label className={styles.label}>Analyse-Intervall (Sek.)</label>
              <input
                type="number"
                className={styles.input}
                value={formData.triggerIntervalSeconds || 10}
                onChange={(e) => _updateField('triggerIntervalSeconds', parseInt(e.target.value))}
                min={3}
                max={60}
              />
              <span className={styles.hint}>Periodisches Analyse-Intervall</span>
            </div>

            <div className={styles.formGroup}>
              <label className={styles.label}>Cooldown (Sek.)</label>
              <input
                type="number"
                className={styles.input}
                value={formData.triggerCooldownSeconds || 3}
                onChange={(e) => _updateField('triggerCooldownSeconds', parseInt(e.target.value))}
                min={1}
                max={30}
              />
              <span className={styles.hint}>Min. Abstand zwischen AI-Calls</span>
            </div>

            <div className={styles.formGroup}>
              <label className={styles.label}>Kontext-Fenster (Segmente)</label>
              <input
                type="number"
                className={styles.input}
                value={formData.contextWindowSegments || 20}
                onChange={(e) => _updateField('contextWindowSegments', parseInt(e.target.value))}
                min={5}
                max={100}
              />
              <span className={styles.hint}>Anzahl Transkript-Segmente fuer AI-Kontext</span>
            </div>
          </div>

          <div className={styles.formGroup}>
            <label className={styles.label}>Browser Bot URL (optional)</label>
            <input
              type="url"
              className={styles.input}
              value={formData.browserBotUrl || ''}
              onChange={(e) => _updateField('browserBotUrl', e.target.value)}
              placeholder="Automatisch aus Umgebungskonfiguration"
            />
            <span className={styles.hint}>URL des Browser Bot Service. Leer lassen fuer Standard-Konfiguration.</span>
          </div>

          <div className={styles.formGroup}>
            <label style={{ display: 'flex', alignItems: 'center', gap: '8px', cursor: 'pointer' }}>
              <input
                type="checkbox"
                checked={formData.debugMode || false}
                onChange={(e) => _updateField('debugMode', e.target.checked)}
                style={{ width: '18px', height: '18px' }}
              />
              <span className={styles.label} style={{ margin: 0 }}>Debug-Modus</span>
            </label>
            <span className={styles.hint}>
              Aktiviert Screenshot-Erfassung bei jedem Join-Schritt fuer Diagnose. Screenshots koennen in der Session-Ansicht eingesehen werden.
            </span>
          </div>
        </div>

        {/* Save Button */}
        <div className={styles.settingsActions}>
          <button
            className={styles.saveButton}
            onClick={_handleSave}
            disabled={saving}
          >
            {saving ? 'Speichern...' : 'Konfiguration speichern'}
          </button>
        </div>
      </div>
    </div>
  );
};

export default TeamsbotSettingsView;
