import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/radiology/models/resp_models/radiology_ai_analysis_response_model.dart';
import 'package:hmg_patient_app_new/features/radiology/radiology_view_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/accessibility/preserve_svg_colors.dart';

class RadiologyAiAnalysisWidget extends StatelessWidget {
  final RadiologyAiAnalysisResponse data;

  const RadiologyAiAnalysisWidget({super.key, required this.data});

  List<String> _parseStringToList(String? text) {
    if (text == null || text.isEmpty) return [];
    // Split by newlines and filter out empty lines
    return text.split('\n').where((line) => line.trim().isNotEmpty).map((line) => line.trim()).toList();
  }

  @override
  Widget build(BuildContext context) {
    // Parse the string fields into lists
    final predictions = _parseStringToList(data.aiPredictions);
    final preventions = _parseStringToList(data.prevention);

    return Container(
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(24.r), gradient: AppColors.aiLinearGradient),
      padding: EdgeInsets.all(1.5.r), // Border thickness
      child: Container(
        decoration: BoxDecoration(
          color: AppColors.whiteColor,
          borderRadius: BorderRadius.circular(22.r),
        ),
        padding: EdgeInsets.all(20.r),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                LocaleKeys.aiAnalysis.tr().toText18(weight: FontWeight.w700, color: AppColors.blackColor),
                Container(
                  padding: EdgeInsets.all(8.r),
                  decoration: BoxDecoration(gradient: AppColors.aiLinearGradient, borderRadius: BorderRadius.circular(8.r)),
                  child: PreserveSvgColors(child: SvgPicture.asset(AppAssets.aiOverView, colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn), height: 18.r, width: 18.r)),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            if (data.summary != null && data.summary!.isNotEmpty) ...[
              LocaleKeys.suggestion.tr().toText16(weight: FontWeight.w700, color: AppColors.blackColor),
              SizedBox(height: 8.h),
              data.summary!.toText14(color: AppColors.textColorLight, height: 1.5, weight: FontWeight.w400),
              SizedBox(height: 16.h),
              Divider(color: AppColors.dividerColor, thickness: 1),
              SizedBox(height: 16.h),
            ],
            if (predictions.isNotEmpty) ...[
              LocaleKeys.prediction.tr().toText16(weight: FontWeight.w700, color: AppColors.blackColor),
              SizedBox(height: 12.h),
              ...predictions.map((item) => _buildBulletItem(item)),
              SizedBox(height: 16.h),
              Divider(color: AppColors.dividerColor, thickness: 1),
              SizedBox(height: 16.h),
            ],
            if (preventions.isNotEmpty) ...[
              LocaleKeys.prevention.tr().toText16(weight: FontWeight.w700, color: AppColors.blackColor),
              SizedBox(height: 12.h),
              ...preventions.map((item) => _buildBulletItem(item)),
              SizedBox(height: 20.h),
            ],
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Icon(Icons.info_outline, color: AppColors.greyTextColor, size: 20.r),
                SizedBox(width: 8.w),
                Expanded(
                  child: ("${LocaleKeys.disclaimer.tr()}:${LocaleKeys.disclaimerRadiology.tr()}").toText12(color: AppColors.greyTextColor, fontWeight: FontWeight.w400),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            CustomButton(
              height: 50.h,
              text: LocaleKeys.close.tr(context: context),
              backgroundColor: AppColors.primaryRedColor,
              borderColor: AppColors.primaryRedColor,
              onPressed: () {
                getIt.get<RadiologyViewModel>().closeRadiologyAIAnalysis();
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBulletItem(String text) {
    return Padding(
      padding: EdgeInsets.only(bottom: 12.h),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(top: 8.h, right: 12.w, left: 4.w),
            child: Container(
              width: 5.r,
              height: 5.r,
              decoration: BoxDecoration(color: AppColors.blackColor, shape: BoxShape.circle),
            ),
          ),
          Expanded(
            child: text.toText14(color: AppColors.textColorLight, height: 1.5, weight: FontWeight.w400),
          ),
        ],
      ),
    );
  }
}
