import 'package:comwell_key_app/check_out/bloc/check_out_cubit.dart';
import 'package:comwell_key_app/check_out/bloc/check_out_state.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../pages/check_out_page.dart';
import 'confirm_check_out_dialog.dart';

class CheckOutBottomSheet extends StatelessWidget {
  final CheckoutState state;
  const CheckOutBottomSheet({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    final cubit = context.read<CheckoutCubit>();
    final theme = Theme.of(context);
    final isEnabled = state.isTermsAccepted;

    final buttonStyle = ButtonStyle(
      backgroundColor: WidgetStateProperty.resolveWith((states) {
        if (states.contains(WidgetState.disabled)) {
          return sandColor.withValues(alpha: 0.5);
        }
        return sandColor;
      }),
      foregroundColor: const WidgetStatePropertyAll(colorBackground),
    );

    return Container(
      decoration:
          const BoxDecoration(shape: BoxShape.rectangle, color: colorBackground),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Divider(color: colorDivider),
          Padding(
            padding: const EdgeInsets.only(
                left: 18.0, right: 18.0, bottom: 32.0, top: 18.0),
            child: Row(
              children: [
                Expanded(
                  child: Builder(builder: (context) {
                    if (cubit.state.page == CheckoutPage.confirmation) {
                      return ElevatedButton(
                          onPressed:  (cubit.booking.balance == 0
                                  ? () async {
                                      await showDialog<void>(
                                        context: context,
                                        builder: (context) =>
                                            ConfirmCheckOutDialog(onConfirm: () {
                                          context.pop();
                                          cubit.onContinueClicked(context);
                                        }),
                                      );
                                    }
                                  : () => cubit.onContinueClicked(context))
                              ,
                          style: buttonStyle,
                          child: Padding(
                            padding: const EdgeInsets.symmetric(vertical: 17.0),
                            child: Text(
                              cubit.booking.balance == 0 || cubit.booking.balance == null
                                  ? context.strings.checkout_page_confirmation
                                  : context.strings.checkout_page_confirmation_continue,
                              style: theme.textTheme.bodyMedium
                                  ?.copyWith(color: colorBackground),
                            ),
                          ));
                    } else {
                      return ElevatedButton(
                          onPressed: isEnabled
                              ? () async {
                                  await showDialog<void>(
                                    context: context,
                                    builder: (context) =>
                                        ConfirmCheckOutDialog(onConfirm: () {
                                      Navigator.of(context).pop();
                                      cubit.onContinueClicked(context);
                                    }),
                                  );
                                }
                              : null,
                          style: buttonStyle,
                          child: Padding(
                            padding: const EdgeInsets.symmetric(vertical: 17.0),
                            child: Text(
                              context.strings.checkout_page_payment_payment_title,
                              style: theme.textTheme.bodyMedium
                                  ?.copyWith(color: colorBackground),
                            ),
                          ));
                    }
                  }),
                ),
              ],
            ),
          ),
          const SizedBox(height: 8)
        ],
      ),
    );
  }
}