import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:krow_domain/krow_domain.dart';
import 'package:core_localization/core_localization.dart';

/// A dialog for identifying and assigning an NFC tag to a hub.
class IdentifyNfcDialog extends StatefulWidget {
  /// The hub to assign the tag to.
  final Hub hub;

  /// Callback when a tag is assigned.
  final Function(String nfcTagId) onAssign;

  /// Callback when the dialog is cancelled.
  final VoidCallback onCancel;

  /// Creates an [IdentifyNfcDialog].
  const IdentifyNfcDialog({
    required this.hub,
    required this.onAssign,
    required this.onCancel,
    super.key,
  });

  @override
  State<IdentifyNfcDialog> createState() => _IdentifyNfcDialogState();
}

class _IdentifyNfcDialogState extends State<IdentifyNfcDialog> {
  String? _nfcTagId;

  void _simulateNFCScan() {
    setState(() {
      _nfcTagId =
          'NFC-${DateTime.now().millisecondsSinceEpoch.toString().substring(8).toUpperCase()}';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: UiColors.bgOverlay,
      child: Center(
        child: SingleChildScrollView(
          child: Container(
            width: MediaQuery.of(context).size.width * 0.9,
            padding: const EdgeInsets.all(UiConstants.space5),
            decoration: BoxDecoration(
              color: UiColors.bgPopup,
              borderRadius: BorderRadius.circular(UiConstants.radiusBase),
              boxShadow: const <BoxShadow>[
                BoxShadow(color: UiColors.popupShadow, blurRadius: 20),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text(
                  t.client_hubs.nfc_dialog.title,
                  style: UiTypography.headline3m.textPrimary,
                ),
                const SizedBox(height: UiConstants.space8),
                Container(
                  width: 80,
                  height: 80,
                  decoration: const BoxDecoration(
                    color: UiColors.tagInProgress,
                    shape: BoxShape.circle,
                  ),
                  child: const Icon(
                    UiIcons.nfc,
                    size: 40,
                    color: UiColors.primary,
                  ),
                ),
                const SizedBox(height: UiConstants.space4),
                Text(widget.hub.name, style: UiTypography.body1b.textPrimary),
                const SizedBox(height: UiConstants.space2),
                Text(
                  t.client_hubs.nfc_dialog.instruction,
                  textAlign: TextAlign.center,
                  style: UiTypography.body2m.textSecondary,
                ),
                const SizedBox(height: UiConstants.space5),
                UiButton.secondary(
                  onPressed: _simulateNFCScan,
                  text: t.client_hubs.nfc_dialog.scan_button,
                  leadingIcon: UiIcons.nfc,
                ),
                if (_nfcTagId != null) ...<Widget>[
                  const SizedBox(height: UiConstants.space6),
                  Container(
                    padding: const EdgeInsets.all(UiConstants.space4),
                    decoration: BoxDecoration(
                      color: UiColors.tagSuccess,
                      borderRadius: BorderRadius.circular(
                        UiConstants.radiusBase,
                      ),
                    ),
                    child: Column(
                      children: <Widget>[
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            const Icon(
                              UiIcons.success,
                              size: 20,
                              color: UiColors.textSuccess,
                            ),
                            const SizedBox(width: UiConstants.space2),
                            Text(
                              t.client_hubs.nfc_dialog.tag_identified,
                              style: UiTypography.body2b.textPrimary,
                            ),
                          ],
                        ),
                        const SizedBox(height: UiConstants.space2),
                        Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: UiConstants.space3,
                            vertical: UiConstants.space2,
                          ),
                          decoration: BoxDecoration(
                            color: UiColors.white,
                            borderRadius: BorderRadius.circular(
                              UiConstants.radiusMdValue + 2,
                            ),
                            border: Border.all(color: UiColors.border),
                          ),
                          child: Text(
                            _nfcTagId!,
                            style: UiTypography.footnote1b.copyWith(
                              fontFamily: 'monospace',
                              color: UiColors.textPrimary,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
                const SizedBox(height: UiConstants.space8),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: UiButton.secondary(
                        onPressed: widget.onCancel,
                        text: t.common.cancel,
                      ),
                    ),
                    const SizedBox(width: UiConstants.space3),
                    Expanded(
                      child: UiButton.primary(
                        onPressed: _nfcTagId != null
                            ? () => widget.onAssign(_nfcTagId!)
                            : null,
                        text: t.client_hubs.nfc_dialog.assign_button,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
