package com.champtechnet.plugins.CardIO;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;
import android.app.*;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import io.card.payment.*;
import android.util.Log;
public class CardIO extends CordovaPlugin {
 private static final String LOG_TAG = "CardIO";
 private int MY_SCAN_REQUEST_CODE = 100; // arbitrary int
 private CallbackContext callbackContext;
 public static int cardNumb;

 @Override
 public boolean execute(String action, JSONArray args,
  CallbackContext callbackContext) throws JSONException {
  if (action.equalsIgnoreCase("scan")) {
    this.callbackContext = callbackContext;
   Log.v(LOG_TAG, "scan executed");
   Log.v(LOG_TAG, "card.io library version: " + CardIOActivity.sdkVersion() + "\nBuilt: " + CardIOActivity.sdkBuildDate());
   scan(callbackContext);
   return true;
  } else {
   Log.v(LOG_TAG, "failed execution");
   PluginResult plresult = new PluginResult(PluginResult.Status.INVALID_ACTION);
   callbackContext.sendPluginResult(plresult);
   return false;
  }
 }
 private void scan(final CallbackContext callbackContext) {
  JSONObject error = new JSONObject();
  Intent scanIntent = new Intent(cordova.getActivity(), CardIOActivity.class);
  // customize these values to suit your needs.
  scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true); // default: false
  scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false); // default: false
  scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_POSTAL_CODE, false); // default: false

  // MY_SCAN_REQUEST_CODE is arbitrary and is only used within this activity.
  cordova.setActivityResultCallback(this);
  cordova.getActivity().startActivityForResult(scanIntent, MY_SCAN_REQUEST_CODE);
  // PluginResult plResult = new PluginResult(PluginResult.Status.OK, "cardnum");
  // callbackContext.sendPluginResult(plResult);

 }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == MY_SCAN_REQUEST_CODE) {
      String resultDisplayStr;
      if (data != null && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) {
          CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);
          // Never log a raw card number. Avoid displaying it, but if necessary use getFormattedCardNumber()
          resultDisplayStr = "Card Number: " + scanResult.getRedactedCardNumber() + "\n";
          // passCard(scanResult.getFormattedCardNumber());
          // Do something with the raw number, e.g.:
          // myService.setCardNumber( scanResult.cardNumber );
          //super.passCard(scanResult.cardNumber);
          // PluginResult plResult = new PluginResult(PluginResult.Status.OK, "cardnum");
          // callbackContext.sendPluginResult(plResult);
          callbackContext.success(scanResult.cardNumber);
          if (scanResult.isExpiryValid()) {
              resultDisplayStr += "Expiration Date: " + scanResult.expiryMonth + "/" + scanResult.expiryYear + "\n";
          }

          if (scanResult.cvv != null) {
              // Never log or display a CVV
              resultDisplayStr += "CVV has " + scanResult.cvv.length() + " digits.\n";
          }

          if (scanResult.postalCode != null) {
              resultDisplayStr += "Postal Code: " + scanResult.postalCode + "\n";
          }
      }
      else {
          resultDisplayStr = "Scan was canceled.";
      }
      // do something with resultDisplayStr, maybe display it in a textView
      // resultTextView.setText(resultDisplayStr);
      Log.v(LOG_TAG, resultDisplayStr);
  }
 //  // else handle other activity results
 }

 public String passCard(String cardNumber, CallbackContext callbackContext) {
   PluginResult plResult = new PluginResult(PluginResult.Status.OK, "cardResult");
  //  Log.v(LOG_TAG, plResult);
   callbackContext.sendPluginResult(plResult);
   cordova.setActivityResultCallback(this);
   return cardNumber;
 }

}
