import 'package:flutter/material.dart';

import '../core/app_colors.dart';
import '../core/app_design_tokens.dart';
import '../core/app_module_visuals.dart';
import '../models/bp_reading.dart';

Future<void> showBleSyncDialog(
  BuildContext context, {
  required BpReading reading,
}) {
  return showDialog<void>(
    context: context,
    barrierDismissible: false,
    barrierColor: Colors.black.withValues(alpha: 0.54),
    builder: (ctx) => Dialog(
      insetPadding: const EdgeInsets.symmetric(horizontal: 28),
      backgroundColor: Colors.transparent,
      child: Container(
        width: double.infinity,
        constraints: const BoxConstraints(maxWidth: 380),
        padding: const EdgeInsets.fromLTRB(22, 26, 22, 22),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: AppRadius.xlBorder,
          boxShadow: AppShadows.floating,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              width: 48,
              height: 48,
              decoration: BoxDecoration(
                color: AppModuleVisuals.device.color,
                borderRadius: AppRadius.mdBorder,
              ),
              child: Icon(
                AppModuleVisuals.device.icon,
                color: Colors.white,
                size: 25,
              ),
            ),
            const SizedBox(height: 14),
            Text(
              '数据已录入',
              textAlign: TextAlign.center,
              style: AppTextStyles.summaryTitle.copyWith(fontSize: 22),
            ),
            const SizedBox(height: 22),
            Row(
              children: [
                Expanded(
                  child: _MetricCard(
                    label: '收缩压',
                    value: '${reading.systolic}',
                    unit: 'mmHg',
                  ),
                ),
                const SizedBox(width: 14),
                Expanded(
                  child: _MetricCard(
                    label: '舒张压',
                    value: '${reading.diastolic}',
                    unit: 'mmHg',
                  ),
                ),
              ],
            ),
            if (reading.pulse != null) ...[
              const SizedBox(height: 14),
              Row(
                children: [
                  Expanded(
                    child: _MetricCard(
                      label: '脉搏',
                      value: '${reading.pulse}',
                      unit: 'bpm',
                    ),
                  ),
                  const Expanded(child: SizedBox.shrink()),
                ],
              ),
            ],
            const SizedBox(height: 26),
            GestureDetector(
              onTap: () => Navigator.pop(ctx),
              child: Container(
                height: 50,
                width: double.infinity,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  gradient: AppModuleVisuals.device.gradient,
                  borderRadius: AppRadius.lgBorder,
                  boxShadow: AppColors.buttonShadow,
                ),
                child: Text(
                  '知道了',
                  style: AppTextStyles.button.copyWith(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

class _MetricCard extends StatelessWidget {
  final String label;
  final String value;
  final String unit;

  const _MetricCard({
    required this.label,
    required this.value,
    required this.unit,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16),
      decoration: BoxDecoration(
        color: AppModuleVisuals.device.lightColor,
        borderRadius: AppRadius.mdBorder,
        border: Border.all(color: AppModuleVisuals.device.borderColor),
      ),
      child: Column(
        children: [
          Text(
            value,
            style: const TextStyle(
              fontSize: 36,
              fontWeight: FontWeight.w700,
              color: AppColors.textPrimary,
              height: 1,
            ),
          ),
          const SizedBox(height: 6),
          Text(unit, style: AppTextStyles.tag),
          const SizedBox(height: 4),
          Text(label, style: AppTextStyles.tag),
        ],
      ),
    );
  }
}
