import 'package:flutter/material.dart';

import '../models/school.dart';
import '../screens/home_page.dart';
import '../screens/lessons_page.dart';
import '../screens/account_page.dart';
import '../screens/meditation_page.dart';

/// Bottom navigation centralizzata.
/// Gestisce da sé la navigazione tra le 4 tab principali.
/// Ogni pagina passa solo il proprio [currentIndex] e i dati utente.
///
/// [onBeforeLeave] è opzionale: le pagine che devono fare pulizia prima di
/// uscire (es. Meditazione che ferma musica/timer) lo passano.
class AppBottomNav extends StatelessWidget {
  final int currentIndex;
  final String token;
  final School school;
  final String? userFirstName;
  final Future<void> Function()? onBeforeLeave;

  const AppBottomNav({
    super.key,
    required this.currentIndex,
    required this.token,
    required this.school,
    this.userFirstName,
    this.onBeforeLeave,
  });

  Future<void> _go(BuildContext context, int i) async {
    if (i == currentIndex) return;

    // pulizia eventuale (musica/timer) prima di lasciare la pagina
    if (onBeforeLeave != null) {
      await onBeforeLeave!();
    }
    if (!context.mounted) return;

    Widget page;
    switch (i) {
      case 0:
        page = HomePage(
          token: token,
          school: school,
          userFirstName: userFirstName,
        );
        break;
      case 1:
        page = LessonsPage(
          token: token,
          school: school,
          userFirstName: userFirstName,
        );
        break;
      case 2:
        page = AccountPage(
          token: token,
          school: school,
          userFirstName: userFirstName,
        );
        break;
      case 3:
        page = MeditationPage(
          token: token,
          school: school,
          userFirstName: userFirstName,
        );
        break;
      default:
        return;
    }

    Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => page));
  }

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      backgroundColor: Colors.white,
      selectedItemColor: const Color(0xFF10B981),
      unselectedItemColor: Colors.black54,
      currentIndex: currentIndex,
      onTap: (i) => _go(context, i),
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home_rounded), label: 'Home'),
        BottomNavigationBarItem(
          icon: Icon(Icons.event_note_rounded),
          label: 'Lezioni',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.person_rounded),
          label: 'Account',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.self_improvement_rounded),
          label: 'Meditazione',
        ),
      ],
    );
  }
}
