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

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

interface PieChartProps {
  data: PieChartData[];
  size?: number;
  colors: any;
  fonts: any;
}

const PieChart: React.FC<PieChartProps> = ({ 
  data, 
  size = 120, 
  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 centerX = size / 2;
  const centerY = size / 2;

  const createArcPath = (startAngle: number, endAngle: number, radius: number) => {
    const start = polarToCartesian(centerX, centerY, radius, endAngle);
    const end = polarToCartesian(centerX, centerY, radius, startAngle);
    const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
    
    return `M ${start.x} ${start.y} A ${radius} ${radius} 0 ${largeArcFlag} 0 ${end.x} ${end.y} L ${centerX} ${centerY} Z`;
  };

  const polarToCartesian = (centerX: number, centerY: number, radius: number, angleInDegrees: number) => {
    const angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
    return {
      x: centerX + (radius * Math.cos(angleInRadians)),
      y: centerY + (radius * Math.sin(angleInRadians))
    };
  };

  return (
    <View style={styles.container}>
      <View style={styles.chartContainer}>
        <Svg width={size} height={size}>
          {data.map((item, index) => {
            const percentage = (item.value / total) * 100;
            const angle = (percentage / 100) * 360;
            const startAngle = currentAngle;
            const endAngle = currentAngle + angle;
            
            currentAngle += angle;
            
            if (percentage === 0) return null;
            
            return (
              <G key={index}>
                <Circle
                  cx={centerX}
                  cy={centerY}
                  r={radius}
                  fill="transparent"
                  stroke={item.color}
                  strokeWidth={20}
                  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 }]}>
            Total
          </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: 18,
    fontWeight: 'bold',
  },
  totalLabel: {
    fontSize: 11,
    marginTop: 2,
  },
  noDataText: {
    fontSize: 14,
    textAlign: 'center',
  },
});

export default PieChart;
