import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

/// Enum for vital sign status to avoid string-based comparisons
enum VitalSignStatus {
  normal,
  high,
  low,
  obese,
  overweight,
  underweight,
  skinny,
}

/// UI-only helper model for Vital Sign cards.
///
/// Keeps presentation logic (chip colors, icon colors, simple status rules)
/// in one place so it can be reused across multiple pages.
class VitalSignUiModel {
  final Color iconBg;
  final Color iconFg;
  final Color chipBg;
  final Color chipFg;

  const VitalSignUiModel({
    required this.iconBg,
    required this.iconFg,
    required this.chipBg,
    required this.chipFg,
  });

  /// Returns a color scheme for a card based on its [status] and [label].
  ///
  /// Rules:
  /// - Height is always blue.
  /// - High, Obese => red scheme.
  /// - Overweight => orange scheme.
  /// - Low, Underweight, Skinny => yellow scheme.
  /// - Otherwise => green scheme (Normal).
  static VitalSignUiModel scheme({required VitalSignStatus? status, required String label}) {
    final l = label.toLowerCase();

    // Height should always be blue.
    if (l.contains('height')) {
      return VitalSignUiModel(
        iconBg: AppColors.infoColor.withValues(alpha: 0.12),
        iconFg: AppColors.infoColor,
        chipBg: AppColors.infoColor.withValues(alpha: 0.12),
        chipFg: AppColors.infoColor,
      );
    }

    // High, Obese => red scheme (health concerns)
    if (status == VitalSignStatus.high || status == VitalSignStatus.obese) {
      return VitalSignUiModel(
        iconBg: AppColors.chipSecondaryLightRedColor,
        iconFg: AppColors.primaryRedColor,
        chipBg: AppColors.chipSecondaryLightRedColor,
        chipFg: AppColors.primaryRedColor,
      );
    }

    // Overweight => orange scheme (warning)
    if (status == VitalSignStatus.overweight) {
      return VitalSignUiModel(
        iconBg: AppColors.warningColorYellow.withValues(alpha: 0.12),
        iconFg: AppColors.warningColorYellow,
        chipBg: AppColors.warningColorYellow.withValues(alpha: 0.12),
        chipFg: AppColors.warningColorYellow,
      );
    }

    // Low, Underweight, Skinny => yellow scheme (warning)
    if (status == VitalSignStatus.low ||
        status == VitalSignStatus.underweight ||
        status == VitalSignStatus.skinny) {
      final Color yellowBg = AppColors.highAndLow.withValues(alpha: 0.12);
      return VitalSignUiModel(
        iconBg: yellowBg,
        iconFg: AppColors.highAndLow,
        chipBg: yellowBg,
        chipFg: AppColors.highAndLow,
      );
    }

    // Normal (green)
    final Color greenBg = AppColors.lightGreenColor;
    return VitalSignUiModel(
      iconBg: greenBg,
      iconFg: AppColors.bgGreenColor,
      chipBg: greenBg,
      chipFg: AppColors.bgGreenColor,
    );
  }

  /// Convert VitalSignStatus enum to localized string
  static String statusToString(VitalSignStatus status) {
    switch (status) {
      case VitalSignStatus.normal:
        return LocaleKeys.vitalSignNormal.tr();
      case VitalSignStatus.high:
        return LocaleKeys.vitalSignHigh.tr();
      case VitalSignStatus.low:
        return LocaleKeys.vitalSignLow.tr();
      case VitalSignStatus.obese:
        return LocaleKeys.vitalSignObese.tr();
      case VitalSignStatus.overweight:
        return LocaleKeys.vitalSignOverweight.tr();
      case VitalSignStatus.underweight:
        return LocaleKeys.vitalSignUnderweight.tr();
      case VitalSignStatus.skinny:
        return LocaleKeys.vitalSignSkinny.tr();
    }
  }

  /// Simple, user-friendly classification:
  /// - Low: systolic < 90 OR diastolic < 60
  /// - High: systolic >= 140 OR diastolic >= 90
  /// - Normal: otherwise
  /// Returns null if values are missing/unparseable.
  static VitalSignStatus? bloodPressureStatus({dynamic systolic, dynamic diastolic}) {
    final int? s = toIntOrNull(systolic);
    final int? d = toIntOrNull(diastolic);
    if (s == null || d == null) return null;

    if (s < 90 || d < 60) return VitalSignStatus.low;
    if (s >= 140 || d >= 90) return VitalSignStatus.high;
    return VitalSignStatus.normal;
  }

  static int? toIntOrNull(dynamic v) {
    if (v == null) return null;
    if (v is int) return v;
    if (v is double) return v.round();
    return int.tryParse(v.toString());
  }

  static VitalSignStatus? bmiStatus(dynamic bmi) {
    // Return null if BMI is not available or is 0
    final double bmiResult = double.tryParse(bmi.toString()) ?? 0;

    if (bmiResult == 0 || bmi == null) {
      return null;
    }

    // BMI >= 30 (Obese) => Red
    if (bmiResult >= 30) {
      return VitalSignStatus.obese;
    }
    // BMI >= 25 (Overweight) => Orange
    else if (bmiResult >= 25) {
      return VitalSignStatus.overweight;
    }
    // BMI >= 18.5 (Normal) => Green
    else if (bmiResult >= 18.5) {
      return VitalSignStatus.normal;
    }
    // BMI >= 16 (Underweight) => Yellow
    else if (bmiResult >= 16) {
      return VitalSignStatus.underweight;
    }
    // BMI < 16 (Skinny/Severely underweight) => Yellow
    else {
      return VitalSignStatus.skinny;
    }
  }

  /// Weight status based on BMI with detailed classification
  /// Returns: Obese, Overweight, Normal, Underweight, Skinny
  static VitalSignStatus? weightStatus(dynamic bmi) {
    // Return null if BMI is not available or is 0
    final double bmiResult = double.tryParse(bmi.toString()) ?? 0;

    if (bmiResult == 0 || bmi == null) {
      return null;
    }

    // BMI >= 30 (Obese) => Red
    if (bmiResult >= 30) {
      return VitalSignStatus.obese;
    }
    // BMI >= 25 (Overweight) => Orange
    else if (bmiResult >= 25) {
      return VitalSignStatus.overweight;
    }
    // BMI >= 18.5 (Normal) => Green
    else if (bmiResult >= 18.5) {
      return VitalSignStatus.normal;
    }
    // BMI >= 16 (Underweight) => Yellow
    else if (bmiResult >= 16) {
      return VitalSignStatus.underweight;
    }
    // BMI < 16 (Skinny/Severely underweight) => Yellow
    else {
      return VitalSignStatus.skinny;
    }
  }
}

