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/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/enums.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/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/common_bottom_sheet.dart';
import 'package:provider/provider.dart';
import 'dart:ui' as ui;

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: false),
        child: Container(
          key: ValueKey<int>(index),
          padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 4.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // ...labOrder!.testDetails!.map((detail) {
              Padding(
                padding: EdgeInsets.only(bottom: 8.h),
                child: '${tests!.description}'.toText14(isBold: true, isEnglishOnly: true),
              ),
              // (tests!.packageShortDescription ?? "").toText12(isBold: true, color: AppColors.textColorLight),

              (((tests!.testShortDescription != null && tests!.testShortDescription!.isNotEmpty) ? tests!.testShortDescription : tests!.packageShortDescription) ?? "")
                  .toText12(isBold: true, color: AppColors.textColorLight),
              SizedBox(height: 12.h),
              Directionality(
                textDirection: ui.TextDirection.ltr,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "${tests?.resultValue} ${tests?.uOM}",
                      style: TextStyle(
                        fontSize: 24.f,
                        fontWeight: FontWeight.w600,
                        fontFamily: getIt.get<AppState>().isArabic() ? 'CairoArabic' : 'Poppins',
                        color: tests!.checkIfGraphShouldBeDisplayed()
                            ? context.read<LabViewModel>().getColor(
                                  tests?.calculatedResultFlag ?? "",
                                )
                            : Colors.grey.shade700,
                        letterSpacing: -1,
                      ),
                    ),
                    // SizedBox(
                    //   height: 2.h,
                    // ),
                    Visibility(
                      visible: tests?.referanceRange != null,
                      child: Text(
                        "(${LocaleKeys.referenceRange.tr(context: context)}: ${tests?.referanceRange})",
                        style: TextStyle(
                          fontSize: 12.f,
                          fontWeight: FontWeight.w600,
                          fontFamily: getIt.get<AppState>().isArabic() ? 'CairoArabic' : 'Poppins',
                          color: AppColors.greyTextColor,
                        ),
                        softWrap: true,
                      ),
                    ),
                    Visibility(
                      visible: tests?.notes != null &&  tests?.notes?.isNotEmpty == true,
                      child: Text(
                        tests?.notes??"",
                        style: TextStyle(
                          fontSize: 12.f,
                          fontWeight: FontWeight.w600,
                          fontFamily: getIt.get<AppState>().isArabic() ? 'CairoArabic' : 'Poppins',
                          color: AppColors.greyTextColor,
                        ),
                        softWrap: true,
                      ),
                    ),
                  ],
                ),
              ),

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

  _buildSeverityDetailsBottomsheet({required String severity,required BuildContext context}) {
    return showCommonBottomSheetWithoutHeight(
      title: LocaleKeys.colorScience.tr(context: context),
      context,
      child: Container(
        padding: EdgeInsets.all(16.w),
        width: double.infinity,
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.r, hasShadow: true),
        child: Column(
          children: [
            _buildInfoTile(severityValue: severity, context: context),
          ],
        ),
      ),
      callBackFunc: () {},
      isFullScreen: false,
      isCloseButtonVisible: true,
    );
  }

  Widget _buildInfoTile({required String severityValue, required BuildContext context}) {

    String title = LocaleKeys.emergency.tr(context: context);
    String description = LocaleKeys.emergencyDescription.tr(context: context);
    Color dotColor = AppColors.chipColorEmergency;
    switch (severityValue) {
      case 'CL':
      case 'LCL':
      title = LocaleKeys.emergency.tr(context: context);
      description = LocaleKeys.emergencyDescription.tr(context: context);
      dotColor = AppColors.chipColorEmergency;
      case 'L':
      title = LocaleKeys.seekMedicalAdvice.tr(context: context);
      description = LocaleKeys.seekMedicalAdviceDescription.tr(context: context);
      dotColor = AppColors.chipColorSeekMedicalAdvice;
      case 'N':
      title = LocaleKeys.monitor.tr(context: context);
      description = LocaleKeys.monitorDescription.tr(context: context);
      dotColor = AppColors.chipColorMonitor;
      case 'H':
        title = LocaleKeys.seekMedicalAdvice.tr(context: context);
        description = LocaleKeys.seekMedicalAdviceDescription.tr(context: context);
        dotColor = AppColors.chipColorSeekMedicalAdvice;
      case 'CH':
      case 'HCH':
      title = LocaleKeys.emergency.tr(context: context);
      description = LocaleKeys.emergencyDescription.tr(context: context);
      dotColor = AppColors.chipColorEmergency;
    }

    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          height: 14.h,
          width: 14.w,
          decoration: BoxDecoration(shape: BoxShape.circle, color: dotColor),
        ).paddingOnly(top: 3.h),
        SizedBox(width: 8.w),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              title.toText14(isBold: true, color: AppColors.textColor),
              SizedBox(height: 4.h),
              description.toText12(isBold: true, color: AppColors.greyTextColor),
            ],
          ),
        ),
      ],
    );
  }
}
