import React, { useState, useEffect, useCallback, useRef } from 'react';
import { useCurrentInstance } from '../../../hooks/useCurrentInstance';
import * as teamsbotApi from '../../../api/teamsbotApi';
import type { TeamsbotConfig, ConfigUpdateRequest, VoiceLanguage, 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<VoiceLanguage[]>([]);
  const [voices, setVoices] = useState<VoiceOption[]>([]);
  const [loadingVoices, setLoadingVoices] = useState(false);

  const _loadConfig = useCallback(async () => {
    if (!instanceId) return;
    try {
      setLoading(true);
      const [configResult, languagesResult] = await Promise.all([
        teamsbotApi.getConfig(instanceId),
        teamsbotApi.fetchLanguages(),
      ]);
      setConfig(configResult.config);
      setFormData(configResult.config);
      setLanguages(languagesResult);
      // Load voices for the current language
      const lang = configResult.config.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 {
      const result = await teamsbotApi.updateConfig(instanceId, formData);
      setConfig(result.config);
      setFormData(result.config);
      setSuccessMsg('Konfiguration 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 testText = `Hallo, ich bin ${botName}. So klinge ich in diesem Meeting.`;
      const result = await teamsbotApi.testVoice(instanceId, testText, 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}>Wird als Teilnehmer-Name im Meeting angezeigt</span>
          </div>

          <div className={styles.formGroup}>
            <label className={styles.label}>Hintergrundbild-URL</label>
            <input
              type="url"
              className={styles.input}
              value={formData.backgroundImageUrl || ''}
              onChange={(e) => _updateField('backgroundImageUrl', e.target.value)}
              placeholder="https://example.com/background.jpg"
            />
            <span className={styles.hint}>Hintergrundbild fuer den Video-Feed des Bots</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>

        {/* 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((lang, idx) => (
                  <option key={`${lang.code}-${idx}`} value={lang.code}>{lang.name} ({lang.code})</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>

        {/* 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;
