import 'package:flutter/material.dart';
import 'dart:math' as math;

class PieChartWidget extends StatelessWidget {
  final double victoryPercentage;
  final double defeatPercentage;
  final double drawPercentage;
  final double size;

  const PieChartWidget({
    super.key,
    required this.victoryPercentage,
    required this.defeatPercentage,
    this.drawPercentage = 0,
    this.size = 140, // Aumentado para 400x300
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: size,
      height: size,
      child: CustomPaint(
        painter: _PieChartPainter(
          victoryPercentage: victoryPercentage,
          defeatPercentage: defeatPercentage,
          drawPercentage: drawPercentage,
        ),
        child: _buildCenterLabels(),
      ),
    );
  }

  Widget _buildCenterLabels() {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            '${(victoryPercentage * 100).toStringAsFixed(1)}%',
            style: TextStyle(
              fontSize: size * 0.2, // Tamanho responsivo (28px para 140px)
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
          SizedBox(height: 4),
          Text(
            'Vitórias',
            style: TextStyle(
              fontSize: size * 0.1, // Tamanho responsivo (14px para 140px)
              color: Colors.white.withOpacity(0.8),
            ),
          ),
        ],
      ),
    );
  }
}

class _PieChartPainter extends CustomPainter {
  final double victoryPercentage;
  final double defeatPercentage;
  final double drawPercentage;

  _PieChartPainter({
    required this.victoryPercentage,
    required this.defeatPercentage,
    required this.drawPercentage,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2 - 5;
    
    // Cores
    const victoryColor = Colors.green;
    const defeatColor = Colors.red;
    const drawColor = Colors.yellow;
    const borderColor = Colors.white30;

    double startAngle = 0;
    
    // Vitórias (verde)
    if (victoryPercentage > 0) {
      final sweepAngle = 2 * math.pi * victoryPercentage;
      _drawSector(canvas, center, radius, startAngle, sweepAngle, victoryColor);
      startAngle += sweepAngle;
    }
    
    // Empates (amarelo)
    if (drawPercentage > 0) {
      final sweepAngle = 2 * math.pi * drawPercentage;
      _drawSector(canvas, center, radius, startAngle, sweepAngle, drawColor);
      startAngle += sweepAngle;
    }
    
    // Derrotas (vermelho)
    if (defeatPercentage > 0) {
      final sweepAngle = 2 * math.pi * defeatPercentage;
      _drawSector(canvas, center, radius, startAngle, sweepAngle, defeatColor);
    }
    
    // Borda
    final borderPaint = Paint()
      ..color = borderColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2;
    
    canvas.drawCircle(center, radius, borderPaint);
  }

  void _drawSector(Canvas canvas, Offset center, double radius, 
                   double startAngle, double sweepAngle, Color color) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      startAngle,
      sweepAngle,
      true,
      paint,
    );
    
    // Linha divisória
    if (sweepAngle < 2 * math.pi) {
      final linePaint = Paint()
        ..color = Colors.white.withOpacity(0.5)
        ..style = PaintingStyle.stroke
        ..strokeWidth = 1.5;
      
      final lineX = center.dx + radius * math.cos(startAngle);
      final lineY = center.dy + radius * math.sin(startAngle);
      
      canvas.drawLine(center, Offset(lineX, lineY), linePaint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}