package com.sultan.plantdatabse;

import android.os.Vibrator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

// implements onClickListener for the onclick behaviour of button
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button scanBtn;
    TextView messageText, messageFormat;

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

        // referencing and initializing
        // the button and textviews
        scanBtn = findViewById(R.id.scanBtn);
        messageText = findViewById(R.id.textContent);
        messageFormat = findViewById(R.id.textFormat);

        // adding listener to the button
        scanBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // we need to create the object
        // of IntentIntegrator class
        // which is the class of QR library
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setPrompt(getString(R.string.scan_qr));
        intentIntegrator.setOrientationLocked(true);
        intentIntegrator.setCaptureActivity(CustomScannerActivity.class);
        intentIntegrator.initiateScan();
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

        // Get the Vibrator service
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

        // if the intentResult is null, toast a message as "cancelled"
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_SHORT).show();
                if (vibrator != null) {
                    vibrator.vibrate(100);
                }
            } else {
                // if the intentResult is not null, extract the dynamic part from the URL
                String scannedContent = intentResult.getContents();

                // Extract the dynamic part after "&" and display it in messageText
                String dynamicPart = extractDynamicPart(scannedContent);

                // Construct the full URL by appending the dynamic part to the base URL
                String fullUrl = "https://bhalumni.in/view/plant-database/entry/" + dynamicPart;

                // Open the WebView activity with the constructed URL
                openWebViewActivity(fullUrl);

                // Vibrate for 200 milliseconds on successful read
                if (vibrator != null) {
                    vibrator.vibrate(200);
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    // Custom method to extract the dynamic part after "&" in the URL
    private String extractDynamicPart(String url) {
        int index = url.lastIndexOf("&");
        if (index != -1 && index < url.length() - 1) {
            return url.substring(index + 1);
        }
        return "";
    }

    // Custom method to open WebView activity with the specified URL
    private void openWebViewActivity(String url) {
        Intent webViewIntent = new Intent(this, WebViewActivity.class);
        webViewIntent.putExtra("url", url);
        startActivity(webViewIntent);
    }
}