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/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/ask_doctor/ask_doctor_view_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/patient_sickleave_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/appointments/widgets/appointment_card.dart';
import 'package:hmg_patient_app_new/presentation/ask_doctor/doctor_response_page.dart';
import 'package:hmg_patient_app_new/presentation/ask_doctor/widgets/ask_doctor_appointment_card.dart';
import 'package:hmg_patient_app_new/presentation/medical_file/widgets/patient_sick_leave_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/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

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

  @override
  State<AskDoctorPage> createState() => _AskDoctorPageState();
}

class _AskDoctorPageState extends State<AskDoctorPage> {
  late MyAppointmentsViewModel myAppointmentsViewModel;

  @override
  Widget build(BuildContext context) {
    myAppointmentsViewModel = Provider.of<MyAppointmentsViewModel>(context, listen: false);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: Consumer<AskDoctorViewModel>(
        builder: (context, askDoctorVM, child) {
          return CollapsingListView(
            title: LocaleKeys.askDoctor.tr(context: context),
            doctorResponse: () {
              askDoctorVM.getDoctorResponses();
              Navigator.of(context).push(
                CustomPageRoute(
                  page: DoctorResponsePage(),
                ),
              );
            },
            child: SingleChildScrollView(
              child: Column(children: [
                SizedBox(height: 16.h),
                ListView.builder(
                  padding: EdgeInsets.only(top: 8.h),
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: askDoctorVM.isAskDoctorAppointmentsLoading
                      ? 4
                      : askDoctorVM.askDoctorAppointmentsList.isNotEmpty
                          ? askDoctorVM.askDoctorAppointmentsList.length
                          : 1,
                  itemBuilder: (context, index) {
                    return askDoctorVM.isAskDoctorAppointmentsLoading
                        ? Container(
                            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                            child: PatientSickLeaveCard(
                              patientSickLeavesResponseModel: PatientSickLeavesResponseModel(),
                              isLoading: true,
                            )).paddingSymmetrical(0.w, 12.h)
                        : askDoctorVM.askDoctorAppointmentsList.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,
                                      margin: EdgeInsets.symmetric(vertical: 8.h),
                                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 20.h, hasShadow: true),
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          AskDoctorAppointmentCard(
                                            askDoctorAppointmentHistoryList: askDoctorVM.askDoctorAppointmentsList[index],
                                            myAppointmentsViewModel: myAppointmentsViewModel,
                                          )
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              )
                            : Utils.getNoDataWidget(context, noDataText: LocaleKeys.noAppointmentsYet.tr(context: context));
                  },
                ).paddingSymmetrical(24.h, 0.h),
                SizedBox(height: 24.h),
              ]),
            ),
          );
        },
      ),
    );
  }
}
