import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Svg, { Rect, Text as SvgText } from 'react-native-svg';

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

interface StackedBarChartProps {
  data: StackedBarChartData[];
  colors: any;
  fonts: any;
  height?: number;
}

const StackedBarChart: React.FC<StackedBarChartProps> = ({ 
  data, 
  colors, 
  fonts,
  height = 120
}) => {
  const total = data.reduce((sum, item) => sum + item.value, 0);
  const maxValue = Math.max(...data.map(item => item.value));
  
  if (total === 0) {
    return (
      <View style={styles.container}>
        <Text style={[styles.noDataText, { color: colors.textLight, fontFamily: fonts.regular }]}>
          No Task Data
        </Text>
      </View>
    );
  }

  const chartWidth = 250;
  const barWidth = chartWidth / data.length - 10;
  const maxBarHeight = height - 40;

  return (
    <View style={styles.container}>
      <Svg width={chartWidth} height={height}>
        {data.map((item, index) => {
          const barHeight = maxValue > 0 ? (item.value / maxValue) * maxBarHeight : 0;
          const x = index * (barWidth + 10) + 5;
          const y = height - 20 - barHeight;
          const percentage = total > 0 ? (item.value / total) * 100 : 0;
          
          return (
            <View key={index}>
              <Rect
                x={x}
                y={y}
                width={barWidth}
                height={barHeight}
                fill={item.color}
                rx={4}
                ry={4}
              />
              <SvgText
                x={x + barWidth / 2}
                y={height - 5}
                fontSize="9"
                fontFamily={fonts.medium}
                fill={colors.textLight}
                textAnchor="middle"
                alignmentBaseline="baseline"
              >
                {item.label}
              </SvgText>
              <SvgText
                x={x + barWidth / 2}
                y={y - 8}
                fontSize="8"
                fontFamily={fonts.bold}
                fill={colors.textLight}
                textAnchor="middle"
                alignmentBaseline="baseline"
              >
                {item.value}
              </SvgText>
            </View>
          );
        })}
      </Svg>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: 10,
  },
  noDataText: {
    fontSize: 14,
    textAlign: 'center',
    fontStyle: 'italic',
  },
});

export default StackedBarChart;
