import 'package:flutter/foundation.dart'
    show TargetPlatform, defaultTargetPlatform;
import 'package:flutter/material.dart';

import '../l10n/generated/app_localizations.dart';

/// PTT capability badge (gen2 v0.9.3 / SDD-091).
///
/// Renders the active PTT level + backend in the Voice Bar so the
/// user understands which input path is in effect. When the
/// resolved capability is `L0Focused` an info icon appears that
/// opens a per-platform explanation sheet describing why Global
/// PTT is not active and what the user can do to engage it.
class PttCapabilityBadge extends StatelessWidget {
  /// Construct a badge.
  const PttCapabilityBadge({
    super.key,
    required this.level,
    required this.backendId,
    required this.boundInputClass,
  });

  /// Resolved capability level as the bridge emits it
  /// (`L0Focused` / `L1WindowsHook` / `L2WindowsRawInput` /
  /// `L1MacOSEventTap` / `L1LinuxGnomeWaylandPortal`).
  final String level;

  /// Stable backend identifier (`focused`, `windows-raw-input`, …).
  final String backendId;

  /// Privacy-safe input class (`keyboard`, `mouse-side-button`,
  /// or empty when no binding is set).
  final String boundInputClass;

  bool get _isFocused => level == 'L0Focused';

  String _explainBodyForPlatform(AppL10n l10n) {
    switch (defaultTargetPlatform) {
      case TargetPlatform.windows:
        return l10n.pttCapabilityExplainGoGlobalWindows;
      case TargetPlatform.macOS:
        return l10n.pttCapabilityExplainGoGlobalMacos;
      case TargetPlatform.linux:
        return l10n.pttCapabilityExplainGoGlobalLinux;
      case TargetPlatform.iOS:
        return l10n.pttCapabilityExplainGoGlobalIos;
      default:
        return l10n.pttCapabilityExplainGoGlobalGeneric;
    }
  }

  void _openExplanationSheet(BuildContext context) {
    final l10n = AppL10n.of(context);
    showModalBottomSheet<void>(
      context: context,
      showDragHandle: true,
      isScrollControlled: true,
      builder: (sheetContext) {
        final theme = Theme.of(sheetContext);
        final maxHeight = MediaQuery.sizeOf(sheetContext).height * 0.72;
        return SafeArea(
          child: ConstrainedBox(
            constraints: BoxConstraints(maxHeight: maxHeight),
            child: SingleChildScrollView(
              padding: const EdgeInsets.fromLTRB(20, 4, 20, 24),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    l10n.pttCapabilityExplainTitle,
                    style: theme.textTheme.titleMedium,
                  ),
                  const SizedBox(height: 12),
                  Text(
                    l10n.pttCapabilityExplainFocusedHeading,
                    style: theme.textTheme.titleSmall,
                  ),
                  const SizedBox(height: 4),
                  Text(
                    l10n.pttCapabilityExplainFocusedBody,
                    style: theme.textTheme.bodyMedium,
                  ),
                  const SizedBox(height: 16),
                  Text(
                    _explainBodyForPlatform(l10n),
                    style: theme.textTheme.bodyMedium,
                  ),
                  const SizedBox(height: 16),
                  Align(
                    alignment: AlignmentDirectional.centerEnd,
                    child: TextButton(
                      onPressed: () => Navigator.of(sheetContext).pop(),
                      child: Text(l10n.closeAction),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppL10n.of(context);
    final theme = Theme.of(context);
    final badgeLabel = l10n.pttCapabilityBadge(level, backendId);
    final tooltipMessage = boundInputClass.isEmpty
        ? badgeLabel
        : '$badgeLabel\n($boundInputClass)';
    return Padding(
      padding: const EdgeInsets.only(bottom: 6),
      child: Tooltip(
        message: tooltipMessage,
        child: Row(
          children: [
            Icon(
              _isFocused ? Icons.crop_free : Icons.public,
              size: 14,
              color: theme.colorScheme.onSurfaceVariant,
            ),
            const SizedBox(width: 4),
            Expanded(
              child: Text(
                badgeLabel,
                style: theme.textTheme.bodySmall?.copyWith(
                  color: theme.colorScheme.onSurfaceVariant,
                ),
              ),
            ),
            if (_isFocused)
              IconButton(
                icon: const Icon(Icons.info_outline, size: 16),
                tooltip: l10n.pttCapabilityExplainTitle,
                visualDensity: VisualDensity.compact,
                onPressed: () => _openExplanationSheet(context),
              ),
          ],
        ),
      ),
    );
  }
}
