import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_contacts/contact.dart';
import 'package:dialer/domain/services/contact_service.dart';

class QRCodeButton extends StatelessWidget {
  final List<Contact> contacts;
  final Contact? selfContact;

  const QRCodeButton({super.key, required this.contacts, this.selfContact});

  Contact? getSelfContact() {
    if (kDebugMode) {
      debugPrint("Checking for self contact");
    }
    for (var contact in contacts) {
      if (contact.groups.any((group) => group.name.toLowerCase() == "user")) {
        return contact;
      }
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.qr_code, color: selfContact != null ? Colors.blue : Colors.grey),
      onPressed: selfContact != null
          ? () {
              // Use the ContactService to show the QR code
              ContactService().showContactQRCodeDialog(context, selfContact!);
            }
          : null,
    );
  }
}
