import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/payment_view_model.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

class PaymentMethodsView extends StatelessWidget {
  final PaymentTypes paymentType;
  final Function? onTempContinue;

  const PaymentMethodsView({Key? key, required this.paymentType, this.onTempContinue}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: LocaleKeys.paymentMethod.tr(),
        profileImageUrl: MyAssets.bnCar,
        isRemoveBackButton: false,
        isDrawerEnabled: false,
      ),
      body: Container(
        padding: const EdgeInsets.only(bottom: 10, left: 21, right: 21),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            20.height,
            LocaleKeys.selectPaymentMethod.tr().toText(fontSize: 18, isBold: true),
            15.height,
            Consumer(
              builder: (BuildContext context, PaymentVM paymentVm, Widget? child) {
                return Expanded(
                  child: ListView(
                    shrinkWrap: true,
                    children: [
                      PaymentMethodContainer(
                          isSelected: paymentVm.selectedPaymentMethod == PaymentMethods.mada, onTap: () => paymentVm.updateSelectedPaymentMethod(PaymentMethods.mada), cardAsset: MyAssets.madaPng),
                      PaymentMethodContainer(
                          isSelected: paymentVm.selectedPaymentMethod == PaymentMethods.visa, onTap: () => paymentVm.updateSelectedPaymentMethod(PaymentMethods.visa), cardAsset: MyAssets.visaPng),
                      PaymentMethodContainer(
                          isSelected: paymentVm.selectedPaymentMethod == PaymentMethods.applePay,
                          onTap: () => paymentVm.updateSelectedPaymentMethod(PaymentMethods.applePay),
                          cardAsset: MyAssets.applePayPng),
                      PaymentMethodContainer(
                          isSelected: paymentVm.selectedPaymentMethod == PaymentMethods.masterCard,
                          onTap: () => paymentVm.updateSelectedPaymentMethod(PaymentMethods.masterCard),
                          cardAsset: MyAssets.mastercardPng),
                      PaymentMethodContainer(
                          isSelected: paymentVm.selectedPaymentMethod == PaymentMethods.tamara,
                          onTap: () => paymentVm.updateSelectedPaymentMethod(PaymentMethods.tamara),
                          cardAsset: MyAssets.tamaraEngPng),
                    ],
                  ),
                );
              },
            ),
            SizedBox(
              width: double.infinity,
              child: ShowFillButton(
                maxHeight: 55,
                title: LocaleKeys.continu.tr(),
                onPressed: () async {
                  bool isNeedToCallPayment = true;
                  if (onTempContinue != null) {
                    isNeedToCallPayment = await onTempContinue!();
                  }
                  if (isNeedToCallPayment) {
                    context.read<PaymentVM>().onContinuePressed(context, paymentType: paymentType);
                  }
                },
                backgroundColor: MyColors.darkPrimaryColor,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class PaymentMethodContainer extends StatelessWidget {
  final bool isSelected;
  final String cardAsset;
  final Function() onTap;

  const PaymentMethodContainer({Key? key, required this.isSelected, required this.cardAsset, required this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 10),
        width: double.infinity,
        height: 80,
        decoration: BoxDecoration(
          color: MyColors.white,
          borderRadius: BorderRadius.circular(10),
          border: Border.all(color: isSelected ? Colors.green : Colors.transparent, width: isSelected ? 2.0 : 0.0),
        ),
        child: Padding(
          padding: const EdgeInsets.all(12),
          child: Row(
            children: [
              Container(
                width: 24,
                height: 24,
                decoration: Utils.containerColorRadiusBorderWidth(isSelected ? MyColors.primaryColor : Colors.transparent, 100, Colors.grey, 0.5),
              ),
              12.width,
              Container(
                height: 80.0,
                width: 80.0,
                padding: const EdgeInsets.all(7.0),
                child: Image.asset(cardAsset),
              ),
              Utils.mFlex(1),
              isSelected ? Utils.statusContainerChip(text: "Selected", padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 10)) : const SizedBox(),
            ],
          ),
        ),
      ),
    );
  }
}
