import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
import '../services/language_services.dart';

class QRScanUtils {
    QRScanUtils._init();
  static QRScanUtils? _instance;
  static QRScanUtils get instance => _instance ??= QRScanUtils._init();

  Future<String> scanQR(BuildContext context) async {
    String barcodeScanRes;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
          '#ffffff', appLocalization(context).cancel_button_content, true, ScanMode.QR);
    } on PlatformException {
      barcodeScanRes = 'Failed to get platform version.';
    }
    return barcodeScanRes;
  }

  Map<String, dynamic> getQRData(String data) {
    if (data.isEmpty) {
      return {};
    } else {
      final parts = data.split('.');
      return {
        'group_id': parts.length == 2 ? parts[0] : '',
        'group_name': parts.length == 2 ? parts[1] : '',
      };
    }
  }
}