package uz.myid.sdk.sample;

import static uz.myid.android.sdk.capture.MyIdResultKt.takeUserResult;

import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.util.Locale;

import uz.myid.android.sdk.capture.MyIdBuildMode;
import uz.myid.android.sdk.capture.MyIdCameraShape;
import uz.myid.android.sdk.capture.MyIdClient;
import uz.myid.android.sdk.capture.MyIdConfig;
import uz.myid.android.sdk.capture.MyIdEntryType;
import uz.myid.android.sdk.capture.MyIdException;
import uz.myid.android.sdk.capture.MyIdResult;
import uz.myid.android.sdk.capture.MyIdResultListener;
import uz.myid.android.sdk.capture.model.OrganizationDetails;

public class ExampleJavaFragment extends Fragment implements MyIdResultListener {

    private final MyIdClient myIdClient = new MyIdClient();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startMyId();
    }

    @Override
    public void onSuccess(@NonNull MyIdResult result) {
        // Get face bitmap, result code, comparison value
    }

    @Override
    public void onError(@NonNull MyIdException e) {
        // Get error message and code
    }

    @Override
    public void onUserExited() {
        // User exited sdk
    }

    private void startMyId() {
        String clientId = "client_id";
        String passportData = "passport_data";
        String dateOfBirth = "date_of_birth";
        String externalId = "external_id";
        float threshold = 0.5f;

        OrganizationDetails organizationDetails = new OrganizationDetails(
                "1234567",
                R.drawable.image_logo
        );

        MyIdConfig myIdConfig = new MyIdConfig.Builder(clientId)
                .withPassportData(passportData)
                .withBirthDate(dateOfBirth)
                .withExternalId(externalId)
                .withThreshold(threshold)
                .withBuildMode(MyIdBuildMode.PRODUCTION)
                .withEntryType(MyIdEntryType.AUTH)
                .withLocale(new Locale("en"))
                .withCameraShape(MyIdCameraShape.CIRCLE)
                .withOrganizationDetails(organizationDetails)
                .withPhoto(false)
                .build();

        Intent intent = myIdClient.createIntent(requireActivity(), myIdConfig);
        result.launch(intent);
    }

    private final ActivityResultLauncher<Intent> result = takeUserResult(this, this);
}
