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_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/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/insurance/insurance_view_model.dart';
import 'package:hmg_patient_app_new/features/insurance/models/resp_models/patient_insurance_approval_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/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';
import 'package:provider/provider.dart';

import 'dart:ui' as ui;

class InsuranceApprovalDetailsPage extends StatelessWidget {
  InsuranceApprovalDetailsPage({super.key, required this.insuranceApprovalResponseModel});

  InsuranceApprovalResponseModel insuranceApprovalResponseModel;
  late InsuranceViewModel insuranceViewModel;
  late AppState appState;

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    insuranceViewModel = Provider.of<InsuranceViewModel>(context, listen: false);
    return CollapsingListView(
      title: "${LocaleKeys.insurance.tr(context: context)} ${LocaleKeys.approvals.tr(context: context)}",
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
              color: AppColors.whiteColor,
              borderRadius: 24.h,
              hasShadow: false,
            ),
            child: Padding(
              padding: EdgeInsets.all(14.h),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Wrap(
                    alignment: WrapAlignment.start,
                    direction: Axis.horizontal,
                    spacing: 6.h,
                    runSpacing: 6.h,
                    children: [
                      AppCustomChipWidget(
                        labelText: insuranceApprovalResponseModel.approvalStatusDescption,
                        backgroundColor: insuranceApprovalResponseModel.status == 9
                            ? AppColors.successColor.withValues(alpha: 0.1)
                            : (insuranceApprovalResponseModel.approvalStatusDescption?.toLowerCase().contains('partial') == true || insuranceApprovalResponseModel.status == 8)
                                ? AppColors.warningColorYellow.withValues(alpha: 0.1)
                                : AppColors.primaryRedColor.withValues(alpha: 0.1),
                        textColor: insuranceApprovalResponseModel.status == 9
                            ? AppColors.successColor
                            : (insuranceApprovalResponseModel.approvalStatusDescption?.toLowerCase().contains('partial') == true || insuranceApprovalResponseModel.status == 8)
                                ? AppColors.warningColorYellow
                                : AppColors.primaryRedColor,
                      ),
                      AppCustomChipWidget(
                        icon: (!insuranceApprovalResponseModel.isLiveCareAppointment! ? AppAssets.walkin_appointment_icon : AppAssets.small_livecare_icon),
                        iconColor: !insuranceApprovalResponseModel.isLiveCareAppointment! ? AppColors.textColor : AppColors.whiteColor,
                        labelText: insuranceApprovalResponseModel.isLiveCareAppointment! ? LocaleKeys.livecare.tr(context: context) : LocaleKeys.walkin.tr(context: context),
                        backgroundColor: (!insuranceApprovalResponseModel.isLiveCareAppointment! ? AppColors.greyColor : AppColors.successColor),
                        textColor: (!insuranceApprovalResponseModel.isLiveCareAppointment! ? AppColors.textColor : AppColors.whiteColor),
                      ),
                      AppCustomChipWidget(
                        labelText: appState.isArabic() ? insuranceApprovalResponseModel.isInOutPatientDescriptionN! : insuranceApprovalResponseModel.isInOutPatientDescription!,
                        backgroundColor: AppColors.warningColorYellow.withValues(alpha: 0.1),
                        textColor: AppColors.warningColorYellow,
                      ),
                    ],
                  ),
                  SizedBox(height: 16.h),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Utils.buildImgWithNetwork(
                        url: insuranceApprovalResponseModel.doctorImageURL!,
                        iconColor: AppColors.blackBgColor,
                        width: 63.h,
                        height: 63.h,
                        fit: BoxFit.cover,
                      ).circle(100),
                      SizedBox(width: 16.h),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            (insuranceApprovalResponseModel.doctorName!).toText16(isBold: true),
                            SizedBox(height: 8.h),
                            Wrap(
                              direction: Axis.horizontal,
                              spacing: 3.h,
                              runSpacing: 4.h,
                              children: [
                                AppCustomChipWidget(labelText: insuranceApprovalResponseModel.clinicName ?? ""),
                                AppCustomChipWidget(labelText: "${LocaleKeys.approvalNo.tr(context: context)} ${insuranceApprovalResponseModel.approvalNo}", isEnglishOnly: true),
                                AppCustomChipWidget(labelText: "${LocaleKeys.unusedCount.tr(context: context)} ${insuranceApprovalResponseModel.unUsedCount}", isEnglishOnly: true),
                                AppCustomChipWidget(labelText: "${LocaleKeys.companyName.tr(context: context)} ${insuranceApprovalResponseModel.companyName}"),
                                AppCustomChipWidget(
                                  richText: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      "${LocaleKeys.receiptOn.tr(context: context)} ".toText10(isBold: true),
                                      Directionality(
                                          textDirection: ui.TextDirection.ltr,
                                          child: DateUtil.formatDateToDate(DateUtil.convertStringToDate(insuranceApprovalResponseModel.receiptOn), false).toText10(isBold: true)),
                                    ],
                                  ),
                                  isEnglishOnly: true,
                                ),

                                AppCustomChipWidget(
                                  richText: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      "${LocaleKeys.expiryOn.tr(context: context)} ".toText10(isBold: true),
                                      Directionality(
                                          textDirection: ui.TextDirection.ltr,
                                          child: DateUtil.formatDateToDate(DateUtil.convertStringToDate(insuranceApprovalResponseModel.expiryDate), false).toText10(isBold: true)),
                                    ],
                                  ),
                                  isEnglishOnly: true,
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 16.h),
          Container(
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
              color: AppColors.whiteColor,
              borderRadius: 24.h,
              hasShadow: false,
            ),
            child: Padding(
              padding: EdgeInsets.all(16.h),
              child: insuranceApprovalResponseModel.apporvalDetails != null ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LocaleKeys.approvalDetails.tr().toText16(isBold: true),
                        ListView.separated(
                          padding: EdgeInsets.only(top: 16.h),
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: insuranceApprovalResponseModel.apporvalDetails!.length,
                          itemBuilder: (context, index) {
                            return 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,
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            LocaleKeys.procedureName.tr(context: context).toText14(isBold: true),
                                            Expanded(
                                              child: (insuranceApprovalResponseModel.apporvalDetails![index]!.procedureName ?? "")
                                                  .toText12(isBold: true, color: AppColors.greyTextColor, textAlignment: TextAlign.end, maxLine: 2),
                                            ),
                                          ],
                                        ),
                                        SizedBox(height: 8.h),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            "${LocaleKeys.status.tr(context: context)}: ".toText14(isBold: true),
                                            (insuranceApprovalResponseModel.apporvalDetails![index].status ?? "").toText12(isBold: true, color: AppColors.greyTextColor),
                                          ],
                                        ),
                                        SizedBox(height: 8.h),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            "${LocaleKeys.usageStatus.tr(context: context)}: ".toText14(isBold: true),
                                            (insuranceApprovalResponseModel.apporvalDetails![index].isInvoicedDesc ?? "").toText12(isBold: true, color: AppColors.greyTextColor),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            );
                          },
                          separatorBuilder: (BuildContext cxt, int index) => Column(
                            children: [
                              SizedBox(height: 16.h),
                              Divider(height: 1, color: AppColors.greyColor),
                              SizedBox(height: 16.h),
                            ],
                          ),
                        ),
                      ],
                    )
                  : SizedBox.shrink(),
            ),
          )
        ],
      ).paddingSymmetrical(24.h, 24.h),
    );
  }
}
