/**
 * EmptyState 空状态组件 - 美化版
 * 显示空数据时的占位界面，采用现代插图风格设计
 */

import React, { useMemo } from 'react';
import { View, StyleSheet, ViewStyle } from 'react-native';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { spacing, fontSizes, borderRadius, useAppColors, type AppColors } from '../../theme';
import Text from './Text';
import Button from './Button';

interface EmptyStateProps {
  icon?: string;
  title: string;
  description?: string;
  actionLabel?: string;
  onAction?: () => void;
  style?: ViewStyle;
  variant?: 'default' | 'modern' | 'compact';
}

function createEmptyStateStyles(colors: AppColors) {
  return StyleSheet.create({
    container: {
      flex: 1,
      alignItems: 'center',
      justifyContent: 'center',
      padding: spacing.xl,
    },
    icon: {
      marginBottom: spacing.lg,
    },
    title: {
      textAlign: 'center',
      marginBottom: spacing.sm,
    },
    description: {
      textAlign: 'center',
      marginBottom: spacing.lg,
    },
    button: {
      minWidth: 120,
    },
    modernContainer: {
      flex: 1,
      alignItems: 'center',
      justifyContent: 'center',
      padding: spacing.xl,
      minHeight: 280,
    },
    illustrationContainer: {
      position: 'relative',
      alignItems: 'center',
      justifyContent: 'center',
      marginBottom: spacing.xl,
      width: 120,
      height: 120,
    },
    iconBackground: {
      width: 100,
      height: 100,
      borderRadius: borderRadius['2xl'],
      backgroundColor: colors.primary.main + '15',
      alignItems: 'center',
      justifyContent: 'center',
      shadowColor: colors.primary.main,
      shadowOffset: { width: 0, height: 4 },
      shadowOpacity: 0.2,
      shadowRadius: 12,
      elevation: 4,
    },
    modernIcon: {
      opacity: 0.9,
    },
    decorativeCircle1: {
      position: 'absolute',
      width: 24,
      height: 24,
      borderRadius: borderRadius.full,
      backgroundColor: colors.primary.light + '30',
      top: 5,
      right: 10,
    },
    decorativeCircle2: {
      position: 'absolute',
      width: 16,
      height: 16,
      borderRadius: borderRadius.full,
      backgroundColor: colors.primary.main + '20',
      bottom: 15,
      left: 5,
    },
    modernTitle: {
      textAlign: 'center',
      marginBottom: spacing.sm,
      fontWeight: '700',
      fontSize: fontSizes.xl,
    },
    modernDescription: {
      textAlign: 'center',
      marginBottom: spacing.lg,
      fontSize: fontSizes.md,
      lineHeight: 22,
      maxWidth: 280,
    },
    modernButton: {
      minWidth: 140,
      borderRadius: borderRadius.lg,
    },
    compactContainer: {
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'center',
      padding: spacing.lg,
    },
    compactIcon: {
      marginRight: spacing.sm,
    },
    compactTitle: {
      fontSize: fontSizes.md,
    },
  });
}

const EmptyState: React.FC<EmptyStateProps> = ({
  icon = 'folder-open-outline',
  title,
  description,
  actionLabel,
  onAction,
  style,
  variant = 'modern',
}) => {
  const colors = useAppColors();
  const styles = useMemo(() => createEmptyStateStyles(colors), [colors]);

  if (variant === 'modern') {
    return (
      <View style={[styles.modernContainer, style]}>
        <View style={styles.illustrationContainer}>
          <View style={styles.iconBackground}>
            <MaterialCommunityIcons
              name={icon as any}
              size={48}
              color={colors.primary.main}
              style={styles.modernIcon}
            />
          </View>
          <View style={styles.decorativeCircle1} />
          <View style={styles.decorativeCircle2} />
        </View>
        <Text variant="h3" color={colors.text.primary} style={styles.modernTitle}>
          {title}
        </Text>
        {description && (
          <Text variant="body" color={colors.text.secondary} style={styles.modernDescription}>
            {description}
          </Text>
        )}
        {actionLabel && onAction && (
          <Button
            title={actionLabel}
            onPress={onAction}
            variant="primary"
            size="md"
            style={styles.modernButton}
          />
        )}
      </View>
    );
  }

  if (variant === 'compact') {
    return (
      <View style={[styles.compactContainer, style]}>
        <MaterialCommunityIcons
          name={icon as any}
          size={40}
          color={colors.text.disabled}
          style={styles.compactIcon}
        />
        <Text variant="body" color={colors.text.secondary} style={styles.compactTitle}>
          {title}
        </Text>
      </View>
    );
  }

  return (
    <View style={[styles.container, style]}>
      <MaterialCommunityIcons
        name={icon as any}
        size={64}
        color={colors.text.disabled}
        style={styles.icon}
      />
      <Text variant="h3" color={colors.text.secondary} style={styles.title}>
        {title}
      </Text>
      {description && (
        <Text variant="body" color={colors.text.secondary} style={styles.description}>
          {description}
        </Text>
      )}
      {actionLabel && onAction && (
        <Button
          title={actionLabel}
          onPress={onAction}
          variant="outline"
          size="md"
          style={styles.button}
        />
      )}
    </View>
  );
};

export default EmptyState;
