import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.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/insurance/insurance_view_model.dart';
import 'package:hmg_patient_app_new/features/insurance/models/resp_models/patient_insurance_approval_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/insurance/widgets/insurance_approval_card.dart';
import 'package:hmg_patient_app_new/presentation/insurance/insurance_approval_details_page.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

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

  @override
  State<InsuranceApprovalsPage> createState() => _InsuranceApprovalsPageState();
}

class _InsuranceApprovalsPageState extends State<InsuranceApprovalsPage> {
  late InsuranceViewModel insuranceViewModel;
  late AppState appState;

  @override
  void initState() {
    scheduleMicrotask(() {
      insuranceViewModel.setIsInsuranceApprovalsLoading(true);
      insuranceViewModel.getPatientInsuranceApprovalsList();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    insuranceViewModel = Provider.of<InsuranceViewModel>(context, listen: false);
    return CollapsingListView(
      title: "${LocaleKeys.insurance.tr(context: context)} ${LocaleKeys.approvals.tr(context: context)}",
      child: SingleChildScrollView(
        child: Consumer<InsuranceViewModel>(builder: (context, insuranceVM, child) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ListView.separated(
                padding: EdgeInsets.only(top: 12.h),
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemCount: insuranceVM.isInsuranceApprovalsLoading
                    ? 5
                    : insuranceVM.patientInsuranceApprovalsList.isNotEmpty
                        ? insuranceVM.patientInsuranceApprovalsList.length
                        : 1,
                itemBuilder: (context, index) {
                  return insuranceVM.isInsuranceApprovalsLoading
                      ? Container(
                          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                          child: InsuranceApprovalCard(
                            insuranceApprovalResponseModel: InsuranceApprovalResponseModel(),
                            appState: appState,
                            isLoading: true,
                          ),
                        ).paddingSymmetrical(24.h, 0.h)
                      : insuranceVM.patientInsuranceApprovalsList.isNotEmpty
                          ? AnimationConfiguration.staggeredList(
                              position: index,
                              duration: const Duration(milliseconds: 500),
                              child: SlideAnimation(
                                verticalOffset: 100.0,
                                child: FadeInAnimation(
                                  child: AnimatedContainer(
                                    duration: Duration(milliseconds: 300),
                                    curve: Curves.easeInOut,
                                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                                    child: InsuranceApprovalCard(
                                      insuranceApprovalResponseModel: insuranceVM.patientInsuranceApprovalsList[index],
                                      appState: appState,
                                      isLoading: false,
                                    ).onPress(() {
                                      Navigator.of(context).push(
                                        CustomPageRoute(
                                          page: InsuranceApprovalDetailsPage(insuranceApprovalResponseModel: insuranceVM.patientInsuranceApprovalsList[index]),
                                        ),
                                      );
                                    }),
                                  ).paddingSymmetrical(24.h, 0.h),
                                ),
                              ),
                            )
                          : Utils.getNoDataWidget(context, noDataText: LocaleKeys.noInsuranceApprovals.tr(context: context));
                },
                separatorBuilder: (BuildContext cxt, int index) => SizedBox(height: 16.h),
              ),
              SizedBox(height: 24.h),
            ],
          );
        }),
      ),
    );
  }
}
