package de.schkola.kitchenscanner.util;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TorchManager {

    private ScheduledExecutorService s;
    private CameraManager manager;
    private AVCallback av;
    private TCallback t;

    public TorchManager(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            s = Executors.newSingleThreadScheduledExecutor();
            while (manager == null) {
                manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            }
            av = new AVCallback();
            manager.registerAvailabilityCallback(av, null);
            t = new TCallback();
            manager.registerTorchCallback(t, null);
        }
    }

    public void shutdown() {
        //Reset torch mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            s.shutdownNow();
            manager.unregisterAvailabilityCallback(av);
            manager.unregisterTorchCallback(t);
            try {
                manager.setTorchMode("0", false);
            } catch (CameraAccessException ignored) {
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    private class AVCallback extends CameraManager.AvailabilityCallback {

        @Override
        public void onCameraAvailable(@NonNull String cameraId) {
            if (cameraId.equals("0")) {
                try {
                    manager.setTorchMode("0", true);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    private class TCallback extends CameraManager.TorchCallback {

        @Override
        public void onTorchModeChanged(@NonNull String cameraId, boolean enabled) {
            if (s.isShutdown()) {
                return;
            }
            if (cameraId.equals("0") && enabled) {
                s.schedule(() -> {
                    try {
                        manager.setTorchMode("0", false);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }, 150, TimeUnit.MILLISECONDS);
            }
        }
    }
}

@@ -426,24 +426,78 @@
 hManager %7B%0A%0A
+    private static final long TORCH_WAIT_TIME = 150;%0A%0A
     private 
@@ -521,17 +521,31 @@
 Service 
-s
+executorService
 ;%0A    pr
@@ -564,17 +564,23 @@
 Manager 
-m
+cameraM
 anager;%0A
@@ -596,20 +596,48 @@
 te A
-VCallback av
+vailabilityCallback availabilityCallback
 ;%0A  
@@ -647,16 +647,20 @@
 rivate T
+orch
 Callback
@@ -661,16 +661,28 @@
 llback t
+orchCallback
 ;%0A%0A    p
@@ -793,17 +793,31 @@
         
-s
+executorService
  = Execu
@@ -876,17 +876,23 @@
  while (
-m
+cameraM
 anager =
@@ -913,25 +913,31 @@
             
-m
+cameraM
 anager = (Ca
@@ -1031,17 +1031,45 @@
   av
- = new AV
+ailabilityCallback = new Availability
 Call
@@ -1080,33 +1080,39 @@
 ();%0A            
-m
+cameraM
 anager.registerA
@@ -1133,16 +1133,34 @@
 lback(av
+ailabilityCallback
 , null);
@@ -1177,16 +1177,28 @@
    t
+orchCallback
  = new T
 Call
@@ -1193,16 +1193,20 @@
  = new T
+orch
 Callback
@@ -1217,25 +1217,31 @@
             
-m
+cameraM
 anager.regis
@@ -1258,16 +1258,28 @@
 llback(t
+orchCallback
 , null);
@@ -1426,17 +1426,31 @@
         
-s
+executorService
 .shutdow
@@ -1461,33 +1461,39 @@
 ();%0A            
-m
+cameraM
 anager.unregiste
@@ -1516,16 +1516,34 @@
 lback(av
+ailabilityCallback
 );%0A     
@@ -1545,25 +1545,31 @@
             
-m
+cameraM
 anager.unreg
@@ -1588,16 +1588,28 @@
 llback(t
+orchCallback
 );%0A     
@@ -1629,33 +1629,39 @@
                 
-m
+cameraM
 anager.setTorchM
@@ -1727,24 +1727,60 @@
  ignored) %7B%0A
+                // Empty on purpose%0A
             
@@ -1857,17 +1857,27 @@
  class A
-V
+vailability
 Callback
@@ -2081,33 +2081,39 @@
                 
-m
+cameraM
 anager.setTorchM
@@ -2329,16 +2329,20 @@
  class T
+orch
 Callback
@@ -2499,17 +2499,31 @@
     if (
-s
+executorService
 .isShutd
@@ -2636,17 +2636,31 @@
         
-s
+executorService
 .schedul
@@ -2719,17 +2719,23 @@
         
-m
+cameraM
 anager.s
@@ -2901,19 +2901,31 @@
      %7D, 
-150
+TORCH_WAIT_TIME
 , TimeUn

Reformat TorchManager