import 'package:flutter/material.dart';
import 'package:initial_folder/providers/order_provider.dart' as provOrder;
import 'package:initial_folder/providers/theme_provider.dart';
import 'package:initial_folder/providers/total_price_provider.dart';
import 'package:initial_folder/providers/payments_provider.dart';
import 'package:initial_folder/screens/checkout/components/course_list.dart';
import 'package:initial_folder/screens/checkout/detail_zero_payment.dart';
import 'package:initial_folder/screens/checkout/snap_payment_page.dart'; // Import SnapPaymentPage
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class CheckoutCartPage extends StatefulWidget {
  CheckoutCartPage({
    Key? key,
    required this.idCart,
    this.potonganKupon,
    this.discountHarga,
    this.isCart,
    this.isDetailCourse,
  }) : super(key: key);

  final List<String> idCart;
  final int? potonganKupon;
  final int? discountHarga;
  final bool? isCart;
  final bool? isDetailCourse;

  @override
  State<CheckoutCartPage> createState() => _CheckoutCartPageState();
}

class _CheckoutCartPageState extends State<CheckoutCartPage> {
  final controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final subTotal = Provider.of<TotalPriceProvider>(context).subTotal;
    final paymentProvider = Provider.of<PaymentsProvider>(context, listen: false);
     final themeProvider = Provider.of<ThemeProvider>(context);

