// ABOUTME: Blood pressure category indicator widget showing BP category with color coding
// ABOUTME: Displays category icon, name, description, and visual status indicator

import 'package:flutter/material.dart';
import '../models/blood_pressure_entry.dart';
import '../utils/blood_pressure_utils.dart';

class BloodPressureCategoryIndicator extends StatelessWidget {
  final BloodPressureEntry? entry;
  final String? category;
  final bool showDescription;
  final bool showIcon;
  final bool compact;

  const BloodPressureCategoryIndicator({
    super.key,
    this.entry,
    this.category,
    this.showDescription = true,
    this.showIcon = true,
    this.compact = false,
  }) : assert(entry != null || category != null, 'Either entry or category must be provided');

  String get _category => entry?.category ?? category!;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final categoryColor = BloodPressureUtils.getCategoryColor(_category);
    final categoryIcon = BloodPressureUtils.getCategoryIcon(_category);
    final displayName = entry?.categoryDisplayName ?? _getCategoryDisplayName(_category);
    final description = BloodPressureUtils.getCategoryDescription(_category);

    if (compact) {
      return _buildCompactIndicator(theme, categoryColor, categoryIcon, displayName);
    }

    return Card(
      margin: EdgeInsets.zero,
      color: categoryColor.withOpacity(0.05),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
        side: BorderSide(color: categoryColor.withOpacity(0.3)),
      ),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              children: [
                if (showIcon) ...[
                  Container(
                    padding: const EdgeInsets.all(6),
                    decoration: BoxDecoration(
                      color: categoryColor.withOpacity(0.2),
                      shape: BoxShape.circle,
                    ),
                    child: Icon(
                      categoryIcon,
                      color: categoryColor,
                      size: 18,
                    ),
                  ),
                  const SizedBox(width: 12),
                ],
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        displayName,
                        style: theme.textTheme.titleSmall?.copyWith(
                          color: categoryColor,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      if (entry != null) ...[
                        const SizedBox(height: 2),
                        Text(
                          entry!.formatted,
                          style: theme.textTheme.bodyMedium?.copyWith(
                            color: theme.colorScheme.onSurface,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ],
                    ],
                  ),
                ),
                _buildStatusIndicator(categoryColor),
              ],
            ),
            if (showDescription) ...[
              const SizedBox(height: 8),
              Text(
                description,
                style: theme.textTheme.bodySmall?.copyWith(
                  color: theme.colorScheme.onSurfaceVariant,
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }

  Widget _buildCompactIndicator(
    ThemeData theme,
    Color categoryColor,
    IconData categoryIcon,
    String displayName,
  ) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: categoryColor.withOpacity(0.1),
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: categoryColor.withOpacity(0.3)),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (showIcon) ...[
            Icon(
              categoryIcon,
              color: categoryColor,
              size: 14,
            ),
            const SizedBox(width: 4),
          ],
          Text(
            displayName,
            style: theme.textTheme.bodySmall?.copyWith(
              color: categoryColor,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildStatusIndicator(Color categoryColor) {
    return Container(
      width: 12,
      height: 12,
      decoration: BoxDecoration(
        color: categoryColor,
        shape: BoxShape.circle,
      ),
    );
  }

  String _getCategoryDisplayName(String category) {
    switch (category) {
      case 'normal':
        return 'Normal';
      case 'elevated':
        return 'Elevated';
      case 'high_stage_1':
        return 'High Stage 1';
      case 'high_stage_2':
        return 'High Stage 2';
      case 'crisis':
        return 'Hypertensive Crisis';
      default:
        return 'Unknown';
    }
  }
}

/// Simplified widget for showing just the category status
class BloodPressureStatusDot extends StatelessWidget {
  final String category;
  final double size;

  const BloodPressureStatusDot({
    super.key,
    required this.category,
    this.size = 12,
  });

  @override
  Widget build(BuildContext context) {
    final categoryColor = BloodPressureUtils.getCategoryColor(category);

    return Container(
      width: size,
      height: size,
      decoration: BoxDecoration(
        color: categoryColor,
        shape: BoxShape.circle,
        boxShadow: [
          BoxShadow(
            color: categoryColor.withOpacity(0.3),
            blurRadius: 2,
            offset: const Offset(0, 1),
          ),
        ],
      ),
    );
  }
}

/// Quick category legend for reference
class BloodPressureCategoryLegend extends StatelessWidget {
  final bool isCompact;

  const BloodPressureCategoryLegend({
    super.key,
    this.isCompact = false,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final categories = [
      'normal',
      'elevated',
      'high_stage_1',
      'high_stage_2',
      'crisis',
    ];

    return Card(
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Blood Pressure Categories',
              style: theme.textTheme.titleSmall?.copyWith(
                fontWeight: FontWeight.w600,
              ),
            ),
            const SizedBox(height: 8),
            ...categories.map((category) {
              return Padding(
                padding: const EdgeInsets.symmetric(vertical: 2),
                child: Row(
                  children: [
                    BloodPressureStatusDot(category: category, size: 8),
                    const SizedBox(width: 8),
                    Text(
                      BloodPressureUtils.getCategoryColor(category) == Colors.green
                          ? 'Normal (< 120/80)'
                          : category == 'elevated'
                              ? 'Elevated (120-129/<80)'
                              : category == 'high_stage_1'
                                  ? 'High Stage 1 (130-139/80-89)'
                                  : category == 'high_stage_2'
                                      ? 'High Stage 2 (140+/90+)'
                                      : 'Crisis (180+/120+)',
                      style: theme.textTheme.bodySmall?.copyWith(
                        color: theme.colorScheme.onSurfaceVariant,
                      ),
                    ),
                  ],
                ),
              );
            }).toList(),
          ],
        ),
      ),
    );
  }
}
