import 'package:comwell_key_app/presentation/screens/check_in/bloc/check_in_cubit.dart';
import 'package:comwell_key_app/presentation/screens/check_in/bloc/check_in_state.dart';
import 'package:comwell_key_app/presentation/screens/check_in/check_in_flow.dart';
import 'package:comwell_key_app/presentation/screens/check_in/check_in_page.dart';
import 'package:comwell_key_app/overview/models/booking.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

final checkInRoute = GoRoute(
  path: AppRoutes.checkIn,
  builder: (context, state) {
    return BlocProvider(
      create: (context) {
        final extras = state.extra as List<dynamic>;
        final booking = extras[0] as Booking;
        final onlyKeys = extras[1] as bool;
        if (onlyKeys) {
          return CheckInCubit.initialOnlyKeys(booking);
        }
        return CheckInCubit(booking);
      },
      child: const CheckInPage(),
    );
  },
);

final checkInPaymentRoute = GoRoute(
  path: AppRoutes.checkInPayment,
  builder: (context, state) {
    final booking = state.extra as Booking;
    return BlocProvider(
      create: (context) => CheckInCubit.withPayment(booking, context.read()),
      child: BlocBuilder<CheckInCubit, CheckInState>(
        builder: (context, checkInState) {
          return CheckInFlow(key: ValueKey(checkInState));
        },
      ),
    );
  },
);