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/date_util.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/features/insurance/insurance_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/insurance/widgets/insurance_update_details_card.dart';
import 'package:hmg_patient_app_new/presentation/insurance/widgets/patient_insurance_card.dart';
import 'package:hmg_patient_app_new/presentation/lab/lab_result_item_view.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/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:provider/provider.dart';

import 'widgets/insurance_history.dart';

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

  @override
  State<InsuranceHomePage> createState() => _InsuranceHomePageState();
}

class _InsuranceHomePageState extends State<InsuranceHomePage> {
  late InsuranceViewModel insuranceViewModel;
  late AppState appState;

  @override
  void initState() {
    scheduleMicrotask(() {
      insuranceViewModel.initInsuranceProvider();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    insuranceViewModel = Provider.of<InsuranceViewModel>(context, listen: false);
    return CollapsingListView(
      title: "${LocaleKeys.updateInsurance.tr(context: context)} ${LocaleKeys.insurance.tr(context: context)}",
      // history: () {
      //   // insuranceViewModel.setIsInsuranceHistoryLoading(true);
      //   // insuranceViewModel.getPatientInsuranceCardHistory();
      //   showCommonBottomSheetWithoutHeight(context, child: InsuranceHistory(), callBackFunc: () {}, title: "", isCloseButtonVisible: false, isFullScreen: false);
      // },
      child: SingleChildScrollView(
        child: Consumer<InsuranceViewModel>(builder: (context, insuranceVM, child) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              insuranceVM.isInsuranceLoading
                  ? LabResultItemView(
                      onTap: () {},
                      labOrder: null,
                      index: 0,
                      isLoading: true,
                    ).paddingSymmetrical(24.h, 24.h)
                  : insuranceVM.patientInsuranceList.isNotEmpty
                      ? ListView.builder(
                          shrinkWrap: true,
                          padding: EdgeInsets.all(16.h),
                          physics: const BouncingScrollPhysics(),
                          itemBuilder: (context, index) {
                            return Column(
                              children: [
                                PatientInsuranceCard(
                                  isShowButtons: index == 0,
                                    insuranceCardDetailsModel: insuranceVM.patientInsuranceList[index],
                                    isInsuranceExpired: DateTime.now().isAfter(DateUtil.convertStringToDate(insuranceVM.patientInsuranceList.first.cardValidTo))),
                                SizedBox(
                                  height: 12.h,
                                )
                              ],
                            );
                          },
                          itemCount: insuranceVM.patientInsuranceList.length,
                        )
                      : Padding(
                          padding: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.12),
                          child: Utils.getNoDataWidget(
                            context,
                            noDataText: LocaleKeys.noInsuranceWithHMG.tr(context: context),
                            callToActionButton: CustomButton(
                              icon: AppAssets.update_insurance_card_icon,
                              iconColor: AppColors.successColor,
                              iconSize: 15.h,
                              text: "${LocaleKeys.updateInsurance.tr(context: context)} ${LocaleKeys.updateInsuranceSubtitle.tr(context: context)}",
                              onPressed: () {
                                insuranceViewModel.setIsInsuranceUpdateDetailsLoading(true);
                                insuranceViewModel.getPatientInsuranceDetailsForUpdate(
                                    appState.getAuthenticatedUser()!.patientId.toString(), appState.getAuthenticatedUser()!.patientIdentificationNo.toString());
                                showCommonBottomSheetWithoutHeight(context, child: PatientInsuranceCardUpdateCard(), callBackFunc: () {}, title: "", isCloseButtonVisible: false, isFullScreen: false);
                              },
                              backgroundColor: AppColors.bgGreenColor.withOpacity(0.20),
                              borderColor: AppColors.bgGreenColor.withOpacity(0.0),
                              textColor: AppColors.bgGreenColor,
                              fontSize: 14,
                              isBold: true,
                              borderRadius: 12,
                              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                              height: 40.h,
                            ).paddingSymmetrical(64.h, 0.h),
                          ),
                        ),
            ],
          );
        }),
      ),
    );
  }
}
