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_assets.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/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/widgets/ask_doctor_appointment_card.dart';
import 'package:hmg_patient_app_new/presentation/ask_doctor/widgets/doctor_response_transactions.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/chip/app_custom_chip_widget.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:provider/provider.dart';

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

  @override
  State<DoctorResponsePage> createState() => _DoctorResponsePageState();
}

class _DoctorResponsePageState extends State<DoctorResponsePage> {
  late MyAppointmentsViewModel myAppointmentsViewModel;

  @override
  Widget build(BuildContext context) {
    myAppointmentsViewModel = Provider.of<MyAppointmentsViewModel>(context, listen: false);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: LocaleKeys.doctorResponses.tr(context: context),
        child: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(height: 16.h),
              Consumer<AskDoctorViewModel>(builder: (context, askDoctorVM, child) {
                return ListView.builder(
                  padding: EdgeInsets.zero,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: askDoctorVM.isDoctorResponsesLoading
                      ? 4
                      : askDoctorVM.doctorResponsesList.isNotEmpty
                          ? askDoctorVM.doctorResponsesList.length
                          : 1,
                  itemBuilder: (context, index) {
                    return askDoctorVM.isDoctorResponsesLoading
                        ? Container(
                            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                            child: PatientSickLeaveCard(
                              patientSickLeavesResponseModel: PatientSickLeavesResponseModel(),
                              isLoading: true,
                            )).paddingSymmetrical(0.w, 12.h)
                        : askDoctorVM.doctorResponsesList.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: Padding(
                                        padding: EdgeInsets.all(16.w),
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            (askDoctorVM.doctorResponsesList[index].requestTypeDescription ?? "").toText18(isBold: true),
                                            SizedBox(height: 24.h),
                                            Row(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: [
                                                Column(
                                                  crossAxisAlignment: CrossAxisAlignment.center,
                                                  children: [
                                                    Utils.buildImgWithNetwork(
                                                      url: "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown.png",
                                                      iconColor: AppColors.blackBgColor,
                                                      width: 50.w,
                                                      height: 50.h,
                                                      fit: BoxFit.cover,
                                                    ).circle(100.r),
                                                  ],
                                                ),
                                                SizedBox(width: 16.h),
                                                Expanded(
                                                  child: Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    children: [
                                                      askDoctorVM.doctorResponsesList[index].doctorName!.toText16(isBold: true, maxlines: 1),
                                                      SizedBox(height: 8.h),
                                                      Wrap(
                                                        direction: Axis.horizontal,
                                                        spacing: 3.h,
                                                        runSpacing: 4.h,
                                                        children: [
                                                          AppCustomChipWidget(
                                                            labelText: (askDoctorVM.doctorResponsesList[index].clinicName ?? "").length > 15
                                                                ? '${askDoctorVM.doctorResponsesList[index].clinicName!.substring(0, 12)}...'
                                                                : askDoctorVM.doctorResponsesList[index].clinicName ?? "",
                                                          ),
                                                          AppCustomChipWidget(
                                                              labelText: (askDoctorVM.doctorResponsesList[index].projectName ?? "").length > 15
                                                                  ? '${askDoctorVM.doctorResponsesList[index].projectName!.substring(0, 12)}...'
                                                                  : askDoctorVM.doctorResponsesList[index].projectName ?? ""),
                                                          AppCustomChipWidget(
                                                            labelPadding: EdgeInsetsDirectional.only(start: -4.w, end: 6.w),
                                                            icon: AppAssets.appointment_calendar_icon,
                                                            labelText: DateUtil.formatDateToDate(DateUtil.convertStringToDate(askDoctorVM.doctorResponsesList[index].createdOn), false),
                                                          ),
                                                        ],
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ],
                                        ),
                                      ),
                                    ).onPress(() {
                                      showCommonBottomSheetWithoutHeight(
                                        context,
                                        title: askDoctorVM.doctorResponsesList[index].requestTypeDescription ?? "",
                                        child: DoctorResponseTransactions(doctorResponseModel: askDoctorVM.doctorResponsesList[index],),
                                        callBackFunc: () {},
                                        isFullScreen: false,
                                        isCloseButtonVisible: true,
                                      );
                                    }),
                                  ),
                                ),
                              )
                            : Utils.getNoDataWidget(context, noDataText: LocaleKeys.noDataAvailable.tr(context: context));
                  },
                ).paddingSymmetrical(24.h, 0.h);
              }),
              SizedBox(height: 24.h),
            ],
          ),
        ),
      ),
    );
  }
}
