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/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/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/prescriptions/prescriptions_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/emergency_services/history/widget/RequestStatus.dart';
import 'package:hmg_patient_app_new/presentation/lab/lab_result_item_view.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/chip/app_custom_chip_widget.dart';
import 'package:provider/provider.dart';

class PrescriptionDeliveryOrdersListPage extends StatelessWidget {
  const PrescriptionDeliveryOrdersListPage({super.key});

  @override
  Widget build(BuildContext context) {
    AppState appState = getIt<AppState>();
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: LocaleKeys.orders.tr(context: context),
        child: SingleChildScrollView(
          child: Consumer<PrescriptionsViewModel>(builder: (context, model, child) {
            return Column(
              children: [
                ListView.builder(
                  itemCount: model.isPrescriptionsDeliveryOrdersLoading
                      ? 4
                      : model.prescriptionsOrderList.isNotEmpty
                          ? model.prescriptionsOrderList.length
                          : 1,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  padding: const EdgeInsets.only(left: 0, right: 8),
                  itemBuilder: (context, index) {
                    return model.isPrescriptionsDeliveryOrdersLoading
                        ? LabResultItemView(
                            onTap: () {},
                            labOrder: null,
                            index: index,
                            isLoading: true,
                          )
                        : model.prescriptionsOrderList.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: InkWell(
                                        onTap: () {},
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Padding(
                                              padding: EdgeInsets.all(16.h),
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                spacing: 8.h,
                                                children: [
                                                  RequestStatus(status: model.prescriptionsOrderList[index].statusId ?? 0),
                                                  appState.isArabic()
                                                      ? " رقم الطلب: ${model.prescriptionsOrderList[index].iD}".toText16(color: AppColors.textColor, isBold: true)
                                                      : "Req ID: ${model.prescriptionsOrderList[index].iD}".toText16(color: AppColors.textColor, isBold: true),
                                                  Row(
                                                    spacing: 4.w,
                                                    children: [
                                                      chip(Utils.getDayMonthYearDateFormatted(DateTime.tryParse(model.prescriptionsOrderList[index].created!)), AppAssets.calendar,
                                                          AppColors.blackBgColor),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              )
                            : Utils.getNoDataWidget(context, noDataText: LocaleKeys.noPrescriptionOrdersYet.tr(context: context));
                  },
                ).paddingSymmetrical(24.h, 0.h),
              ],
            );
          }),
        ),
      ),
    );
  }

  chip(String title, String iconString, Color iconColor) {
    return AppCustomChipWidget(
      labelText: title,
      icon: iconString,
      iconColor: iconColor,
      iconSize: 12.h,
    );
  }
}
