import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
// import 'package:image_gallery_saver/image_gallery_saver.dart';
// import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
// import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';
// import 'package:uco_mobile_poc/app/custom_widgets/custom_toasty.dart';
import 'package:screenshot/screenshot.dart';
// import 'package:screenshot/screenshot.dart';
// import 'package:share_plus/share_plus.dart';
import '../../../core/utils/logs_utils.dart';
import '../../../custom_widgets/custom_toasty.dart';
// import 'package:share_plus/share_plus.dart';



class QrSettingController extends GetxController {
  //
  //
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

  Barcode? result;
  //
  // QRViewController? qrController;

  ScreenshotController screenshotController = ScreenshotController();

  List<QRSetting> qrList = [
    QRSetting(name: "Muhammad Bilal", accountNumber: "MSISDN: ********** 2222"),
  ];

  Future saveAssetImage() async {
    Uint8List? image = await screenshotController.capture();

    if (image != null) {
      // await ImageGallerySaver.saveImage(image);
      Toasty.error("QR Image save successfully");
    }
  }

  void shareQRImage() async {
    try {
      //

      var tempDir = await getTemporaryDirectory();

      var image = await screenshotController.captureAndSave(tempDir.path);

      dp("Path ", image);

      if (image != null) {
        Share.shareXFiles([XFile(image)]);
      }

      //
    } catch (e) {
      if (kDebugMode) {
        print('Error sharing asset image: $e');
      }
      // Handle any errors
    }
  }

  void toggleFlash() {
    // qrController?.toggleFlash();
  }

  Future<bool?>? getFlashStatus() {
    // return qrController?.getFlashStatus() ?? Future.value(false);
  }

  Future<void> scanfromImage() async {

    // var imagePath = await ImagePicker().pickImage(source: ImageSource.gallery);

    // var result = await qrController?.scanQrcodeFromImage(imagePath!.path);

    // qrController?.pauseCamera();
    //
    // if (result != null && result.isNotEmpty) {
    //   Get.back(result: result.first);
    // } else {
    //   Get.back();
    //   Toasty.error("Please re scan data not fetch");
    // }
    //
    // dp("Result Image", result?.map((e) => e).toList());
    //
    // dp("Result Image", result?.length);
  }

  @override
  void dispose() {
    // qrController?.dispose();
    super.dispose();
  }

  // void onQRViewCreated(QRViewController p1) {
  //   //
  //
  //   qrController = p1;
  //
  //   update();
  //
  //   qrController?.scannedDataStream.listen((scanData) {
  //     result = scanData;
  //
  //     qrController?.pauseCamera();
  //
  //     dp("Barcode result", result?.code);
  //
  //     if (result != null) {
  //       Get.back(result: result!.code);
  //     }
  //
  //     //
  //   });
  // }
}

class QRSetting {
  String name = "";
  String accountNumber = "";

  QRSetting({this.name = "", this.accountNumber = ""});
}
