package com.nutomic.syncthingandroid.activities;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.nutomic.syncthingandroid.R;

import java.util.List;

public class QRScannerActivity extends ThemedAppCompatActivity implements BarcodeCallback {

    // region === Static ===
    static final String QR_RESULT_ARG = "QR_CODE";
    static Intent intent(Context context) {
        return new Intent(context, QRScannerActivity.class);
    }
    // endregion

    private final int RC_HANDLE_CAMERA_PERM = 888;

    private DecoratedBarcodeView barcodeView;

    // region === Activity Lifecycle ===
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_qr_scanner);

        this.barcodeView = findViewById(R.id.bar_code_scanner_view);

        findViewById(R.id.cancel_button).setOnClickListener(view -> {
            pauseScanner();
            finish();
        });

        checkPermissionAndStartScanner();
    }
    // endregion

    // region === Permissions Callback ===
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == RC_HANDLE_CAMERA_PERM) {
            if (grantResults.length !=0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startScanner();
            } else {
                finish();
            }
        }
    }
    // endregion

    // region === BarcodeCallback ===
    @Override
    public void barcodeResult(BarcodeResult result) {
        String code = result.getText();
        Intent intent = new Intent();
        intent.putExtra(QR_RESULT_ARG, code);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {
        // Unused
    }
    // endregion

    // region === Private Methods ===
    private void checkPermissionAndStartScanner() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            String[] permissions = {Manifest.permission.CAMERA};
            ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
        } else {
            startScanner();
        }
    }

    private void startScanner() {
        this.barcodeView.resume();
        this.barcodeView.decodeSingle(this);
    }

    private void pauseScanner() {
        this.barcodeView.pause();
    }
    // endregion
}

@@ -1492,42 +1492,22 @@
     
-pauseScanner();%0A            finish
+finishScanning
 ();%0A
@@ -1558,32 +1558,133 @@
 Scanner();%0A    %7D
+%0A%0A    @Override%0A    protected void onStop() %7B%0A        super.onStop();%0A        finishScanning();%0A    %7D
 %0A    // endregio
@@ -2426,32 +2426,40 @@
 ;%0A        finish
+Scanning
 ();%0A    %7D%0A%0A    @
@@ -3148,20 +3148,22 @@
 oid 
-pause
+finish
 Scann
-er
+ing
 () %7B
@@ -3193,24 +3193,42 @@
 ew.pause();%0A
+        finish();%0A
     %7D%0A    //

Stop camera when exiting QR scan with back button (fixes #1614) (#1641)