import 'dart:async';
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 StatefulWidget {
  final PaymentTypes paymentType;
  final Function? onTempContinue;

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

  @override
  State<PaymentMethodsView> createState() => _PaymentMethodsViewState();
}

class _PaymentMethodsViewState extends State<PaymentMethodsView> {
  @override
  void initState() {
    super.initState();
    // Automatically trigger payment flow when screen opens
    scheduleMicrotask(() async {
      // Show loading for 2 seconds so user can see it
      await Future.delayed(const Duration(seconds: 2));
      
      bool isNeedToCallPayment = true;
      if (widget.onTempContinue != null) {
        isNeedToCallPayment = await widget.onTempContinue!();
      }
      
      // Pop the loading screen BEFORE calling payment to avoid context issues
      if (mounted) {
        Navigator.of(context).pop();
      }
      
      if (isNeedToCallPayment && mounted) {
        // Call payment which will open webview (context still works for PaymentVM)
        await context.read<PaymentVM>().onVisaCardSelected(context, widget.paymentType);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation<Color>(MyColors.darkPrimaryColor),
            ),
            24.height,
            LocaleKeys.pleaseWait.tr().toText(
              fontSize: 16,
              color: MyColors.darkTextColor,
            ),
          ],
        ),
      ),
    );
  }
}

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(),
            ],
          ),
        ),
      ),
    );
  }
}
