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/state.dart';
import 'package:hl_lieferservice/feature/delivery/model/delivery_phase.dart';
import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery_fail_page.dart';
import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery_overview.dart';
import 'package:hl_lieferservice/model/tour.dart';
import 'package:hl_lieferservice/widget/home/presentation/home_drawer.dart';
import 'package:hl_lieferservice/widget/phase_stepper/phase_stepper.dart';
import '../../bloc/tour_bloc.dart';
import '../../bloc/tour_state.dart';

/// Inhalt der Phase "Ausliefern". Sortieren und Beladen werden über eigene
/// Pages und das Phasen-Routing in `Home` gerendert — diese Page übernimmt
/// nur noch die letzte Phase. Der Phasen-Stepper bleibt sichtbar, damit der
/// Fahrer bei Bedarf zurückspringen kann; das BottomNav der Auslieferung
/// liegt im umgebenden `Home`-Scaffold.
class DeliveryOverviewPage extends StatefulWidget {
  const DeliveryOverviewPage({super.key});

  @override
  State<StatefulWidget> createState() => _DeliveryOverviewPageState();
}

class _DeliveryOverviewPageState extends State<DeliveryOverviewPage> {
  Widget _buildOverviewWithBanner({
    required Tour tour,
    required String bannerText,
  }) {
    return Column(
      children: [
        Material(
          color: Colors.amber.shade100,
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: Row(
              children: [
                const SizedBox(
                  width: 16,
                  height: 16,
                  child: CircularProgressIndicator(strokeWidth: 2),
                ),
                const SizedBox(width: 12),
                Expanded(child: Text(bannerText)),
              ],
            ),
          ),
        ),
        Expanded(
          child: DeliveryOverview(tour: tour),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final carState = context.watch<CarSelectBloc>().state;
    final carId = carState is CarSelectComplete
        ? carState.selectedCar.id.toString()
        : "";

    return Scaffold(
      // Drawer ist hier ebenfalls aktiv, damit der Menü-Button des Steppers
      // konsistent über alle Phasen funktioniert.
      drawer: const HomeAppDrawer(),
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(140),
        child: PhaseStepper(
          currentPhase: DeliveryPhase.ausliefern,
          carId: carId,
        ),
      ),
      body: BlocBuilder<TourBloc, TourState>(
        builder: (context, state) {
          if (state is TourLoaded) {
            if (state.distances == null) {
              return _buildOverviewWithBanner(
                tour: state.tour,
                bannerText: "Berechne Distanzen…",
              );
            }
            return DeliveryOverview(tour: state.tour);
          }

          if (state is TourLoadingFailed) {
            return DeliveryLoadingFailedPage();
          }

          return const Center(child: CircularProgressIndicator());
        },
      ),
    );
  }
}
