import 'package:flutter/material.dart';
import 'package:playmaker/classe/home.config.dart';
import 'package:playmaker/grafico%20de%20pizza/grafico.dart';
import 'package:playmaker/pages/gamePage.dart';
import 'package:playmaker/pages/teamPage.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _selectedIndex = 0;


  late final List<Widget> _pages;

  @override
  void initState() {
    super.initState();
    _pages = [
      _buildHomeContent(),
      const GamePage(),
      const TeamsPage(),                         
      const Center(child: Text('Tela de Status')), 
    ];
  }

  void _onItemSelected(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('PlayMaker'),
        backgroundColor: HomeConfig.primaryColor,
        foregroundColor: Colors.white,
        leading: IconButton(
          icon: const Icon(Icons.person),
          onPressed: () {},
        ),
      ),
      
      body: IndexedStack(
        index: _selectedIndex,
        children: _pages,
      ),

      bottomNavigationBar: NavigationBar(
        selectedIndex: _selectedIndex,
        onDestinationSelected: _onItemSelected,
        backgroundColor: Theme.of(context).colorScheme.surface,
        surfaceTintColor: Theme.of(context).colorScheme.surfaceTint,
        elevation: 1,
        height: 70,
        destinations: const [
          NavigationDestination(
            icon: Icon(Icons.home_outlined),
            selectedIcon: Icon(Icons.home_filled),
            label: 'Home',
          ),
          NavigationDestination(
            icon: Icon(Icons.sports_soccer_outlined),
            selectedIcon: Icon(Icons.sports_soccer),
            label: 'Jogo',
          ),
          NavigationDestination(
            icon: Icon(Icons.people_outline),
            selectedIcon: Icon(Icons.people),
            label: 'Equipas',
          ),
          NavigationDestination(
            icon: Icon(Icons.insights_outlined),
            selectedIcon: Icon(Icons.insights),
            label: 'Status',
          ),
        ],
      ),
    );
  }
  Widget _buildHomeContent() {
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildStatCard(
                  title: 'Mais Pontos',
                  playerName: 'Michael Jordan',
                  statValue: '34.5',
                  statLabel: 'PPG',
                  color: Colors.blue[800]!,
                  icon: Icons.sports_basketball,
                  isHighlighted: true,
                ),
                const SizedBox(width: 20),
                _buildStatCard(
                  title: 'Mais Assistências',
                  playerName: 'Magic Johnson',
                  statValue: '12.8',
                  statLabel: 'APG',
                  color: Colors.green[800]!,
                  icon: Icons.sports_basketball,
                  isHighlighted: false,
                ),
              ],
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildStatCard(
                  title: 'Mais Rebotes',
                  playerName: 'Dennis Rodman',
                  statValue: '15.3',
                  statLabel: 'RPG',
                  color: Colors.purple[800]!,
                  icon: Icons.sports_basketball,
                  isHighlighted: false,
                ),
                const SizedBox(width: 20),
                PieChartCard(
                  title: 'DESEMPENHO',
                  subtitle: 'Vitórias vs Derrotas',
                  backgroundColor: Colors.red[800]!,
                  onTap: () {},
                ),
              ],
            ),
            const SizedBox(height: 40),
            Text(
              'Histórico de Jogos',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
                color: Colors.grey[800],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatCard({
    required String title,
    required String playerName,
    required String statValue,
    required String statLabel,
    required Color color,
    required IconData icon,
    bool isHighlighted = false,
  }) {
    return SizedBox(
      width: HomeConfig.cardwidthPadding,
      height: HomeConfig.cardheightPadding,
      child: Card(
        elevation: 0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
          side: isHighlighted 
            ? const BorderSide(color: Colors.amber, width: 2)
            : BorderSide.none,
        ),
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20),
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [color.withOpacity(0.9), color.withOpacity(0.7)],
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(title.toUpperCase(), style: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white70)),
                          Text(playerName, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white)),
                        ],
                      ),
                    ),
                    if (isHighlighted) const Icon(Icons.star, color: Colors.amber, size: 20),
                  ],
                ),
                const Spacer(),
                Center(
                  child: Text(statValue, style: const TextStyle(fontSize: 38, fontWeight: FontWeight.bold, color: Colors.white)),
                ),
                Center(
                  child: Text(statLabel, style: const TextStyle(fontSize: 12, color: Colors.white70)),
                ),
                const Spacer(),
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  decoration: BoxDecoration(color: Colors.white24, borderRadius: BorderRadius.circular(10)),
                  child: const Center(child: Text('VER DETALHES', style: TextStyle(color: Colors.white, fontSize: 12))),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}