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/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/medical_file/medical_file_view_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/patient_sickleave_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/lab/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:provider/provider.dart';

import 'widgets/patient_sick_leave_card.dart';

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

  @override
  State<PatientSickleavesListPage> createState() => _PatientSickleavesListPageState();
}

class _PatientSickleavesListPageState extends State<PatientSickleavesListPage> {
  late MedicalFileViewModel medicalFileViewModel;

  @override
  void initState() {
    scheduleMicrotask(() {
      medicalFileViewModel.setIsPatientSickLeaveListLoading(true);
      medicalFileViewModel.getPatientSickLeaveList(onError: (error) {
        Navigator.of(context).pop();
        Navigator.of(context).pop();
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    medicalFileViewModel = Provider.of<MedicalFileViewModel>(context, listen: false);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: "${LocaleKeys.sick.tr(context: context)} ${LocaleKeys.sickSubtitle.tr(context: context)}",
        child: SingleChildScrollView(
          child: Consumer<MedicalFileViewModel>(builder: (context, medicalFileVM, child) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ListView.separated(
                  scrollDirection: Axis.vertical,
                  itemCount: medicalFileVM.isPatientSickLeaveListLoading ? 3 : medicalFileVM.patientSickLeaveList.length,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    return medicalFileVM.isPatientSickLeaveListLoading
                        ? PatientSickLeaveCard(
                            patientSickLeavesResponseModel: PatientSickLeavesResponseModel(),
                            isLoading: true,
                          ).paddingSymmetrical(24.h, 0.0)
                        : medicalFileVM.patientSickLeaveList.isNotEmpty
                            ? AnimationConfiguration.staggeredList(
                                position: index,
                                duration: const Duration(milliseconds: 1000),
                                child: SlideAnimation(
                                  verticalOffset: 100.0,
                                  child: FadeInAnimation(
                                    child: PatientSickLeaveCard(
                                      patientSickLeavesResponseModel: medicalFileVM.patientSickLeaveList.first,
                                      isLoading: false,
                                    ).paddingSymmetrical(24.h, 0.0),
                                  ),
                                ),
                              )
                            : SizedBox.shrink();
                  },
                  separatorBuilder: (BuildContext cxt, int index) => SizedBox(height: 8.h),
                ),
                SizedBox(height: 60.h),
              ],
            );
          }),
        ),
      ),
    );
  }
}
