import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/habib_wallet/habib_wallet_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/habib_wallet/recharge_wallet_page.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

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

  @override
  State<HabibWalletPage> createState() => _HabibWalletState();
}

class _HabibWalletState extends State<HabibWalletPage> {
  late HabibWalletViewModel habibWalletVM;

  @override
  void initState() {
    scheduleMicrotask(() async {
      habibWalletVM.initHabibWalletProvider();
      habibWalletVM.getPatientBalanceAmount();
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    habibWalletVM = Provider.of<HabibWalletViewModel>(context, listen: false);
    AppState _appState = getIt.get<AppState>();
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: LocaleKeys.myWallet.tr(),
        child: Padding(
          padding: EdgeInsets.all(24.h),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  width: double.infinity,
                  height: 180.h,
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                    // color: AppColors.blackBgColor,
                    color: Color(0xFF2E3039),
                    borderRadius: 24.r,
                    hasShadow: true
                  ),
                  child: Padding(
                    padding: EdgeInsets.all(16.h),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                "${_appState.getAuthenticatedUser()!.firstName!} ${_appState.getAuthenticatedUser()!.lastName!}".toText19(isBold: true, color: Colors.white),
                                "${LocaleKeys.fileno.tr(context: context)}: ${_appState.getAuthenticatedUser()!.patientId!}"
                                    .toText14(isBold: true, color: AppColors.greyTextColor, isEnglishOnly: true),
                              ],
                            ).expanded,
                            Utils.buildSvgWithAssets(icon: AppAssets.habiblogo, width: 24.h, height: 24.h, applyThemeColor: false),
                          ],
                        ),
                        Spacer(),
                        LocaleKeys.balanceAmount.tr(context: context).toText14(isBold: true, color: Colors.white),
                        SizedBox(height: 4.h),
                        Consumer<HabibWalletViewModel>(builder: (context, habibWalletVM, child) {
                          return Utils.getPaymentAmountWithSymbol2(
                                  habibWalletVM.habibWalletAmount,
                                  textColor: Colors.white,
                                  iconColor: Colors.white,
                                  iconSize: 16,
                                  isExpanded: false,
                                  letterSpacing: -1)
                              .toShimmer2(isShow: habibWalletVM.isWalletAmountLoading, radius: 12.h, width: 80.h, height: 24.h);
                        }),
                      ],
                    ),
                  ),
                ),
                SizedBox(height: 16.h),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Flexible(
                      child: CustomButton(
                        height: 40.h,
                        icon: AppAssets.recharge_icon,
                        iconSize: 24.w,
                        backgroundColor: AppColors.infoColor,
                        textColor: Colors.white,
                        text: LocaleKeys.recharge.tr(context: context),
                        borderWidth: 0.w,
                        fontWeight: FontWeight.w600,
                        borderColor: Colors.transparent,
                        padding: EdgeInsets.fromLTRB(4, 0, 12, 0),
                        fontSize: 14.f,
                        onPressed: () {
                          Navigator.of(context)
                              .push(CustomPageRoute(
                            page: RechargeWalletPage(),
                          ))
                              .then((val) {
                            habibWalletVM.initHabibWalletProvider();
                            habibWalletVM.getPatientBalanceAmount();
                          });
                        },
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 16.h),
                Container(
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                    color: AppColors.whiteColor,
                    borderRadius: 24.r,
                    hasShadow: false,
                  ),
                  child: Consumer<HabibWalletViewModel>(builder: (context, habibWalletVM, child) {
                    return ListView.separated(
                      itemCount: habibWalletVM.habibWalletBalanceList.length,
                      physics: NeverScrollableScrollPhysics(),
                      padding: EdgeInsets.only(top: 8, bottom: 8),
                      shrinkWrap: true,
                      itemBuilder: (context, index) {
                        return Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(child: habibWalletVM.habibWalletBalanceList[index].projectDescription!.toText16(isBold: true, color: AppColors.textColor)),
                            Utils.getPaymentAmountWithSymbol2(habibWalletVM.habibWalletBalanceList[index].patientAdvanceBalanceAmount!, //did the changes because of negative value
                                textColor: AppColors.textColor, iconColor: AppColors.textColor, iconSize: 18.h, isExpanded: false, fontSize: 28.f, fontWeight: FontWeight.w600, letterSpacing: -1),
                          ],
                        ).paddingSymmetrical(0, 12.h);
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return Divider(height: 1, color: AppColors.textColor.withAlpha(20));
                      },
                    ).paddingSymmetrical(16.h, 24.h).toShimmer2(isShow: habibWalletVM.isWalletAmountLoading);
                  }),
                ),
                SizedBox(height: 24.h),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
