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/enums.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/lab_view_model.dart' show LabViewModel;
import 'package:hmg_patient_app_new/features/lab/models/resp_models/lab_result.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/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';
import 'package:hmg_patient_app_new/widgets/chip/custom_chip_widget.dart';
import 'package:provider/provider.dart';

class LabOrderResultItem extends StatelessWidget {
  final VoidCallback onTap;
  final int index;
  final LabResult? tests;
  final String severityText;
  final bool isLoading;
  final bool isExpanded;
  final Color iconColor;

  const LabOrderResultItem({super.key, required this.onTap, this.tests, required this.index, this.isLoading = false, this.isExpanded = false,required this.iconColor, required this.severityText});

  @override
  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: Container(
          key: ValueKey<int>(index),
          padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 16.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // ...labOrder!.testDetails!.map((detail) {
              Padding(
                padding: EdgeInsets.only(bottom: 8.h),
                child: '${tests!.description}'.toText14(weight: FontWeight.w500),
              ),
              '${tests!.packageShortDescription}'.toText12(fontWeight: FontWeight.w500, color: AppColors.textColorLight),
              SizedBox(height: 12.h),
              Row(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Flexible(
                    child: Text(
                      "${tests?.resultValue} ${tests?.uOM}",
                      style: TextStyle(
                        fontSize: 24.fSize,
                        fontWeight: FontWeight.w600,
                        fontFamily: 'Poppins',
                        color: context.read<LabViewModel>().getColor(
                          tests?.calculatedResultFlag ?? "",
                        ),
                        letterSpacing: -2,
                      ),
                    ),
                  ),
                  SizedBox(width: 4.h,),
                  Visibility(
                    visible: tests?.checkIfGraphShouldBeDisplayed() == true,
                    child: Expanded(
                      flex: 2,
                    child: Visibility(
                      visible: tests?.referanceRange != null,
                      child: Text(
                        "(Reference range ${tests?.referanceRange})".needTranslation,
                        style: TextStyle(
                          fontSize: 12.fSize,
                          fontWeight: FontWeight.w500,
                          fontFamily: 'Poppins',
                          color: AppColors.greyTextColor,
                        ),
                        // overflow: TextOverflow.ellipsis,
                        // maxLines: 2,
                        softWrap: true,
                      ),
                    ),
                    ),
                  )
                ],
              ),

              SizedBox(height: 12.h),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Visibility(
                    visible: tests?.checkIfGraphShouldBeDisplayed() == true,
                    child: Column(
                      spacing: 6.h,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        severityText.tr().toText10(
                            weight: FontWeight.w500,
                            color: AppColors.greyTextColor),
                        Utils.buildSvgWithAssets(
                            icon: AppAssets.lab_result_indicator,
                            width: 21,
                            height: 23,
                            iconColor: iconColor),
                      ],
                    ),
                  ),
                  CustomButton(
                    icon: AppAssets.view_report_icon,
                    iconColor: AppColors.primaryRedColor,
                    iconSize: 16.h,
                    text: LocaleKeys.viewReport.tr(context: context),
                    onPressed: () {
                        onTap();
                    },
                    backgroundColor: AppColors.secondaryLightRedColor,
                    borderColor: AppColors.secondaryLightRedColor,
                    textColor: AppColors.primaryRedColor,
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    borderRadius: 12,
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 40.h,
                  ),
                ],
              ),
            ],
          ),
        ));
  }
}