    Widget bottomNav(String totalPrice) {
      String totalHarga =
          "Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(totalPrice == '0' ? widget.discountHarga : double.parse(totalPrice))}";

      return Container(
        width: double.infinity,
        height: getProportionateScreenHeight(64),
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.background,
          boxShadow: [
            BoxShadow(
              color: Theme.of(context).colorScheme.onPrimary.withOpacity(0.1),
              spreadRadius: 4,
              blurRadius: 15,
              offset: Offset(0, 4),
            ),
          ],
        ),
        child: Row(
          children: [
            SizedBox(width: 13),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: getProportionateScreenHeight(6)),
                Text(
                  "Total",
                  style: thirdTextStyle.copyWith(
                    fontSize: getProportionateScreenHeight(12),
                    fontWeight: semiBold,
                  ),
                ),
                SizedBox(height: getProportionateScreenHeight(2)),
                Text(
                  'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(Provider.of<provOrder.OrderProvider>(context).totalPrice as String == "0" ? (widget.discountHarga! < 50000 ? widget.discountHarga! + 5000 : widget.discountHarga) : (int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice!) == widget.discountHarga ? (int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice!) < 50000 ? int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice!) + 5000 : int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice as String)) : int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice as String)))}',
                  style: primaryTextStyle.copyWith(
                    fontSize: SizeConfig.blockVertical! * 2.5,
                    fontWeight: bold,
                  ),
                ),
                SizedBox(height: getProportionateScreenHeight(2)),
              ],
            ),
            Spacer(),
            GestureDetector(
            onTap: () async {
              String orderId = 'order-${DateTime.now().millisecondsSinceEpoch}';
              int grossAmount = Provider.of<provOrder.OrderProvider>(context, listen: false)
                      .orders
                      .fold(0, (sum, order) {
                    // Gunakan harga diskon jika tersedia, jika tidak ada gunakan harga asli
                    int harga = (order.discountPrice != null && order.discountPrice != "0") 
                        ? int.parse(order.discountPrice) 
                        : int.parse(order.price);
                    
                    // Tambahkan biaya admin jika harga di bawah 50.000
                    return sum + (harga < 50000 ? harga + 5000 : harga);
                  });

              // Persiapkan data_invoice berdasarkan kursus yang ada di keranjang
              List<Map<String, dynamic>> dataInvoice = [];
              Provider.of<provOrder.OrderProvider>(context, listen: false).orders.forEach((order) {
                dataInvoice.add({
                  'id_kursus': order.idCourse.toString(),
                  'title_kursus': order.title,
                  // Gunakan harga diskon jika tersedia, jika tidak gunakan harga asli
                  'harga': (order.discountPrice != null && order.discountPrice != "0")
                      ? order.discountPrice.toString()
                      : order.price.toString(),
                  'qty': '1',
                });
              });

              // Panggil Snap melalui provider
              Map<String, String>? paymentResponse = await paymentProvider.startSnapPayment(
                orderId: orderId,
                grossAmount: grossAmount,
                dataInvoice: dataInvoice,
              );

              if (paymentResponse != null) {
                // Snap berhasil dibuka, navigasi ke SnapPaymentPage untuk menampilkan WebView
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => SnapPaymentPage(
                      transactionToken: paymentResponse['transactionToken'] ?? '',
                      orderId: orderId, 
                      grossAmount: grossAmount, courseTitle: '', courseThumbnail: '', courseInstructor: '', courseId: '',
                    ),
                  ),
                );
              } else {
                print("Pembayaran gagal");
              }
            },

              child: Container(
                margin: EdgeInsets.only(right: getProportionateScreenWidth(15)),
                width: getProportionateScreenWidth(135),
                height: getProportionateScreenHeight(35),
                decoration: BoxDecoration(
                    color: themeProvider.themeData == ThemeClass.darkmode
                              ?primaryColor : primaryColorligtmode,
                    borderRadius: BorderRadius.circular(10)),
                child: Center(
                  child: Text(
                    'Pilih Pembayaran',
                    style: thirdTextStyle.copyWith(
                      fontSize: SizeConfig.blockVertical! * 1.7,
                      fontWeight: semiBold,
                      letterSpacing: 0.5,
                      color: baruTextutih,
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      );
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.background,
        centerTitle: true,
        title: Text(
          'Checkout',
          style: thirdTextStyle.copyWith(
              letterSpacing: 1,
              fontWeight: semiBold,
              fontSize: getProportionateScreenWidth(14)),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(16)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'Detail Pesanan',
                style: thirdTextStyle.copyWith(
                  letterSpacing: 1,
                  fontWeight: semiBold,
                  fontSize: getProportionateScreenWidth(14),
                ),
              ),
              SizedBox(height: getProportionateScreenHeight(20)),
              Consumer<provOrder.OrderProvider>(
                builder: (context, state, _) {
                  return Column(
                    children: [
                      ...state.orders.map(
                        (order) => widget.isDetailCourse != null
                            ? CourseList(
                                idCourse: order.idCourse,
                                title: order.title,
                                price: widget.isCart == null
                                    ? order.price
                                    : (order.discountPrice != null
                                        ? order.discountPrice
                                        : order.price),
                                discountPrice: order.discountPrice == null
                                    ? order.discountPrice
                                    : order.price,
                                imageUrl: order.imageUrl,
                                isDetailCourse: true,
                              )
                            : CourseList(
                                idCourse: order.idCourse,
                                title: order.title,
                                price: widget.isCart == null
                                    ? order.price
                                    : (order.discountPrice != null
                                        ? order.discountPrice
                                        : order.price),
                                discountPrice: order.discountPrice == null
                                    ? order.discountPrice
                                    : order.price,
                                imageUrl: order.imageUrl,
                              ),
                      ),
                    ],
                  );
                },
              ),
              SizedBox(height: getProportionateScreenHeight(5)),
              Text(
                'Rincian Harga',
                style: thirdTextStyle.copyWith(
                  letterSpacing: 1,
                  fontWeight: semiBold,
                  fontSize: getProportionateScreenWidth(14),
                ),
              ),
              SizedBox(height: getProportionateScreenHeight(10)),
              Row(
                children: [
                  Text(
                    'Subtotal Harga Kursus',
                    style: thirdTextStyle.copyWith(
                      fontWeight: reguler,
                      fontSize: getProportionateScreenWidth(10),
                    ),
                  ),
                  Spacer(),
                  RichText(
                    text: TextSpan(
                      children: [
                        if (widget.discountHarga.toString() != subTotal)
                          TextSpan(
                            text:
                                'Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(int.parse(subTotal!))}',
                            style: thirdTextStyle.copyWith(
                              decoration: TextDecoration.lineThrough,
                              color: fourthColor,
                              fontWeight: reguler,
                              fontSize: SizeConfig.blockHorizontal! * 2.8,
                            ),
                          ),
                        WidgetSpan(
                            child: SizedBox(
                                width: getProportionateScreenWidth(8))),
                        TextSpan(
                          text:
                              'Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(Provider.of<provOrder.OrderProvider>(context).totalPrice as String == "0" ? (widget.discountHarga! < 50000 ? widget.discountHarga! + 5000 : widget.discountHarga) : int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice as String))}',
                          style: thirdTextStyle.copyWith(
                            color: Theme.of(context).colorScheme.onPrimary,
                            fontWeight: reguler,
                            fontSize: getProportionateScreenWidth(12),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
      bottomNavigationBar: bottomNav(
        (int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice as String)).toString(),
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
