import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flux/core/utils/extensions.dart';
import 'package:flux/features/attachments/blocs/attachments_bloc.dart';
import 'package:qr_flutter/qr_flutter.dart';

class QrUploadDialog extends StatelessWidget {
  final String deepLinkUrl;
  final String title;

  const QrUploadDialog({
    super.key,
    required this.deepLinkUrl,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    // Usiamo i colori del tema per renderlo coerente col tuo design
    final theme = Theme.of(context);

    return BlocListener<AttachmentsBloc, AttachmentsState>(
      listener: (context, state) {
        Navigator.of(context).pop();
      },
      listenWhen: (previous, current) =>
          previous.allFiles.length < current.allFiles.length,
      child: AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
        backgroundColor: theme.colorScheme.surface,
        title: Column(
          children: [
            Icon(
              Icons.qr_code_scanner,
              size: 48,
              color: theme.colorScheme.primary,
            ),
            const SizedBox(height: 16),
            Text(
              title,
              textAlign: TextAlign.center,
              style: const TextStyle(fontWeight: FontWeight.bold),
            ),
          ],
        ),
        content: SizedBox(
          height: 400,
          width: 350,
          child: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min, // Fondamentale per i dialog
              children: [
                const Text(
                  "Inquadra questo codice con la fotocamera del tuo telefono per scattare e caricare i documenti direttamente qui.",
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 14, color: Colors.grey),
                ),
                const SizedBox(height: 24),
                Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: Colors
                        .white, // Lo sfondo bianco salva la vita sui temi scuri
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: QrImageView(
                    data: deepLinkUrl,
                    version: QrVersions.auto,
                    size: 200.0,
                    //Opzionale: puoi metterci il logo di FLUX in mezzo!
                    embeddedImage: const AssetImage('assets/images/logo.png'),
                    embeddedImageStyle: const QrEmbeddedImageStyle(
                      size: Size(40, 40),
                    ),
                  ),
                ),
                const SizedBox(height: 16),
                Text(
                  "In attesa di file...",
                  style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                    color: theme.colorScheme.primary,
                  ),
                ),
                const SizedBox(height: 8),
                const LinearProgressIndicator(), // Per far capire che è "in ascolto"
              ],
            ),
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: Text(context.l10n.commonClose),
          ),
        ],
        actionsAlignment: MainAxisAlignment.center,
      ),
    );
  }
}
