import React from 'react';
import {
  Modal,
  View,
  Text,
  TouchableOpacity,
  StyleSheet,
  Pressable,
  Image,
} from 'react-native';
import { useTranslation } from 'react-i18next';
import { LANGUAGES, saveLanguage } from '../i18n';
import pxToDp from '../helper/pxToDp';
import { THEME } from '../theme';

interface LanguageModalProps {
  visible: boolean;
  onClose: () => void;
}

const LanguageModal: React.FC<LanguageModalProps> = ({ visible, onClose }) => {
  const { t, i18n } = useTranslation();
  const currentLanguage = i18n.language;

  const handleLanguageSelect = async (languageKey: string) => {
    await saveLanguage(languageKey);
    i18n.changeLanguage(languageKey);
    onClose();
  };

  return (
    <Modal
      visible={visible}
      transparent
      animationType="fade"
      onRequestClose={onClose}
      statusBarTranslucent
    >
      <Pressable style={styles.overlay} onPress={onClose}>
        <Pressable 
          style={styles.modalContainer} 
          onPress={(e) => e.stopPropagation()}
        >
          {/* 标题栏 */}
          <View style={styles.header}>
            <Text style={styles.title}>{t('languageModal.title')}</Text>
            <TouchableOpacity onPress={onClose} style={styles.closeButton}>
              <Text style={styles.closeIcon}>✕</Text>
            </TouchableOpacity>
          </View>

          {/* 语言列表 */}
          <View style={styles.languageList}>
            {LANGUAGES.map((language) => (
              <TouchableOpacity
                key={language.key}
                style={styles.languageItem}
                onPress={() => handleLanguageSelect(language.key)}
                activeOpacity={0.7}
              >
                <Text style={styles.languageText}>{language.label}</Text>
                {currentLanguage === language.key && (
                  <Text style={styles.checkmark}>✓</Text>
                )}
              </TouchableOpacity>
            ))}
          </View>
        </Pressable>
      </Pressable>
    </Modal>
  );
};

const styles = StyleSheet.create({
  overlay: {
    flex: 1,
    backgroundColor: THEME.overlay,
    justifyContent: 'center',
    alignItems: 'center',
  },
  modalContainer: {
    width: pxToDp(630),
    backgroundColor: THEME.surface,
    borderRadius: pxToDp(24),
    overflow: 'hidden',
    borderWidth: 1,
    borderColor: THEME.border,
  },
  header: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    paddingVertical: pxToDp(32),
    borderBottomWidth: pxToDp(1),
    borderBottomColor: THEME.border,
    position: 'relative',
  },
  title: {
    fontSize: pxToDp(32),
    fontWeight: '600',
    color: THEME.text,
  },
  closeButton: {
    position: 'absolute',
    right: pxToDp(32),
    padding: pxToDp(8),
  },
  closeIcon: {
    fontSize: pxToDp(40),
    color: THEME.textMuted,
    fontWeight: '300',
  },
  languageList: {
    paddingVertical: pxToDp(16),
  },
  languageItem: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    paddingHorizontal: pxToDp(40),
    paddingVertical: pxToDp(32),
    backgroundColor: THEME.surface,
  },
  languageText: {
    fontSize: pxToDp(30),
    color: THEME.text,
  },
  checkmark: {
    fontSize: pxToDp(40),
    color: THEME.text,
    fontWeight: 'bold',
  },
});

export default LanguageModal;
