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 Scaffold(
          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,
                    ),
                  ),
                  Text(
                    "P.C. Wallet",
                    style: TextStyle(
                      fontSize: 16,
                      height: 1.1,
                      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}",
                          style: TextStyle(fontSize: 14),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.vertical(
                bottom: Radius.circular(30), // Adjust the radius as needed
              ),
            ),
          ),
          backgroundColor: AppColors.scaffold_bg_color,
          body: 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}",
                                      style: TextStyle(
                                        fontSize: 30,
                                        height: 1,
                                        color: Color(0xFF0D9C00),
                                      ),
                                    ),
                                  ],
                                ),
                                Text("Credited"),
                              ],
                            ),
                          ),
                        ),
                        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}",
                                      style: TextStyle(
                                        fontSize: 30,
                                        height: 1,
                                        color: Color(0xFFED3424),
                                      ),
                                    ),
                                  ],
                                ),
                                Text("Debited"),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  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,
                                  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.spaceBetween,
                                  children: [
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Text(part1),
                                        Text(
                                          "${part2}",
                                          style: TextStyle(
                                            color: Color(0xFF818181),
                                          ),
                                        ),
                                      ],
                                    ),

                                    RichText(
                                      text: TextSpan(
                                        children: [
                                          TextSpan(
                                            text:
                                                item.transactionType == "Credit"
                                                    ? "+"
                                                    : "-",
                                            style: TextStyle(
                                              color:
                                                  item.transactionType ==
                                                          "Credit"
                                                      ? Color(0xFF0D9C00)
                                                      : Color(0xFFED3424),
                                              fontSize: 16,
                                              fontFamily: "Lexend",
                                            ),
                                          ),
                                          TextSpan(
                                            text: "₹${item.amount!}",
                                            style: TextStyle(
                                              color:
                                                  item.transactionType ==
                                                          "Credit"
                                                      ? Color(0xFF0D9C00)
                                                      : Color(0xFFED3424),
                                              fontSize: 16,
                                              fontFamily: "Lexend",
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }).toList(),
                          ],
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
