package com.example.puntobip;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
/* loaded from: classes.dex */
public class MainActivity extends Activity {
    private final String MONTO_DEFAULT = "2000";
    private IntentFilter[] mFilters;
    private NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;
    private String[][] mTechList;
    private TapDialog tapDialog;

    @Override // android.app.Activity
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.tapDialog = new TapDialog(this, getResources().getString(R.string.consultar_saldo)).display();
        inicializarNFC();
    }

    public void onCargarSaldo(View view) {
        String txtSaldo = ((TextView) findViewById(R.id.tvSaldoBIP)).getText().toString().replace("$", "");
        TapDialog tapDialog = new TapDialog(this, getResources().getString(R.string.cargar_saldo));
        if (txtSaldo.isEmpty()) {
            txtSaldo = "2000";
        }
        this.tapDialog = tapDialog.displayCargar(txtSaldo);
    }

    public void onCambiarNumero(View view) {
        TextView tvNumeroBIP = (TextView) findViewById(R.id.tvNumeroBIP);
        this.tapDialog = new TapDialog(this, getResources().getString(R.string.cambiar_numero)).displayNumero(tvNumeroBIP.getText().toString());
    }

    private void inicializarNFC() {
        this.mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (this.mNfcAdapter == null) {
            Toast.makeText(this, "Este dispositivo no soporta NFC.", 1).show();
            finish();
            return;
        }
        if (!this.mNfcAdapter.isEnabled()) {
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("NFC deshabilitado");
            alertDialog.setMessage("Su NFC esta deshabilitado.\nIngrese a las opciones de configuracion y activelo.");
            alertDialog.show();
        }
        this.mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(536870912), 0);
        IntentFilter ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
        try {
            ndef.addDataType("*/*");
            this.mFilters = new IntentFilter[]{ndef};
            this.mTechList = new String[][]{new String[]{MifareClassic.class.getName()}};
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("error", e);
        }
    }

    @Override // android.app.Activity
    protected void onResume() {
        super.onResume();
        this.mNfcAdapter.enableForegroundDispatch(this, this.mPendingIntent, this.mFilters, this.mTechList);
    }

    @Override // android.app.Activity
    protected void onPause() {
        super.onPause();
        this.mNfcAdapter.disableForegroundDispatch(this);
    }

    @Override // android.app.Activity
    protected void onNewIntent(Intent intent) {
        String action = intent.getAction();
        if ("android.nfc.action.TECH_DISCOVERED".equals(action)) {
            Tag tagFromIntent = (Tag) intent.getParcelableExtra("android.nfc.extra.TAG");
            MifareManager mifare = new MifareManager(tagFromIntent);
            try {
                try {
                    mifare.open();
                    if (this.tapDialog.isShowing()) {
                        procesarOperacion(this.tapDialog, mifare);
                    }
                    desplegarDatosTarjeta(mifare);
                    if (this.tapDialog != null) {
                        this.tapDialog.dismiss();
                    }
                    mifare.close();
                } catch (AuthenticateException e) {
                    Log.e("error", "error autenticando sector", e);
                    Toast.makeText(this, "error autenticando sector " + e.getSector(), 1).show();
                    if (this.tapDialog != null) {
                        this.tapDialog.dismiss();
                    }
                    mifare.close();
                } catch (Exception e2) {
                    Log.e("error", "error desconocido", e2);
                    Toast.makeText(this, e2.getMessage(), 1).show();
                    if (this.tapDialog != null) {
                        this.tapDialog.dismiss();
                    }
                    mifare.close();
                }
            } catch (Throwable th) {
                if (this.tapDialog != null) {
                    this.tapDialog.dismiss();
                }
                mifare.close();
                throw th;
            }
        }
    }

    private void desplegarDatosTarjeta(MifareManager mifare) throws IOException, AuthenticateException {
        TextView tvNumeroUUID = (TextView) findViewById(R.id.tvNumeroUUID);
        TextView tvNumeroBIP = (TextView) findViewById(R.id.tvNumeroBIP);
        TextView tvSaldoBIP = (TextView) findViewById(R.id.tvSaldoBIP);
        tvNumeroUUID.setText(Utils.leToNumericString(mifare.getTag().getId(), 4));
        byte[] data = mifare.readBlock(1);
        tvNumeroBIP.setText(Utils.leToNumericString(new byte[]{data[4], data[5], data[6], data[7]}, 4));
        tvSaldoBIP.setText(Utils.formatMoneda(Utils.leToNumeric(mifare.readBlock(33), 2)));
    }

    private void procesarOperacion(TapDialog tapDialog, MifareManager mifare) throws IOException, AuthenticateException {
        int montoCargar = 0;
        switch (tapDialog.getTag()) {
            case 2:
                String txtMontoCargar = ((EditText) tapDialog.findViewById(R.id.etMontoCargar)).getText().toString();
                if (txtMontoCargar.length() != 0) {
                    montoCargar = Integer.parseInt(txtMontoCargar);
                }
                mifare.writeValue(33, montoCargar);
                mifare.writeValue(34, montoCargar);
                return;
            case 3:
                String txtNumeroBip = ((EditText) tapDialog.findViewById(R.id.etNumeroBip)).getText().toString();
                long numeroBip = txtNumeroBip.length() == 0 ? 0L : Long.parseLong(txtNumeroBip);
                byte[] data = mifare.readBlock(1);
                data[4] = (byte) (numeroBip >>> 0);
                data[5] = (byte) (numeroBip >>> 8);
                data[6] = (byte) (numeroBip >>> 16);
                data[7] = (byte) (numeroBip >>> 24);
                mifare.writeBlock(1, data);
                return;
            default:
                return;
        }
    }
}
