import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart' show AppAssets;
import 'package:hmg_patient_app_new/core/app_export.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/emergency_services/emergency_services_view_model.dart';
import 'package:hmg_patient_app_new/features/emergency_services/models/OrderDisplay.dart';
import 'package:hmg_patient_app_new/features/emergency_services/models/resp_model/AmbulanceRequestOrdersModel.dart';
import 'package:hmg_patient_app_new/features/emergency_services/models/resp_model/RRTServiceData.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/emergency_services/history/widget/ambulance_history_item.dart' show AmbulanceHistoryItem;
import 'package:hmg_patient_app_new/presentation/emergency_services/history/widget/rrt_item.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 ErHistoryListing extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
              child: CollapsingListView(
                  title: LocaleKeys.history.tr(context: context),
                  child: SingleChildScrollView(
                    physics: NeverScrollableScrollPhysics(),
                    child: Column(
                      children: [
                        Selector<EmergencyServicesViewModel,(List, bool)>(
                          selector: (context, vm) => (vm.orderDisplayList, vm.historyLoading),
                          builder: (context, data, _) {
                            return Column(
                              children: [
                                orderChips(context, data.$2, data.$1),
                                Visibility(
                                visible:data.$1.isNotEmpty == true,
                                child: ListView.builder(
                                  padding: EdgeInsets.only(top:24.h ),
                                    shrinkWrap: true,
                                    physics: NeverScrollableScrollPhysics(),
                                    itemCount:data?.$1.length ?? 0,
                                    itemBuilder: (_, index) {
                                        var order = data.$1[index];
                                        if (order is AmbulanceRequestOrdersModel) {
                                          return AmbulanceHistoryItem(order: order).toShimmer2(isShow: data.$2);
                                        } else {
                                          return RRTItem(order: (order as GetCMCAllOrdersResponseModel)).toShimmer2(isShow: data.$2);
                                        }
                                      }),
                                                      ),
                                Visibility(
                                    visible: data.$1.isEmpty == true,
                                      child: Center(
                                        child: Utils.getNoDataWidget(context, noDataText: LocaleKeys.noDataAvailable.tr(context: context)),
                                      )),
                              ],
                            );
                          }
                        ),


                      ]
                    ).paddingAll(16.h)

                  ))

          ),



        ],
      ),
    );
  }

  orderChips(BuildContext context, bool isLoading, List dataList) {

    if (dataList?.isEmpty == true) {
      return SizedBox.shrink();
    }
    return Selector<EmergencyServicesViewModel, OrderDislpay>(
          selector: (context, vm) => vm.currentlyDisplayedOrder,
          builder: (context, value, __) {
            return Row(
              spacing: 8.h,
              children: [
                if(dataList.isNotEmpty == true)
                AppCustomChipWidget(
                  labelText: LocaleKeys.allFacilities.tr(context: context),
                  shape: RoundedRectangleBorder(
                      side: BorderSide(
                        color: value == OrderDislpay.ALL ? AppColors.errorColor : AppColors.chipBorderColorOpacity20,
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(10)),
                  backgroundColor: value == OrderDislpay.ALL ? AppColors.secondaryLightRedColor : AppColors.whiteColor,
                  textColor: value == OrderDislpay.ALL ? AppColors.errorColor : AppColors.blackColor,
                ).onPress(() {
                  context.read<EmergencyServicesViewModel>().changeOrderDisplayItems(OrderDislpay.ALL);
                }).toShimmer2(isShow: isLoading),
                if(context
                    .read<EmergencyServicesViewModel>()
                    .ambulanceOrders
                    ?.isNotEmpty == true)
                  AppCustomChipWidget(
                    labelText: LocaleKeys.ambulancerequest.tr(context: context),
                    icon: AppAssets.ambulance,
                    shape: RoundedRectangleBorder(
                        side: BorderSide(
                          color: value == OrderDislpay.AMBULANCE ? AppColors.errorColor : AppColors.chipBorderColorOpacity20,
                          width: 1,
                        ),
                        borderRadius: BorderRadius.circular(10)),
                    backgroundColor: value == OrderDislpay.AMBULANCE ? AppColors.secondaryLightRedColor : AppColors.whiteColor,
                    textColor: value == OrderDislpay.AMBULANCE ? AppColors.errorColor : AppColors.blackColor,
                  ).toShimmer2(isShow: isLoading).onPress(() {
                    context.read<EmergencyServicesViewModel>().changeOrderDisplayItems(OrderDislpay.AMBULANCE);
                  }),
                if(context
                    .read<EmergencyServicesViewModel>()
                    .ordersRRT
                    ?.completedOrders
                    .isNotEmpty == true)
                  AppCustomChipWidget(
                    labelText: LocaleKeys.rapidResponseTeam.tr(context: context),
                    icon: AppAssets.ic_rrt_vehicle,
                    shape: RoundedRectangleBorder(
                        side: BorderSide(
                          color: value == OrderDislpay.RRT ? AppColors.errorColor : AppColors.chipBorderColorOpacity20,
                          width: 1,
                        ),
                        borderRadius: BorderRadius.circular(10)),
                    backgroundColor: value == OrderDislpay.RRT ? AppColors.secondaryLightRedColor : AppColors.whiteColor,
                    textColor: value == OrderDislpay.RRT ? AppColors.errorColor : AppColors.blackColor,
                  ).toShimmer2(isShow: isLoading).onPress(() {
                    context.read<EmergencyServicesViewModel>().changeOrderDisplayItems(OrderDislpay.RRT);
                  }),
              ],
            );
          });

  }
}
