import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/symptoms_checker/models/resp_models/triage_level_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

class TriageLevelCard extends StatefulWidget {
  final TriageLevelDataDetails triageLevelData;

  const TriageLevelCard({super.key, required this.triageLevelData});

  @override
  State<TriageLevelCard> createState() => _TriageLevelCardState();
}

class _TriageLevelCardState extends State<TriageLevelCard> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _borderAnimation;

  @override
  void initState() {
    super.initState();

    // Create animation controller for border pulse effect
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 2000),
    );

    // Create animation that goes from 1.0 to 0.3 and back (for opacity)
    _borderAnimation = Tween<double>(begin: 1.0, end: 0.3).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeInOut,
      ),
    );

    // Start the animation and repeat
    _animationController.repeat(reverse: true);
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  /// Get localized title based on triage level
  String getTitle(BuildContext context) {
    final triageLevel = widget.triageLevelData.triageLevel ?? '';

    switch (triageLevel) {
      case 'emergency_ambulance':
        return LocaleKeys.triageLevelEmergencyAmbulance.tr(context: context);
      case 'emergency':
        return LocaleKeys.triageLevelEmergency.tr(context: context);
      case 'consultation_24':
        return LocaleKeys.triageLevelConsultation24.tr(context: context);
      case 'consultation':
        return LocaleKeys.triageLevelConsultation.tr(context: context);
      case 'self_care':
        return LocaleKeys.triageLevelSelfCare.tr(context: context);
      default:
        return LocaleKeys.triageLevelConsultation.tr(context: context);
    }
  }

  /// Get localized description based on triage level
  String getDescription(BuildContext context) {
    final triageLevel = widget.triageLevelData.triageLevel ?? '';

    switch (triageLevel) {
      case 'emergency_ambulance':
        return LocaleKeys.triageLevelEmergencyAmbulanceDesc.tr(context: context);
      case 'emergency':
        return LocaleKeys.triageLevelEmergencyDesc.tr(context: context);
      case 'consultation_24':
        return LocaleKeys.triageLevelConsultation24Desc.tr(context: context);
      case 'consultation':
        return LocaleKeys.triageLevelConsultationDesc.tr(context: context);
      case 'self_care':
        return LocaleKeys.triageLevelSelfCareDesc.tr(context: context);
      default:
        return LocaleKeys.triageLevelConsultationDesc.tr(context: context);
    }
  }

  /// Get color based on triage level
  Color getBackgroundColor() {
    final triageLevel = widget.triageLevelData.triageLevel ?? '';

    switch (triageLevel) {
      case 'emergency_ambulance':
      case 'emergency':
        return AppColors.chipColorEmergency.withValues(alpha: 0.1);
      case 'consultation_24':
      case 'consultation':
        return AppColors.chipColorSeekMedicalAdvice.withValues(alpha: 0.1);
      case 'self_care':
        return AppColors.chipColorMonitor.withValues(alpha: 0.1);
      default:
        return AppColors.chipColorSeekMedicalAdvice.withValues(alpha: 0.1);
    }
  }

  /// Get border color based on triage level
  Color getBorderColor() {
    final triageLevel = widget.triageLevelData.triageLevel ?? '';

    switch (triageLevel) {
      case 'emergency_ambulance':
      case 'emergency':
        return AppColors.chipColorEmergency;
      case 'consultation_24':
      case 'consultation':
        return AppColors.chipColorSeekMedicalAdvice;
      case 'self_care':
        return AppColors.chipColorMonitor;
      default:
        return AppColors.chipColorSeekMedicalAdvice;
    }
  }

  /// Get icon based on triage level
  IconData getIcon() {
    final triageLevel = widget.triageLevelData.triageLevel ?? '';

    switch (triageLevel) {
      case 'emergency_ambulance':
        return Icons.local_hospital;
      case 'emergency':
        return Icons.emergency;
      case 'consultation_24':
        return Icons.schedule;
      case 'consultation':
        return Icons.medical_services;
      case 'self_care':
        return Icons.self_improvement;
      default:
        return Icons.medical_services;
    }
  }

  /// Get root cause text (localized)
  String? getRootCauseText(BuildContext context) {
    final rootCause = widget.triageLevelData.rootCause ?? '';

    switch (rootCause) {
      case 'emergency_evidence_present':
        return LocaleKeys.rootCauseEmergencyEvidencePresent.tr(context: context);
      case 'serious_evidence_present':
        return LocaleKeys.rootCauseSeriousEvidencePresent.tr(context: context);
      case 'emergency_condition_likely':
        return LocaleKeys.rootCauseEmergencyConditionLikely.tr(context: context);
      case 'emergency_condition_possible':
        return LocaleKeys.rootCauseEmergencyConditionPossible.tr(context: context);
      case 'consultation_condition_likely':
        return LocaleKeys.rootCauseConsultationConditionLikely.tr(context: context);
      case 'self_care_sufficient':
        return LocaleKeys.rootCauseSelfCareSufficient.tr(context: context);
      case 'diagnosis_unknown':
        return LocaleKeys.rootCauseDiagnosisUnknown.tr(context: context);
      default:
        return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    final borderColor = getBorderColor();
    final title = getTitle(context);
    final description = getDescription(context);
    final rootCauseText = getRootCauseText(context);
    final seriousSymptoms = widget.triageLevelData.serious ?? [];

    return AnimatedBuilder(
      animation: _borderAnimation,
      builder: (context, child) {
        return Container(
          width: double.infinity,
          margin: EdgeInsets.only(left: 24.w, right: 24.w, top: 16.h, bottom: 8.h),
          padding: EdgeInsets.all(20.w),
          decoration: BoxDecoration(
            color: AppColors.whiteColor,
            borderRadius: BorderRadius.circular(24.r),
            border: Border.all(
              color: borderColor.withValues(alpha: _borderAnimation.value),
              width: 2.w,
            ),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withValues(alpha: 0.08),
                blurRadius: 10.r,
                offset: Offset(0, 2.h),
              ),
            ],
          ),
          child: child,
        );
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Expanded(
                child: title.toText18(isBold: true, color: AppColors.textColor),
              ),
            ],
          ),

          SizedBox(height: 12.h),

          // Description
          description.toText14(color: AppColors.textColor),

          // Root Cause (if available)
          if (rootCauseText != null) ...[
            SizedBox(height: 12.h),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 0.w, vertical: 8.h),
              decoration: BoxDecoration(
                color: AppColors.whiteColor.withValues(alpha: 0.5),
                borderRadius: BorderRadius.circular(8.r),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(
                    Icons.info_outline,
                    size: 16.f,
                    color: borderColor,
                  ),
                  SizedBox(width: 8.w),
                  Flexible(
                    child: rootCauseText.toText12(
                      color: AppColors.textColor,
                      isBold: true,
                    ),
                  ),
                ],
              ),
            ),
          ],

          // Serious Symptoms (if any)
          if (seriousSymptoms.isNotEmpty) ...[
            SizedBox(height: 16.h),
            LocaleKeys.seriousSymptoms.tr(context: context).toText14(
                  isBold: true,
                  color: borderColor,
                ),
            SizedBox(height: 8.h),
            ...seriousSymptoms.map((symptom) {
              return Padding(
                padding: EdgeInsets.only(bottom: 4.h),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 6.h, right: 8.w, left: 8.w),
                      width: 6.w,
                      height: 6.h,
                      decoration: BoxDecoration(shape: BoxShape.circle, color: borderColor),
                    ),
                    Expanded(
                      child: (symptom.getDisplayName()).toText13(color: AppColors.textColor),
                    ),
                  ],
                ),
              );
            }),
          ],
        ],
      ),
    );
  }
}
