import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/PCWalletProvider.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:provider/provider.dart';

import '../../Models/PaymentCollectionWalletResponse.dart';
import '../../Utils/app_colors.dart';

class Pcwallet extends StatefulWidget {
  const Pcwallet({super.key});

  @override
  State<Pcwallet> createState() => _PcwalletState();
}

class _PcwalletState extends State<Pcwallet> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var provider = Provider.of<Pcwalletprovider>(context, listen: false);
      provider.LoadTransactionsListAPI(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Pcwalletprovider>(
      builder: (context, provider, child) {
        final groupedData = provider.groupByDate(provider.historyList);
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: AppBar(
              automaticallyImplyLeading: false,
              elevation: 2.0,
              title: SizedBox(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    InkResponse(
                      onTap: () => Navigator.pop(context, true),
                      child: SvgPicture.asset(
                        "assets/svg/app_bar_back.svg",
                        height: 25,
                      ),
                    ),
                    InkResponse(
                      onTap: () => Navigator.pop(context,true),
                      child: Text(
                        "P.C. Wallet",
                        style: TextStyle(
                          fontSize: 16,
                          height: 1.1,
                          fontFamily: "JakartaSemiBold",
                          color: AppColors.semi_black,
                        ),
                      ),
                    ),
                    Spacer(),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 10),
                      height: 40,
                      decoration: BoxDecoration(
                        color: Color(0xFFF2FAFF),
                        borderRadius: BorderRadius.circular(16),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          SvgPicture.asset(
                            'assets/svg/pc_wallet.svg',
                            height: 30,
                          ),
                          SizedBox(width: 5),
                          Text(
                            "${provider.totlDetails.balanceAmount??"0.00"}",
                            style: TextStyle(fontSize: 14),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.vertical(
                  bottom: Radius.circular(30), // Adjust the radius as needed
                ),
              ),
            ),
            backgroundColor: AppColors.scaffold_bg_color,
            body: SafeArea(
              child: Container(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16),
                        ),
                        child: Row(
                          children: [
                            Expanded(
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 15,
                                ),
                                decoration: BoxDecoration(
                                  color: Color(0xFFE7FFE5),
                                  borderRadius: BorderRadius.circular(16),
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      crossAxisAlignment: CrossAxisAlignment.center,
                                      children: [
                                        Text(
                                          "₹",
                                          style: TextStyle(
                                            height: 1,
                                            color: Color(0xFF0D9C00),
                                          ),
                                        ),
                                        Text(
                                          "${provider.totlDetails.creditAmount??"0.00"}",
                                          style: TextStyle(
                                            fontSize: 30,
                                            height: 1,
                                            color: Color(0xFF0D9C00),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Text("Credited",style: TextStyle(
                                      fontFamily: "JakartaMedium"
                                    ),),
                                  ],
                                ),
                              ),
                            ),
                            SizedBox(width: 10),
                            Expanded(
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 15,
                                ),
                                decoration: BoxDecoration(
                                  color: Color(0xFFFFEFEF),
                                  borderRadius: BorderRadius.circular(16),
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      crossAxisAlignment: CrossAxisAlignment.center,
                                      children: [
                                        Text(
                                          "₹",
                                          style: TextStyle(
                                            height: 1,
                                            color: Color(0xFFED3424),
                                          ),
                                        ),
                                        Text(
                                          "${provider.totlDetails.debitAmount??0.00}",
                                          style: TextStyle(
                                            fontSize: 30,
                                            height: 1,
                                            color: Color(0xFFED3424),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Text("Debited",style: TextStyle(
                                        fontFamily: "JakartaMedium"
                                    )),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: groupedData.keys.length,
                        itemBuilder: (context, index) {
                          String date = groupedData.keys.elementAt(index);
                          List<HistoryList> items = groupedData[date]!;
              
                          return Container(
                            padding: EdgeInsets.symmetric(vertical: 5),
                            margin: EdgeInsets.symmetric(horizontal: 10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  padding: EdgeInsets.only(bottom: 5),
                                  child: Text(
                                    date,
                                    style: TextStyle(
                                      fontSize: 14,
                                        fontFamily: "JakartaMedium",
                                      color: Color(0xFF818181),
                                    ),
                                  ),
                                ),
                                ...items.map((item) {
                                  String part1 =
                                      item.description!.split(' (').first;
              
                                  RegExp regExp = RegExp(r'\(([^)]+)\)');
                                  String? part2 = regExp
                                      .firstMatch(item.description!)
                                      ?.group(1);
              
                                  return Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 10,
                                      vertical: 10,
                                    ),
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(16),
                                    ),
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.start,
                                      children: [
                                        Expanded(
                                          flex:1,
                                          child: SizedBox(
                                          child: item.transactionType == "Credit"
                                              ? Image.asset(
                                                "assets/images/trans_credit.png",
                                            height: 45,
                                            width: 45,
                                             fit: BoxFit.contain,
                                              )
                                              : Image.asset(
                                                "assets/images/trans_debit.png",
                                            height: 45,
                                            width: 45,
                                            fit: BoxFit.contain,
                                              ),
                                          ),
                                        ),
                                        SizedBox(width: 10,),
                                        Expanded(
                                          flex:5,
                                          child: SizedBox(
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: [
                                                Text(part1,style: TextStyle(
                                                    fontFamily: "JakartaMedium"
                                                ),),
                                                Text(
                                                  "${part2}",
                                                  style: TextStyle(
                                                    color: Color(0xFF818181),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                        Spacer(),
                                        Expanded(
                                          flex: 2,
                                          child: SizedBox(
                                            child: RichText(
                                              text: TextSpan(
                                                children: [
                                                  TextSpan(
                                                    text: item.transactionType ==
                                                        "Credit"
                                                        ? "+":"-",
                                                    style: TextStyle(
                                                      color: Color(0xFF2D2D2D),
                                                      fontSize: 14,
                                                      fontFamily: "JakartaRegular",
                                                    ),
                                                  ),
                                                  TextSpan(
                                                    text: "₹${item.amount!}",
                                                    style: TextStyle(
                                                      color: Color(0xFF2D2D2D),
                                                      fontSize: 14,
                                                      fontFamily: "JakartaMedium",
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  );
                                }).toList(),
                              ],
                            ),
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
