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";
            String accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJhdXRoLXNlcnZpY2U6MS4wLjAiLCJzdWIiOnsibmFtZSI6Ik5haXJpbmRvIE1lcmNoYW50IiwidXNlcm5hbWUiOiJuYWlyaW5kbyIsImF1dGhfbGV2ZWwiOiJNRVJDSEFOVCIsIm1pZCI6IjI1MTNhM2JjMjc2YjdkYmEzZjc2MmEzNDQ4MDg1YWRmIn0sImlhdCI6MTYxMTEzNzc0NCwiZXhwIjoxNjExMjI0MTQ0LCJuYmYiOjE2MTExMzc3NDZ9.flOShSWrToezutnZspQddW7SJURiblH0HkP4x0IKfgRt-NC55BVB69RQABZLdvNjvZzoLRwEqgXgbYB7tlnqjKWg13HZbEVKBTcBDki0NJxi2WqG0EqqByj3yI92qQW7hXwHhuKOyHZrVwIZO14KuwY3AsYzAJM7LiClP3J-zNUQTT4-PG-TgHuu_Uk6DA8NnDRGkLmbB9BpaWzC10kVuRGkge9D4_qs-petEGEhpe48R4trk7IJ9rvk1ZWHugxRt9YX2gzZzJctJ3sFU0jVcdOJm_nsQ5GJ4TuNjpoMCHrNlyjClPl0bqGzPGU3AZWO6cF28durxgIM3rrF6S6ymA";
            AarDeviceId aarDeviceId = new AarDeviceId(context);
            try {
                aarDeviceId.getDeviceId();
            } catch (DeviceNotRegisteredException e) {
                aarDeviceId.init(accessToken, DeviceEnvironment.PROD);
            }

            try {
                ConfigService configService = new ConfigService();
                configService.setMid("f5a31563e8ce850305948529268bd970");
                configService.setTid("1234");
                configService.setToken("fa7dfd5b2711f0f18ef6b00feb815874");
                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);
            }
        });

    }
}
