import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import { useTheme } from '@/shared/styles/useTheme';
import { StackedBarChart } from '@/shared/components/charts';

interface BarChartWidgetProps {
  data: Array<{
    label: string;
    value: number;
    color: string;
  }>;
  title: string;
  subtitle?: string;
  onPress?: () => void;
  height?: number;
}

const BarChartWidget: React.FC<BarChartWidgetProps> = ({
  data,
  title,
  subtitle,
  onPress,
  height = 120,
}) => {
  const { colors, fonts, spacing, shadows } = useTheme();

  const maxValue = Math.max(...data.map(item => item.value));
  const total = data.reduce((sum, item) => sum + item.value, 0);

  const WidgetContent = () => (
    <View style={[
      styles.container,
      {
        backgroundColor: colors.surface,
        borderColor: colors.border,
        ...shadows.light,
      }
    ]}>
      <View style={styles.header}>
        <View style={styles.titleContainer}>
          <Icon name="bar-chart" size={20} color={colors.primary} />
          <View style={styles.titleTextContainer}>
            <Text style={[styles.title, { color: colors.text, fontFamily: fonts.medium }]}>
              {title}
            </Text>
            {subtitle && (
              <Text style={[styles.subtitle, { color: colors.textLight, fontFamily: fonts.regular }]}>
                {subtitle}
              </Text>
            )}
          </View>
        </View>
      </View>

      <View style={styles.content}>
        <StackedBarChart
          data={data}
          colors={colors}
          fonts={fonts}
          height={height}
        />
      </View>

      <View style={styles.footer}>
        <View style={styles.summaryContainer}>
          <Text style={[styles.summaryLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Total: {total.toLocaleString()}
          </Text>
          <Text style={[styles.summaryLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Max: {maxValue.toLocaleString()}
          </Text>
        </View>
      </View>
    </View>
  );

  if (onPress) {
    return (
      <TouchableOpacity onPress={onPress} activeOpacity={0.8}>
        <WidgetContent />
      </TouchableOpacity>
    );
  }

  return <WidgetContent />;
};

const styles = StyleSheet.create({
  container: {
    borderRadius: 12,
    borderWidth: 1,
    padding: 16,
    marginBottom: 12,
  },
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    marginBottom: 16,
  },
  titleContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  titleTextContainer: {
    marginLeft: 8,
  },
  title: {
    fontSize: 16,
  },
  subtitle: {
    fontSize: 12,
    marginTop: 2,
  },
  content: {
    alignItems: 'center',
    justifyContent: 'center',
  },
  footer: {
    marginTop: 12,
    paddingTop: 12,
    borderTopWidth: 1,
    borderTopColor: '#E5E7EB',
  },
  summaryContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  summaryLabel: {
    fontSize: 12,
  },
});

export default BarChartWidget;
