import 'package:adyen_checkout/adyen_checkout.dart';
import 'package:common/localization/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:payment_plugin/presentation/app/bloc/payment_cards_cubit.dart';
import 'package:payment_plugin/themes/light_theme.dart';

class AddCard extends StatelessWidget {
  const AddCard({super.key});

  Future<void> showPaymentDialog(BuildContext context) async {
    final cubit = context.read<PaymentCardsCubit>();
    //Extracting the scheme payment method config from the available payment methods
    final paymentMethodConfig = cubit
        .paymentConfigurations?.availablePaymentMethods
        .firstWhere((element) => element["type"] == "scheme");

    await showModalBottomSheet<void>(
      isDismissible: true,
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return SafeArea(
            child: Padding(
          padding: MediaQuery.of(context).viewInsets,
          child: AdyenCardComponent(
            configuration:
                cubit.paymentConfigurations?.cardComponentConfiguration ??
                    CardComponentConfiguration(
                      environment: Environment.test,
                      countryCode: "DK",
                      clientKey: "test",
                    ),
            paymentMethod: paymentMethodConfig ?? const {"type": "", "name": ""},
            checkout: cubit.sessionCheckout(),
            onPaymentResult: (result) => cubit.onPaymentResult(result, context),
          ),
        ));
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () async {
        await showPaymentDialog(context);
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color: colorDivider),
        ),
        padding: const EdgeInsets.all(12),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              context.strings.preregistration_payment_add_card,
              style: Theme.of(context).textTheme.bodyMedium,
            ),
            const SizedBox(width: 12),
            SvgPicture.asset("assets/icons/visa.svg"),
            const SizedBox(width: 4),
            SvgPicture.asset("assets/icons/mastercard.svg"),
            const SizedBox(width: 4),
            SvgPicture.asset("assets/icons/maestro.svg"),
            const Expanded(child: SizedBox()),
            SvgPicture.asset("assets/icons/ic_plus.svg"),
          ],
        ),
      ),
    );
  }
}