package id.mdd.implementationpartnerconnect;

import android.content.Context;
import android.util.Log;

import com.mdd.aar.deviceid.AarDeviceId;
import com.mdd.aar.deviceid.DeviceEnvironment;
import com.mdd.aar.deviceid.exceptions.DeviceNotRegisteredException;
import com.mdd.topupUnik.ubal.ApiUtils;
import com.mdd.topupUnik.ubal.InitDebugCertificate;
import com.mdd.topupUnik.ubal.InitReader;
import com.medicom.dudikov.mybanklibrary.nativeLib;

import id.emhusnan.mdd.service.ConfigService;
import id.emhusnan.mdd.service.LibraryConfig;
import io.reactivex.Single;

public class RXReader {
        Single<InitReader> initEdcReader(Context context, int deviceDriver) {
        return Single.create(emitter -> {
            String accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJhdXRoLXNlcnZpY2U6MS4wLjAiLCJzdWIiOnsibmFtZSI6IlRlc3RTdGFnIiwidXNlcm5hbWUiOiJUZXN0U3RhZyIsImF1dGhfbGV2ZWwiOiJNRVJDSEFOVCIsIm1pZCI6IjIwZjFmMTNmODA1MGFjY2QxYjNjNzFlOWUyYTQ0NzU3In0sImlhdCI6MTU5MzQxMzM5MCwiZXhwIjoxNzUxMTk4MTUwLCJuYmYiOjE1OTM0MTMzOTJ9.E6UJMKha5YCbri4KRExDV4zeA1ZBfabpW-O6SInxxvfyTTYnUZCEu58STBGrOCL0RuxTFp5Y-XEyoa932fQcTpMBMsKkvDmGeftJ0hmjzAxAjHTfUxUyEa-HHF1whsM4YQUG9CesEBjU9IUcE6Rs-jykYxuryvvqqop5BoEIwO71qQBrq6DtpwjwFk9hbNt0EKs_HmCpySfXQxorfLMbAVJCHBYoo4UVymUfwUbps7hW6eQwNoBnt11O59CRPf6uUVIfCrsaaBMcJI-mc214-EMyl3RmMbqNl_f_0ITEJhMtK2zbUFZ8dMrGdZZgWJxiHdmzxqdaEPDVshYi9Uo31w";

            AarDeviceId aarDeviceId = new AarDeviceId(context);
            try {
                aarDeviceId.getDeviceId();
            } catch (DeviceNotRegisteredException e) {
                aarDeviceId.init(accessToken, DeviceEnvironment.PROD);
            }

            try {
                ConfigService configService = new ConfigService();
                configService.setMid("ba7db816e59612f8ce1630a095ef01c5");
                configService.setTid("1234");
                configService.setToken("5b423ef49cb029340d7231cace0de201");
                LibraryConfig config = new LibraryConfig(
                        configService.getMid(),
                        configService.getTid(),
                        configService.getToken(),
                        accessToken
                , ApiUtils.prod_environment);
                nativeLib nativeLibrary = new nativeLib(context, deviceDriver);

                InitDebugCertificate debugCertificate = new InitDebugCertificate();
                InitReader reader = debugCertificate.getDebug(nativeLibrary, context, deviceDriver, config, true);
                emitter.onSuccess(reader);
            } catch (Exception e) {
                emitter.onError(e);
            }
        });

    }
}
