package com.rnbiometrics;

import android.util.Base64;

import androidx.annotation.NonNull;
import androidx.biometric.BiometricPrompt;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;

import java.security.Signature;

public class CreateSignatureCallback extends BiometricPrompt.AuthenticationCallback {
    private Promise promise;
    private String payload;
    private int type;

    public static final int TYPE_BASE64 = 1;
    public static final int TYPE_STRING = 0;
    public CreateSignatureCallback(Promise promise, String payload, int type) {
        super();
        this.promise = promise;
        this.payload = payload;
        this.type   = type;
    }

    @Override
    public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
        super.onAuthenticationError(errorCode, errString);
        WritableMap resultMap = new WritableNativeMap();
        resultMap.putBoolean("success", false);
        resultMap.putString("message", errString.toString());
        resultMap.putInt("code", errorCode);
        promise.resolve(resultMap);
    }

    @Override
    public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
        super.onAuthenticationSucceeded(result);

        try {
            BiometricPrompt.CryptoObject cryptoObject = result.getCryptoObject();
            Signature cryptoSignature = cryptoObject.getSignature();
            if (type == TYPE_BASE64) {
                byte[] byteb64 = Base64.decode(this.payload,0);
                cryptoSignature.update(byteb64);
            }else{
                cryptoSignature.update(this.payload.getBytes());
            }
            byte[] signed = cryptoSignature.sign();
            String signedString = Base64.encodeToString(signed, Base64.DEFAULT);
            signedString = signedString.replaceAll("\r", "").replaceAll("\n", "");

            WritableMap resultMap = new WritableNativeMap();
            resultMap.putBoolean("success", true);
            resultMap.putString("signature", signedString);
            resultMap.putString("message", "Success!");
            promise.resolve(resultMap);
        } catch (Exception e) {
            WritableMap resultMap = new WritableNativeMap();
            resultMap.putBoolean("success", false);
            resultMap.putString("message", "Error creating signature: " + e.getMessage());
            promise.resolve(resultMap);
        }
    }
}
