package it.jaschke.alexandria.changes;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.GestureDetector;
import android.view.ScaleGestureDetector;
import android.view.View;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import it.jaschke.alexandria.R;

/**
 * @author tham
 *
 * Activity to capture the Barcode using camera functionality.
 */
public class BarcodeCaptureActivity extends Activity {
    private static final String LOG_TAG = BarcodeCaptureActivity.class.getSimpleName();

    private static final int HANDLE_GMS = 9001;
    private static final int HANDLE_CAMERA_PERMISSION = 2;

    private static final String AUTOFOCUS = "AutoFocus";
    private static final String USEFLASH = "UseFlash";
    private static final String BARCODE_OBJECT = "Barcode";

    private CameraSource cameraSource;
    private CameraSourcePreview cameraSourcePreview;
    private GraphicOverlay<BarcodeGraphic> graphicOverlay;

    private ScaleGestureDetector scaleGestureDetector;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode);

        startCameraPreview();

        final BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build();
    }

    private void startCameraPreview() {
        Log.i(LOG_TAG, "Enters startCameraPreview()");

        cameraSourcePreview = (CameraSourcePreview) findViewById(R.id.preview);
        graphicOverlay = (GraphicOverlay<BarcodeGraphic>) findViewById(R.id.graphicOverlay);

        final boolean hasAutoFocus = getIntent().getBooleanExtra(AUTOFOCUS, false);
        final boolean canUseFlash = getIntent().getBooleanExtra(USEFLASH, false);

        final int cameraPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (cameraPermission == PackageManager.PERMISSION_GRANTED) {
            createCameraSource(hasAutoFocus, canUseFlash);
        } else {
            requestCameraPermission();
        }
    }

    private void requestCameraPermission() {
        Log.w(LOG_TAG, "Camera permission is NOT granted; Requesting permission");

        final String[] permissions = new String[] {Manifest.permission.CAMERA};
        if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
            ActivityCompat.requestPermissions(this, permissions, HANDLE_CAMERA_PERMISSION);
            return;
        }

        final Activity current = this;

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCompat.requestPermissions(current, permissions, HANDLE_CAMERA_PERMISSION);
            }
        };
    }

    private void createCameraSource(boolean hasAutoFocus, boolean canUseFlash) {
        Log.i(LOG_TAG, "Camera permission is already granted; Creating camera source");

        final Context context = getApplicationContext();

    }
}

@@ -119,16 +119,84 @@
 ontext;%0A
+import android.content.Intent;%0Aimport android.content.IntentFilter;%0A
 import a
@@ -430,16 +430,45 @@
 ew.View;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -512,16 +512,69 @@
 Source;%0A
+import com.google.android.gms.vision.MultiProcessor;%0A
 import c
@@ -3431,21 +3431,724 @@
 nContext();%0A
+        final BarcodeTrackerFactory trackerFactory = new BarcodeTrackerFactory(graphicOverlay);%0A        final BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();%0A        barcodeDetector.setProcessor(new MultiProcessor.Builder%3C%3E(trackerFactory).build());%0A%0A        if (!barcodeDetector.isOperational()) %7B%0A            Log.w(LOG_TAG, %22Detector dependencies are NOT yet available!%22);%0A        %7D%0A%0A        final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);%0A        boolean hasLowStorage = registerReceiver(null, intentFilter) != null;%0A        if (hasLowStorage) %7B%0A            Toast.makeText(this, %22Low storage%22, Toast.LENGTH_LONG).show();%0A        %7D
 %0A    %7D%0A%7D%0A

create camera source introduced