import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/car_selection/bloc/bloc.dart';
import 'package:hl_lieferservice/feature/car_selection/bloc/events.dart';
import 'package:hl_lieferservice/feature/car_selection/bloc/state.dart';
import 'package:hl_lieferservice/feature/cars/presentation/car_management_page.dart';
import 'package:hl_lieferservice/feature/settings/presentation/settings_page.dart';

/// Globales Side-Menu für Fahrzeuge & Einstellungen.
///
/// Da Sortieren und Beladen kein BottomNav mehr haben, sind diese
/// administrativen Funktionen nur noch über den Drawer erreichbar.
/// Während der Auslieferungs-Phase steht zusätzlich das alte BottomNav
/// weiterhin zur Verfügung.
class HomeAppDrawer extends StatelessWidget {
  const HomeAppDrawer({super.key});

  void _openPage(BuildContext context, Widget page) {
    Navigator.of(context).pop();
    Navigator.of(context).push(
      MaterialPageRoute(builder: (_) => page),
    );
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final carState = context.watch<CarSelectBloc>().state;

    return Drawer(
      child: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            DrawerHeader(
              decoration: BoxDecoration(color: theme.primaryColor),
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    "Holzleitner",
                    style: TextStyle(
                      color: theme.colorScheme.onSecondary,
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 4),
                  if (carState is CarSelectComplete)
                    Row(
                      children: [
                        Icon(
                          Icons.local_shipping,
                          color: theme.colorScheme.onSecondary,
                          size: 16,
                        ),
                        const SizedBox(width: 6),
                        Text(
                          carState.selectedCar.plate,
                          style: TextStyle(
                            color: theme.colorScheme.onSecondary,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    )
                  else
                    Text(
                      "Kein Fahrzeug ausgewählt",
                      style: TextStyle(
                        color: theme.colorScheme.onSecondary.withValues(
                          alpha: 0.8,
                        ),
                        fontSize: 13,
                      ),
                    ),
                ],
              ),
            ),
            if (carState is CarSelectComplete)
              ListTile(
                leading: const Icon(Icons.swap_horiz),
                title: const Text("Fahrzeug wechseln"),
                onTap: () {
                  Navigator.of(context).pop();
                  context.read<CarSelectBloc>().add(CarSelectChange());
                },
              ),
            ListTile(
              leading: const Icon(Icons.local_shipping_outlined),
              title: const Text("Fahrzeuge verwalten"),
              onTap: () => _openPage(context, const CarManagementPage()),
            ),
            ListTile(
              leading: const Icon(Icons.settings_outlined),
              title: const Text("Einstellungen"),
              onTap: () => _openPage(context, const SettingsPage()),
            ),
            const Spacer(),
            const Divider(height: 1),
            const Padding(
              padding: EdgeInsets.all(12),
              child: Text(
                "Lieferservice-App",
                style: TextStyle(fontSize: 11, color: Colors.grey),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
