import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

import '../../app/route.dart';
import '../../provider/current_scan_provider.dart';

class InputScanScreen extends HookConsumerWidget {
  const InputScanScreen({super.key, required this.data});
  final InputScanPekerjaanProp data;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    print("DATAXSCAN : ${data.input} - ${data.tipe}");
    MobileScannerController cameraController = MobileScannerController();
    final hasilScan = useState<String>("");

    return Scaffold(
      appBar: AppBar(
        title: const Text('Scan Barcode'),
        actions: [
          IconButton(
            color: Colors.white,
            icon: ValueListenableBuilder(
              valueListenable: cameraController.torchState,
              builder: (context, state, child) {
                switch (state as TorchState) {
                  case TorchState.off:
                    return const Icon(Icons.flash_off, color: Colors.grey);
                  case TorchState.on:
                    return const Icon(Icons.flash_on, color: Colors.yellow);
                }
              },
            ),
            iconSize: 32.0,
            onPressed: () => cameraController.toggleTorch(),
          ),
          IconButton(
            color: Colors.white,
            icon: ValueListenableBuilder(
              valueListenable: cameraController.cameraFacingState,
              builder: (context, state, child) {
                switch (state as CameraFacing) {
                  case CameraFacing.front:
                    return const Icon(Icons.camera_front);
                  case CameraFacing.back:
                    return const Icon(Icons.camera_rear);
                }
              },
            ),
            iconSize: 32.0,
            onPressed: () => cameraController.switchCamera(),
          ),
        ],
      ),
      body: MobileScanner(
        // fit: BoxFit.contain,
        controller: cameraController,
        onDetect: (capture) {
          final List<Barcode> barcodes = capture.barcodes;
          for (final barcode in barcodes) {
            hasilScan.value = barcode.rawValue.toString();
            debugPrint('Barcode found! ${barcode.rawValue}');
          }

          if (hasilScan.value.isNotEmpty) {
            // set ke provider global
            ref
                .read(barcodeScanResult.notifier)
                .update((state) => hasilScan.value);

            if (data.tipe == 0) {
              Navigator.of(context).popAndPushNamed(
                inputPekerjaan,
                arguments: inputPekerjaanProp(0, 0),
              );
            } else {
              if (data.tipe == 1) {
                Navigator.of(context).popAndPushNamed(
                  inputPekerjaan,
                  arguments: inputPekerjaanProp(0, 1),
                );
              } else {
                if (data.tipe == 2) {
                  Navigator.of(context).popAndPushNamed(
                    inputPekerjaan,
                    arguments: inputPekerjaanProp(0, 2),
                  );
                }
              }
            }

            // Navigator.pop(context);
          }
        },
      ),
    );
  }
}
