import 'package:flutter/material.dart';
import 'package:playmaker/grafico%20de%20pizza/widgets/grafico_widgets.dart';
import 'dados_grafico.dart';
import 'controllers/contollers_grafico.dart';
import 'package:playmaker/classe/home.config.dart';

class PieChartCard extends StatefulWidget {
  final PieChartController? controller;
  final String title;
  final String subtitle;
  final Color backgroundColor;
  final VoidCallback? onTap;

  const PieChartCard({
    super.key,
    this.controller,
    this.title = 'DESEMPENHO',
    this.subtitle = 'Vitórias vs Derrotas',
    this.onTap, required this.backgroundColor,
  });

  @override
  State<PieChartCard> createState() => _PieChartCardState();
}

class _PieChartCardState extends State<PieChartCard> with SingleTickerProviderStateMixin {
  late PieChartController _controller;
  late AnimationController _animationController;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? PieChartController();
    
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 600),
      vsync: this,
    );
    
    _animation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeOutBack,
      ),
    );
    
    _animationController.forward();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final data = _controller.chartData;
    
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        return Transform.scale(
          scale: 0.95 + (_animation.value * 0.05),
          child: Opacity(
            opacity: _animation.value,
            child: child,
          ),
        );
      },
      child: Container(
        width: HomeConfig.cardwidthPadding,
        height: HomeConfig.cardheightPadding,
        child: Card(
          elevation: 0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20),
          ),
          child: InkWell(
            onTap: widget.onTap,
            borderRadius: BorderRadius.circular(20),
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    widget.backgroundColor.withOpacity(0.9),
                    widget.backgroundColor.withOpacity(0.7),
                  ],
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Cabeçalho compacto
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              widget.title,
                              style: TextStyle(
                                fontSize: 14, // Pequeno
                                fontWeight: FontWeight.bold,
                                color: Colors.white.withOpacity(0.9),
                                letterSpacing: 1.5,
                              ),
                            ),
                            SizedBox(height: 2), // Muito pequeno
                            Text(
                              widget.subtitle,
                              style: TextStyle(
                                fontSize: 16, // Moderado
                                fontWeight: FontWeight.bold,
                                color: Colors.white,
                              ),
                            ),
                          ],
                        ),
                        Container(
                          padding: EdgeInsets.all(6), // Pequeno
                          decoration: BoxDecoration(
                            color: Colors.orange.withOpacity(0.8),
                            shape: BoxShape.circle,
                          ),
                          child: Icon(
                            Icons.pie_chart,
                            size: 18, // Pequeno
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                    
                    SizedBox(height: 8), // Pequeno espaço
                    
                    // Conteúdo principal - COMPACTO E CONTROLADO
                    Expanded(
                      child: Row(
                        children: [
                          // Gráfico de pizza
                          Expanded(
                            flex: 3,
                            child: Center(
                              child: PieChartWidget(
                                victoryPercentage: data.victoryPercentage,
                                defeatPercentage: data.defeatPercentage,
                                drawPercentage: data.drawPercentage,
                                size: 130, // Pequeno para caber
                              ),
                            ),
                          ),
                          
                          SizedBox(width: 8), // Pequeno espaço
                          
                          // Estatísticas - EXTREMAMENTE COMPACTAS
                          Expanded(
                            flex: 2,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                // Vitórias - Layout horizontal ultra compacto
                                Container(
                                  margin: EdgeInsets.only(bottom: 6), // Muito pequeno
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      // Número
                                      Text(
                                        data.victories.toString(),
                                        style: TextStyle(
                                          fontSize: 26, // Pequeno mas legível
                                          fontWeight: FontWeight.bold,
                                          color: Colors.green,
                                          height: 0.8,
                                        ),
                                      ),
                                      SizedBox(width: 6), // Pequeno
                                      // Info compacta
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          mainAxisSize: MainAxisSize.min,
                                          children: [
                                            Row(
                                              children: [
                                                Container(
                                                  width: 8,
                                                  height: 8,
                                                  margin: EdgeInsets.only(right: 4),
                                                  decoration: BoxDecoration(
                                                    color: Colors.green,
                                                    shape: BoxShape.circle,
                                                  ),
                                                ),
                                                Text(
                                                  'VIT',
                                                  style: TextStyle(
                                                    fontSize: 10, // Muito pequeno
                                                    color: Colors.white.withOpacity(0.8),
                                                    fontWeight: FontWeight.w600,
                                                  ),
                                                ),
                                              ],
                                            ),
                                            SizedBox(height: 2),
                                            Text(
                                              '${(data.victoryPercentage * 100).toStringAsFixed(0)}%',
                                              style: TextStyle(
                                                fontSize: 12, // Pequeno
                                                color: Colors.green,
                                                fontWeight: FontWeight.bold,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                
                                // Divisor sutil
                                Container(
                                  height: 0.5,
                                  color: Colors.white.withOpacity(0.1),
                                  margin: EdgeInsets.symmetric(vertical: 4),
                                ),
                                
                                // Derrotas
                                Container(
                                  margin: EdgeInsets.only(bottom: 6),
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Text(
                                        data.defeats.toString(),
                                        style: TextStyle(
                                          fontSize: 26,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.red,
                                          height: 0.8,
                                        ),
                                      ),
                                      SizedBox(width: 6),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          mainAxisSize: MainAxisSize.min,
                                          children: [
                                            Row(
                                              children: [
                                                Container(
                                                  width: 8,
                                                  height: 8,
                                                  margin: EdgeInsets.only(right: 4),
                                                  decoration: BoxDecoration(
                                                    color: Colors.red,
                                                    shape: BoxShape.circle,
                                                  ),
                                                ),
                                                Text(
                                                  'DER',
                                                  style: TextStyle(
                                                    fontSize: 10,
                                                    color: Colors.white.withOpacity(0.8),
                                                    fontWeight: FontWeight.w600,
                                                  ),
                                                ),
                                              ],
                                            ),
                                            SizedBox(height: 2),
                                            Text(
                                              '${(data.defeatPercentage * 100).toStringAsFixed(0)}%',
                                              style: TextStyle(
                                                fontSize: 12,
                                                color: Colors.red,
                                                fontWeight: FontWeight.bold,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                
                                // Divisor sutil
                                Container(
                                  height: 0.5,
                                  color: Colors.white.withOpacity(0.1),
                                  margin: EdgeInsets.symmetric(vertical: 4),
                                ),
                                
                                // Total
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Text(
                                      data.total.toString(),
                                      style: TextStyle(
                                        fontSize: 26,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white,
                                        height: 0.8,
                                      ),
                                    ),
                                    SizedBox(width: 6),
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          Row(
                                            children: [
                                              Container(
                                                width: 8,
                                                height: 8,
                                                margin: EdgeInsets.only(right: 4),
                                                decoration: BoxDecoration(
                                                  color: Colors.white,
                                                  shape: BoxShape.circle,
                                                ),
                                              ),
                                              Text(
                                                'TOT',
                                                style: TextStyle(
                                                  fontSize: 10,
                                                  color: Colors.white.withOpacity(0.8),
                                                  fontWeight: FontWeight.w600,
                                                ),
                                              ),
                                            ],
                                          ),
                                          SizedBox(height: 2),
                                          Text(
                                            '100%',
                                            style: TextStyle(
                                              fontSize: 12,
                                              color: Colors.white,
                                              fontWeight: FontWeight.bold,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    
                    SizedBox(height: 10), // Espaço controlado
                    
                    // Win rate - Sempre visível e não sobreposto
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.1),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            data.victoryPercentage > 0.5 
                              ? Icons.trending_up 
                              : Icons.trending_down,
                            color: data.victoryPercentage > 0.5 
                              ? Colors.green 
                              : Colors.red,
                            size: 18, // Pequeno
                          ),
                          SizedBox(width: 8),
                          Text(
                            'Win Rate: ${(data.victoryPercentage * 100).toStringAsFixed(1)}%',
                            style: TextStyle(
                              fontSize: 14, // Pequeno
                              fontWeight: FontWeight.bold,
                              color: Colors.white,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}