import 'package:flutter/material.dart';
import 'pages/feed_page.dart';
import 'pages/invitations_page.dart';
import 'pages/profile_page.dart';
import 'pages/puzzles_page.dart';
import '../services/invitations_service.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

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

  final List<Widget> _pages = [
    FeedPage(),
    InvitationsPage(),
    PuzzlesPage(),
    ProfilePage(),
  ];

  @override
  void initState() {
    super.initState();
    // Start polling and listen to invitations stream
    InvitationsService.startPolling();
    _listenToInvitations();
  }

  @override
  void dispose() {
    InvitationsService.stopPolling();
    super.dispose();
  }

  void _listenToInvitations() {
    InvitationsService.getInvitationsStream().listen((invitationsData) {
      if (mounted) {
        setState(() {
          _availableInvitationsCount = invitationsData.available.length;
        });
      }
    });
  }

  Widget _buildInvitationsBadge() {
    if (_availableInvitationsCount == 0) {
      return Icon(Icons.mail);
    }

    return Stack(
      children: [
        Icon(Icons.mail),
        Positioned(
          right: 0,
          top: 0,
          child: Container(
            padding: EdgeInsets.all(1),
            decoration: BoxDecoration(
              color: Colors.red,
              borderRadius: BorderRadius.circular(10),
            ),
            constraints: BoxConstraints(minWidth: 16, minHeight: 16),
            child: Text(
              _availableInvitationsCount > 99
                  ? '99+'
                  : '$_availableInvitationsCount',
              style: TextStyle(
                color: Colors.white,
                fontSize: 10,
                fontWeight: FontWeight.bold,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false, // Prevent going back to authentication screens
      child: Scaffold(
        body: _pages[_currentIndex],
        bottomNavigationBar: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.black12,
                blurRadius: 4,
                offset: Offset(0, -2),
              ),
            ],
          ),
          child: SafeArea(
            child: Container(
              height: 80,
              child: BottomNavigationBar(
                currentIndex: _currentIndex,
                onTap: (index) {
                  setState(() {
                    _currentIndex = index;
                  });
                },
                type: BottomNavigationBarType.fixed,
                selectedItemColor: Color(0xFF6A4C93),
                unselectedItemColor: Colors.grey,
                backgroundColor: Colors.transparent,
                elevation: 0,
                items: [
                  BottomNavigationBarItem(
                    icon: Icon(Icons.home),
                    label: 'Feed',
                  ),
                  BottomNavigationBarItem(
                    icon: _buildInvitationsBadge(),
                    label: 'Invitations',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.games),
                    label: 'Puzzles',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.person),
                    label: 'Profile',
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
