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_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/medical_file/medical_file_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:provider/provider.dart';

import '../../widgets/chip/app_custom_chip_widget.dart';

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

  @override
  State<VaccineListPage> createState() => _VaccineListPageState();
}

class _VaccineListPageState extends State<VaccineListPage> {
  late MedicalFileViewModel medicalFileViewModel;

  @override
  void initState() {
    super.initState();
    scheduleMicrotask(() {
      medicalFileViewModel = Provider.of<MedicalFileViewModel>(context, listen: false);
      medicalFileViewModel.setIsPatientVaccineListLoading(true);
      medicalFileViewModel.getPatientVaccinesList(
        onSuccess: (data) {
         // print("✅ Vaccine data received: ${medicalFileViewModel.patientVaccineList.length} items");
        },
        onError: (error) {
          //print("❌ Vaccine error: $error");
        },
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    medicalFileViewModel = Provider.of<MedicalFileViewModel>(context, listen: false);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: LocaleKeys.vaccineInfomation.tr(context: context),
        child: SingleChildScrollView(
          child: Consumer<MedicalFileViewModel>(builder: (context, medicalFileVM, child) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 16.h),
                ListView.separated(
                  scrollDirection: Axis.vertical,
                  itemCount: medicalFileVM.isPatientVaccineListLoading
                      ? 5
                      : medicalFileVM.patientVaccineList.isNotEmpty
                          ? medicalFileVM.patientVaccineList.length
                          : 1,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.only(left: 24.h, right: 24.h),
                  itemBuilder: (context, index) {
                    return medicalFileVM.isPatientVaccineListLoading
                        ? Container(
                            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                              color: AppColors.whiteColor,
                              borderRadius: 20.h,
                              hasShadow: true,
                            ),
                            child: Padding(
                              padding: EdgeInsets.all(14.h),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Utils.buildImgWithNetwork(
                                        url: "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown_female.png",
                                        iconColor: AppColors.transparent,
                                        width: 63.h,
                                        height: 63.h,
                                        fit: BoxFit.cover,
                                      ).circle(100).toShimmer2(isShow: true),
                                      SizedBox(width: 16.h),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            "Dr John Smith".toText16(isBold: true).toShimmer2(isShow: true),
                                            SizedBox(height: 8.h),
                                            Wrap(
                                              direction: Axis.horizontal,
                                              spacing: 3.h,
                                              runSpacing: 4.h,
                                              children: [
                                                AppCustomChipWidget(labelText: "").toShimmer2(isShow: true, width: 16.h),
                                                AppCustomChipWidget(labelText: "").toShimmer2(isShow: true, width: 16.h),
                                              ],
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          )
                        : medicalFileVM.patientVaccineList.isNotEmpty
                            ? AnimationConfiguration.staggeredList(
                                position: index,
                            duration: const Duration(milliseconds: 1000),
                            child: SlideAnimation(
                              verticalOffset: 100.0,
                              child: FadeInAnimation(
                                child: Container(
                                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                                    color: AppColors.whiteColor,
                                    borderRadius: 20.h,
                                    hasShadow: true,
                                  ),
                                  child: Padding(
                                    padding: EdgeInsets.all(14.h),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            // Image.network(
                                            //   medicalFileVM.patientVaccineList[index].doctorImageURL,
                                            //   width: 63.h,
                                            //   height: 63.h,
                                            //   fit: BoxFit.fill,
                                            // ).circle(100).toShimmer2(isShow: false),

                                            SizedBox(width: 8.h),
                                            Expanded(
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                children: [
                                                  AppCustomChipWidget(
                                                      backgroundColor: AppColors.lightGreenButtonColor,
                                                      textColor: AppColors.textGreenColor,
                                                      padding: EdgeInsets.all(0),
                                                      labelText: medicalFileVM.patientVaccineList[index].vaccineName).toShimmer2(isShow: false, width: 16.h, ),
                                                  SizedBox(height:10.h),
                                                  (medicalFileVM.patientVaccineList[index].doctorName).toString().toText16(isBold: true).toShimmer2(isShow: false),
                                                  SizedBox(height: 8.h),
                                                  Wrap(
                                                    direction: Axis.horizontal,
                                                    spacing: 3.h,
                                                    runSpacing: 4.h,
                                                    children: [
                                                      AppCustomChipWidget(
                                                          icon: AppAssets.doctor_calendar_icon,
                                                          labelText: DateUtil.formatDateToDate(DateUtil.convertStringToDate(medicalFileVM.patientVaccineList[index].vaccinationDate), false)),

                                                      AppCustomChipWidget(labelText: medicalFileVM.patientVaccineList[index].clinicName).toShimmer2(isShow: false, width: 16.h),
                                                      AppCustomChipWidget(labelText: medicalFileVM.patientVaccineList[index].projectName).toShimmer2(isShow: false, width: 16.h),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ],
                                        ),
                                        // Row(
                                        //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        //   children: [
                                        //     "".toText16(),
                                        //     Utils.buildSvgWithAssets(icon: AppAssets.forward_arrow_icon, width: 15.h, height: 15.h, fit: BoxFit.contain, iconColor: AppColors.textColor),
                                        //   ],
                                        // ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ),
                              )
                            : Utils.getNoDataWidget(context, noDataText: LocaleKeys.noDataAvailable.tr(context: context));
                  },
                  separatorBuilder: (BuildContext cxt, int index) => SizedBox(height: 16.h),
                ),
                SizedBox(height: 60.h),
              ],
            );
          }),
        ),
      ),
    );
  }
}
