package com.cpm.lorealbaautomation.dailyactivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gsonGetterSetter.JourneyPlan;
import com.google.zxing.BarcodeFormat;
import com.journeyapps.barcodescanner.BarcodeEncoder;
import java.util.Objects;


public class BaAssessmentActivity extends AppCompatActivity {
    private ImageView qrImage;
    private Lorealba_Database db;
    JourneyPlan jcp_Object = new JourneyPlan();

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ba_assessment);
        SharedPreferences preferences = PrefHelper.getPrefs(this);
        String date = preferences.getString(CommonString.KEY_DATE, "");
        String username = preferences.getString(CommonString.KEY_USERNAME, "");
        String counter_id = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        String couter_code = preferences.getString(CommonString.KEY_COUNTER_CODE, "");
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        qrImage = findViewById(R.id.qrImage);
        setSupportActionBar(toolbar);
        if (!CommonFunctions.isTablet(this)) {
            toolbar.setTitleTextAppearance(this, R.style.changestext_sizefor_mobile);
        }
        Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        db = new Lorealba_Database(this);
        db.open();
        jcp_Object = db.getBIDfromJCP(username, counter_id);
        setTitle("BA Assessment - " + date);
        String link = CommonString.BA_ASSESSMENT_URL + "c=" + couter_code + "&u=" + username;
        Log.e("link", link);
        generateQRCode(link);
    }

    @Override
    protected void onResume() {
        super.onResume();
        db.open();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
    }

    private void generateQRCode(String text) {
        try {
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            Bitmap bitmap = barcodeEncoder.encodeBitmap(
                    text,
                    BarcodeFormat.QR_CODE,
                    400,
                    400
            );
            qrImage.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }
}
