import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/date_util.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/lab/models/resp_models/patient_lab_orders_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';

class LabResultItemView extends StatelessWidget {
  final VoidCallback onTap;
  final int index;
  final PatientLabOrdersResponseModel? labOrder;
  final bool isLoading;
  final bool isExpanded;

  LabResultItemView({Key? key, required this.onTap, this.labOrder, required this.index, this.isLoading = false, this.isExpanded = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return 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: () {
          if (!isLoading) {
            onTap();
          }
        },
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.all(16.h),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      AppCustomChipWidget(
                        labelText: getLabOrderStatusText(labOrder?.status ?? 0, context),
                        backgroundColor: getLabOrderStatusColor(labOrder?.status ?? 0).withOpacity(0.15),
                        textColor: getLabOrderStatusColor(labOrder?.status ?? 0),
                      ).toShimmer2(isShow: isLoading, width: 100),
                      // if (!isLoading) Icon(isExpanded ? Icons.expand_less : Icons.expand_more),
                    ],
                  ),
                  SizedBox(height: 8.h),
                  Row(
                    children: [
                      Utils.buildImgWithNetwork(
                        url: isLoading ? "" : labOrder!.doctorImageURL!,
                        iconColor: AppColors.transparent,
                        width: 24.h,
                        height: 24.h,
                        fit: BoxFit.cover,
                        errorBuilder: (cxt, child, tr) {
                          return SizedBox(height: 24, width: 24);
                        },
                      ).toShimmer2(isShow: isLoading).circle(100),
                      SizedBox(width: 4.h),
                      (labOrder?.doctorName ?? "").toText16(isBold: true).toShimmer2(isShow: isLoading, width: 200)
                    ],
                  ),
                  SizedBox(height: 8.h),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Wrap(
                        spacing: 8.h,
                        runSpacing: 0.h,
                        children: [
                          AppCustomChipWidget(labelText: isLoading ? "null" : DateUtil.formatDateToDate(DateUtil.convertStringToDate(labOrder!.createdOn), false)).toShimmer2(isShow: isLoading, width: 70),
                          AppCustomChipWidget(labelText: isLoading ? "null" : labOrder!.clinicDescription!).toShimmer2(isShow: isLoading, width: 100),
                        ],
                      ),
                      isLoading
                          ? SizedBox.shrink()
                          : Utils.buildSvgWithAssets(
                              icon: AppAssets.forward_arrow_icon_small,
                        iconColor: AppColors.textColor,
                        width: 20.h,
                        height: 14.h,
                        fit: BoxFit.contain,
                      ),
                    ],
                  ),
                ],
              ),
            ),
            // AnimatedSwitcher(
            //   duration: Duration(milliseconds: 300),
            //   switchInCurve: Curves.easeIn,
            //   switchOutCurve: Curves.easeOut,
            //   transitionBuilder: (Widget child, Animation<double> animation) {
            //     return FadeTransition(
            //       opacity: animation,
            //       child: SizeTransition(
            //         sizeFactor: animation,
            //         axisAlignment: 0.0,
            //         child: child,
            //       ),
            //     );
            //   },
            //   child: isExpanded
            //       ? Container(
            //           key: ValueKey<int>(index),
            //           padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 8.h),
            //           child: Column(
            //             crossAxisAlignment: CrossAxisAlignment.start,
            //             children: [
            //               ...labOrder!.testDetails!.map((detail) {
            //                 return Padding(
            //                   padding: EdgeInsets.only(bottom: 8.h),
            //                   child: '● ${detail.description}'.toText14(isBold: true),
            //                 );
            //               }).toList(),
            //               SizedBox(height: 16.h),
            //               Row(
            //                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //                 children: [
            //                   SizedBox(),
            //                   CustomButton(
            //                     icon: AppAssets.view_report_icon,
            //                     iconColor: AppColors.primaryRedColor,
            //                     iconSize: 16.h,
            //                     text: LocaleKeys.viewReport.tr(context: context),
            //                     onPressed: () {},
            //                     backgroundColor: AppColors.secondaryLightRedColor,
            //                     borderColor: AppColors.secondaryLightRedColor,
            //                     textColor: AppColors.primaryRedColor,
            //                     fontSize: 14,
            //                     fontWeight: FontWeight.bold,
            //                     borderRadius: 12,
            //                     padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
            //                     height: 40.h,
            //                   ),
            //                 ],
            //               ),
            //             ],
            //           ),
            //         )
            //       : SizedBox.shrink(key: ValueKey<int>(-index)),
            // ),
          ],
        ),
      ),
    );
  }

  String getLabOrderStatusText(num status, context) {
    switch (status) {
      case 44:
        return LocaleKeys.resultsPending.tr(context: context);
      case 45:
        return LocaleKeys.resultsPending.tr(context: context);
      case 16:
        return LocaleKeys.resultsAvailable.tr(context: context);
      case 17:
        return LocaleKeys.resultsAvailable.tr(context: context);
      default:
        return "";
    }
  }

  Color getLabOrderStatusColor(num status) {
    switch (status) {
      case 44:
        return AppColors.warningColorYellow;
      case 45:
        return AppColors.warningColorYellow;
      case 16:
        return AppColors.successColor;
      case 17:
        return AppColors.successColor;
      default:
        return AppColors.greyColor;
    }
  }
}
