package com.example.musedroid.musedroid;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

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

import java.io.IOException;

public class QrShowActivity extends AppCompatActivity {
    private final int permissionCode = 101;

    Intent intent;
    SurfaceView cameraView;
    TextView qrInfo;
    CameraSource cameraSource;
    BarcodeDetector barcodeDetector;
    String[] perm = new String[]{Manifest.permission.CAMERA};
    Vibrator v;
    Context context;
    boolean foundFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_show);
        cameraView = (SurfaceView) findViewById(R.id.cameraView);
        qrInfo = (TextView) findViewById(R.id.qrTextView);
        ActivityCompat.requestPermissions(QrShowActivity.this, perm, permissionCode);
        context = this;
        Intent i = getIntent();
        foundFlag = i.getBooleanExtra("flag", false);


        v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
        barcodeDetector =
                new BarcodeDetector.Builder(this)
                        .setBarcodeFormats(Barcode.QR_CODE)
                        .build();

        cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .setRequestedPreviewSize(640, 480)
                .build();

        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(QrShowActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }

                    cameraSource.start(cameraView.getHolder());
                } catch (IOException ie) {
                    Log.e("CAMERA SOURCE", ie.getMessage());
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();
            }

        });

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();

                if (barcodes.size() != 0 && !foundFlag) {

                    // Vibrate for 500 milliseconds
                    v.vibrate(500);
                    qrInfo.post(new Runnable() {    // Use the post method of the TextView
                        // Update the TextView
                        public void run() {
                            qrInfo.setText(barcodes.valueAt(0).displayValue);
                            intent = new Intent(QrShowActivity.this, ExhibitShowActivity.class);
                            intent.putExtra("exhibitId", barcodes.valueAt(0).displayValue);
                            startActivity(intent);
                            finish();
                        }
                    });
                    foundFlag = true;
                }
            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        switch (requestCode) {
            case 101: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    try {
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        cameraSource.start(cameraView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.

                }
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}

@@ -1310,22 +1310,8 @@
 ew =
- (SurfaceView)
  fin
@@ -1342,67 +1342,8 @@
 w);%0A
-        qrInfo = (TextView) findViewById(R.id.qrTextView);%0A
     
@@ -3901,276 +3901,9 @@
 0);%0A
-                    qrInfo.post(new Runnable() %7B    // Use the post method of the TextView%0A                        // Update the TextView%0A                        public void run() %7B%0A                            qrInfo.setText(barcodes.valueAt(0).displayValue);%0A        
+%0A
     
@@ -3987,24 +3987,16 @@
 class);%0A
-        
         
@@ -4091,24 +4091,16 @@
         
-  
-      
 startAct
@@ -4134,24 +4134,16 @@
         
-        
 finish()
@@ -4148,57 +4148,8 @@
 ();%0A
-                        %7D%0A                    %7D);
 %0A   
@@ -5667,282 +5667,22 @@
    %7D
- else %7B%0A%0A                    // permission denied, boo! Disable the%0A                    // functionality that depends on this permission.%0A%0A                %7D%0A            %7D%0A%0A            // other 'case' lines to check for other%0A            // permissions this app might request
+%0A            %7D
 %0A   

fix qr issue not showing exhibit show page , because of invalid textview