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 { PieChart, DonutChart } from '@/shared/components/charts';

interface RevenueChartProps {
  data: Array<{
    label: string;
    value: number;
    color: string;
  }>;
  title: string;
  total?: number;
  chartType?: 'pie' | 'donut';
  onPress?: () => void;
}

const RevenueChart: React.FC<RevenueChartProps> = ({
  data,
  title,
  total,
  chartType = 'donut',
  onPress,
}) => {
  const { colors, fonts, spacing, shadows } = useTheme();

  const calculatedTotal = 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="pie-chart" size={20} color={colors.primary} />
          <Text style={[styles.title, { color: colors.text, fontFamily: fonts.medium }]}>
            {title}
          </Text>
        </View>
      </View>

      <View style={styles.content}>
        <View style={styles.chartContainer}>
          {chartType === 'pie' ? (
            <PieChart
              data={data}
              size={140}
              colors={colors}
              fonts={fonts}
            />
          ) : (
            <DonutChart
              data={data}
              size={140}
              colors={colors}
              fonts={fonts}
            />
          )}
        </View>

        <View style={styles.legendContainer}>
          {data.map((item, index) => (
            <View key={index} style={styles.legendItem}>
              <View style={[styles.legendDot, { backgroundColor: item.color }]} />
              <Text style={[styles.legendLabel, { color: colors.text, fontFamily: fonts.regular }]}>
                {item.label}
              </Text>
              <Text style={[styles.legendValue, { color: colors.text, fontFamily: fonts.medium }]}>
                {item.value}
              </Text>
            </View>
          ))}
        </View>
      </View>

      {calculatedTotal > 0 && (
        <View style={styles.footer}>
          <Text style={[styles.totalLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Total: {calculatedTotal.toLocaleString()}
          </Text>
        </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',
  },
  title: {
    fontSize: 16,
    marginLeft: 8,
  },
  content: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  chartContainer: {
    flex: 1,
    alignItems: 'center',
  },
  legendContainer: {
    flex: 1,
    paddingLeft: 16,
  },
  legendItem: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 8,
  },
  legendDot: {
    width: 8,
    height: 8,
    borderRadius: 4,
    marginRight: 8,
  },
  legendLabel: {
    fontSize: 12,
    flex: 1,
  },
  legendValue: {
    fontSize: 12,
  },
  footer: {
    marginTop: 12,
    paddingTop: 12,
    borderTopWidth: 1,
    borderTopColor: '#E5E7EB',
  },
  totalLabel: {
    fontSize: 12,
    textAlign: 'center',
  },
});

export default RevenueChart;
