import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Svg, { Circle, G } from 'react-native-svg';

interface DonutChartData {
  label: string;
  value: number;
  color: string;
}

interface DonutChartProps {
  data: DonutChartData[];
  size?: number;
  colors: any;
  fonts: any;
}

const DonutChart: React.FC<DonutChartProps> = ({ 
  data, 
  size = 140, 
  colors, 
  fonts 
}) => {
  const total = data.reduce((sum, item) => sum + item.value, 0);
  
  if (total === 0) {
    return (
      <View style={styles.container}>
        <View style={[styles.chartContainer, { width: size, height: size }]}>
          <Text style={[styles.noDataText, { color: colors.textLight, fontFamily: fonts.regular }]}>
            No Data
          </Text>
        </View>
      </View>
    );
  }

  let currentAngle = 0;
  const radius = (size - 40) / 2;
  const innerRadius = radius * 0.6;
  const centerX = size / 2;
  const centerY = size / 2;

  return (
    <View style={styles.container}>
      <View style={styles.chartContainer}>
        <Svg width={size} height={size}>
          {/* Background circle */}
          <Circle
            cx={centerX}
            cy={centerY}
            r={radius}
            fill="transparent"
            stroke={colors.border}
            strokeWidth={16}
          />
          
          {/* Inner circle (donut hole) */}
          <Circle
            cx={centerX}
            cy={centerY}
            r={innerRadius}
            fill={colors.surface}
          />
          
          {data.map((item, index) => {
            const percentage = (item.value / total) * 100;
            const angle = (percentage / 100) * 360;
            const startAngle = currentAngle;
            
            currentAngle += angle;
            
            if (percentage === 0) return null;
            
            return (
              <G key={index}>
                <Circle
                  cx={centerX}
                  cy={centerY}
                  r={radius}
                  fill="transparent"
                  stroke={item.color}
                  strokeWidth={16}
                  strokeDasharray={`${(percentage / 100) * (2 * Math.PI * radius)} ${2 * Math.PI * radius}`}
                  strokeDashoffset={-(startAngle / 360) * (2 * Math.PI * radius)}
                  transform={`rotate(-90 ${centerX} ${centerY})`}
                />
              </G>
            );
          })}
        </Svg>
        <View style={styles.centerText}>
          <Text style={[styles.totalText, { color: colors.text, fontFamily: fonts.bold }]}>
            {total}
          </Text>
          <Text style={[styles.totalLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Sources
          </Text>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
  },
  chartContainer: {
    position: 'relative',
    alignItems: 'center',
    justifyContent: 'center',
  },
  centerText: {
    position: 'absolute',
    alignItems: 'center',
    justifyContent: 'center',
  },
  totalText: {
    fontSize: 16,
    fontWeight: 'bold',
  },
  totalLabel: {
    fontSize: 9,
    marginTop: 2,
  },
  noDataText: {
    fontSize: 14,
    textAlign: 'center',
  },
});

export default DonutChart;
