import 'package:comwell_key_app/common/components/comwell_app_bar.dart';
import 'package:comwell_key_app/my_booking/cubit/my_booking_cubit.dart';
import 'package:comwell_key_app/my_booking/cubit/my_booking_state.dart';
import 'package:comwell_key_app/overview/models/booking.dart';
import 'package:comwell_key_app/presentation/screens/booking_details/bloc/booking_details_cubit.dart';
import 'package:comwell_key_app/presentation/screens/booking_details/components/share_button.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/services/mappers/booking_mapper.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:comwell_key_app/utils/locator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:payment_plugin/presentation/app/bloc/payment_cubit.dart';
import 'package:payment_plugin/presentation/app/bloc/payment_processing_state.dart';

import '../.generated/assets/assets.gen.dart';

class MyBookingPage extends StatelessWidget {
  final Booking booking;

  const MyBookingPage({super.key, required this.booking});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return BlocBuilder<MyBookingCubit, MyBookingState>(
      builder: (context, state) {
        return BlocListener<PaymentCubit, PaymentProcessingState>(
          listener: (context, state) async {
            if (state is PaymentProcessingStateConfirmed) {
              await Future<void>.delayed(const Duration(seconds: 2));
              if (!context.mounted) return;
              Navigator.of(
                context,
              ).popUntil((route) => route.settings.name == AppRoutes.bookingDetails);
            }
          },
          child: Builder(
            builder: (context) {
              return Scaffold(
                backgroundColor: Colors.white,
                appBar: const ComwellAppBar(
                  shouldShowProfileButton: false,
                ),
                body: Stack(
                  children: [
                    SingleChildScrollView(
                      child: Padding(
                        padding: const EdgeInsets.all(16.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              context.strings.my_booking,
                              style: theme.textTheme.titleLarge?.copyWith(
                                fontWeight: FontWeight.bold,
                                color: colorTertiary,
                              ),
                            ),
                            const SizedBox(height: 8),
                            Text(
                              context.strings.booking_reference,
                              style: theme.textTheme.bodySmall?.copyWith(
                                color: colorHeadlineText,
                              ),
                            ),
                            Text(
                              booking.confirmationNumber,
                              style: theme.textTheme.bodyMedium,
                            ),
                            const SizedBox(height: 16),
                            const Divider(color: colorDivider),
                            _buildCheckInOutSection(context, theme),
                            const Divider(color: colorDivider),
                            _buildBookingDetails(context, theme),
                            const Divider(color: colorDivider),
                            _buildPaymentSection(context, theme),
                            const Divider(color: colorDivider),
                            _buildCancellationPolicy(context, theme),
                            const Divider(color: colorDivider),
                            const SizedBox(height: 100),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        );
      },
    );
  }

  Widget _buildCheckInOutSection(BuildContext context, ThemeData theme) {
    return Row(
      children: [
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                context.strings.check_in,
                style: theme.textTheme.bodySmall?.copyWith(
                  color: colorHeadlineText,
                ),
              ),
              const SizedBox(height: 4),
              Text(
                DateFormat('d. MMM').format(booking.startDate),
                style: theme.textTheme.bodyMedium,
              ),
              Text(
                DateFormat('HH:mm').format(booking.startDate.add(const Duration(hours: 15))),
                style: theme.textTheme.bodyMedium,
              ),
            ],
          ),
        ),
        const Expanded(child: Icon(Icons.arrow_forward)),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                context.strings.check_out,
                style: theme.textTheme.bodySmall?.copyWith(
                  color: colorHeadlineText,
                ),
              ),
              const SizedBox(height: 4),
              Text(
                DateFormat('d. MMM').format(booking.endDate),
                style: theme.textTheme.bodyMedium,
              ),
              const SizedBox(height: 4),
              Text(
                DateFormat('HH:mm').format(booking.endDate.add(const Duration(hours: 10))),
                style: theme.textTheme.bodyMedium,
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildBookingDetails(BuildContext context, ThemeData theme) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          context.strings.booking_details,
          style: theme.textTheme.bodySmall?.copyWith(
            color: colorHeadlineText,
          ),
        ),
        Text(booking.toRoomType(), style: theme.textTheme.bodyMedium),
        const SizedBox(height: 16),
        const Divider(color: colorDivider),
        Text(
          context.strings.number_of_guests,
          style: theme.textTheme.bodySmall?.copyWith(
            color: colorHeadlineText,
          ),
        ),
        Text(
          '${booking.adults} ${booking.adults > 1 ? context.strings.adults : context.strings.adult}${booking.children > 0 ? ' | ${booking.children} ${booking.children > 1 ? context.strings.children : context.strings.child}' : ''}',
          style: theme.textTheme.bodyMedium,
        ),
        const SizedBox(height: 8),
        const Divider(color: colorDivider),
        Text(
          context.strings.booker,
          style: theme.textTheme.bodySmall?.copyWith(
            color: colorHeadlineText,
          ),
        ),
        Text("${booking.firstName} ${booking.lastName}", style: theme.textTheme.bodyMedium),
        const SizedBox(height: 8),
        const Divider(color: colorDivider),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  context.strings.share_booking,
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: colorHeadlineText,
                  ),
                ),
                Text(context.strings.not_shared, style: theme.textTheme.bodyMedium),
              ],
            ),
            SizedBox(
              width: 100,
              height: 70,
              child: BlocProvider(
                create: (context) => BookingDetailsCubit(
                  booking: booking,
                  locator(),
                  locator(),
                  locator(),
                  locator(),
                  locator(),
                  locator(),
                ),
                child: ShareButton(
                  isMyBooking: true,
                  guests: booking.guests,
                  buttonColor: sandColor[40],
                  userButtonSize: 30,
                  userButtonOverlap: 5,
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildPaymentSection(BuildContext context, ThemeData theme) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          context.strings.payment,
          style: theme.textTheme.headlineLarge?.copyWith(
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 16),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  context.strings.payment_method,
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: colorHeadlineText,
                  ),
                ),
                const SizedBox(width: 8),
                Text('•••• •••• •••• ${booking.maskedCardNumber}', style: theme.textTheme.bodyMedium),
                const SizedBox(width: 8),
              ],
            ),
            Assets.images.master.svg(width: 30, height: 30),
          ],
        ),
        const SizedBox(height: 16),
        const Divider(color: colorDivider),
        const SizedBox(height: 8),
        Text(
          context.strings.payment_date,
          style: theme.textTheme.bodySmall?.copyWith(
            color: colorHeadlineText,
          ),
        ),
        const SizedBox(height: 4),
        Text(DateFormat('d. MMM').format(booking.bookingDate), style: theme.textTheme.bodyMedium),
        const SizedBox(height: 16),
      ],
    );
  }

  Widget _buildCancellationPolicy(BuildContext context, ThemeData theme) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          context.strings.cancellation_policy,
          style: theme.textTheme.bodySmall?.copyWith(
            color: colorHeadlineText,
          ),
        ),
        const SizedBox(height: 8),
        Text(
          context.strings.cancellation_policy_description,
          style: theme.textTheme.bodyMedium,
        ),
      ],
    );
  }
}