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/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/dental_chief_complaints_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/select_doctor_page.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/widgets/chief_complaint_card.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 DentalChiefComplaintsPage extends StatefulWidget {
  const DentalChiefComplaintsPage({super.key});

  @override
  State<DentalChiefComplaintsPage> createState() => _DentalChiefComplaintsPageState();
}

class _DentalChiefComplaintsPageState extends State<DentalChiefComplaintsPage> {
  late AppState appState;
  late BookAppointmentsViewModel bookAppointmentsViewModel;

  @override
  void initState() {
    scheduleMicrotask(() {
      bookAppointmentsViewModel.getDentalChiefComplaintsList();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    bookAppointmentsViewModel = Provider.of<BookAppointmentsViewModel>(context, listen: false);
    appState = getIt.get<AppState>();
    return CollapsingListView(
      title: LocaleKeys.dentalChiefComplaints.tr(),
      child: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 24.h),
          child: Consumer<BookAppointmentsViewModel>(builder: (context, bookAppointmentsVM, child) {
            return ListView.separated(
              padding: EdgeInsets.only(top: 24.h),
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: bookAppointmentsVM.isChiefComplaintsListLoading ? 5 : bookAppointmentsVM.dentalChiefComplaintsList.length,
              itemBuilder: (context, index) {
                return bookAppointmentsVM.isChiefComplaintsListLoading
                    ? ChiefComplaintCard(
                        bookAppointmentsVM: bookAppointmentsVM,
                        dentalChiefComplaintsListResponseModel: DentalChiefComplaintsListResponseModel(),
                        isLoading: bookAppointmentsVM.isChiefComplaintsListLoading,
                      )
                    : 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: ChiefComplaintCard(
                                bookAppointmentsVM: bookAppointmentsVM,
                                dentalChiefComplaintsListResponseModel: bookAppointmentsVM.dentalChiefComplaintsList[index],
                                isLoading: bookAppointmentsVM.isChiefComplaintsListLoading,
                              ).onPress(() {
                                bookAppointmentsVM.setSelectedChiefComplaintID(bookAppointmentsVM.dentalChiefComplaintsList[index].iD!);
                                bookAppointmentsViewModel.setIsDoctorsListLoading(true);
                                Navigator.of(context).push(
                                  CustomPageRoute(
                                    page: SelectDoctorPage(),
                                  ),
                                );
                              }),
                            ),
                          ),
                        ),
                      );
              },
              separatorBuilder: (BuildContext cxt, int index) => SizedBox(height: 16.h),
            );
          }),
        ),
      ),
    );
  }
}
