package id.mdd.implementationpartnerconnect;

import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bugfender.sdk.Bugfender;
import com.mdd.aar.deviceid.AarDeviceId;
import com.mdd.aar.deviceid.DeviceEnvironment;
import com.mdd.aar.deviceid.exceptions.DeviceNotRegisteredException;
import com.mdd.topupUnik.ubal.InitReader;
import com.medicom.dudikov.mybanklibrary.nativeLib;

import butterknife.BindView;
import id.emhusnan.mdd.service.ConfigService;
import id.emhusnan.mdd.service.MDDService;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyApp extends Application {
    String accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJhdXRoLXNlcnZpY2U6MS4wLjAiLCJzdWIiOnsibmFtZSI6IlRlc3RTdGFnIiwidXNlcm5hbWUiOiJUZXN0U3RhZyIsImF1dGhfbGV2ZWwiOiJNRVJDSEFOVCIsIm1pZCI6IjIwZjFmMTNmODA1MGFjY2QxYjNjNzFlOWUyYTQ0NzU3In0sImlhdCI6MTU5MzQxMzM5MCwiZXhwIjoxNzUxMTk4MTUwLCJuYmYiOjE1OTM0MTMzOTJ9.E6UJMKha5YCbri4KRExDV4zeA1ZBfabpW-O6SInxxvfyTTYnUZCEu58STBGrOCL0RuxTFp5Y-XEyoa932fQcTpMBMsKkvDmGeftJ0hmjzAxAjHTfUxUyEa-HHF1whsM4YQUG9CesEBjU9IUcE6Rs-jykYxuryvvqqop5BoEIwO71qQBrq6DtpwjwFk9hbNt0EKs_HmCpySfXQxorfLMbAVJCHBYoo4UVymUfwUbps7hW6eQwNoBnt11O59CRPf6uUVIfCrsaaBMcJI-mc214-EMyl3RmMbqNl_f_0ITEJhMtK2zbUFZ8dMrGdZZgWJxiHdmzxqdaEPDVshYi9Uo31w";

    int idDriver = nativeLib.USE_NFC_NATIVE;
    private InitReader reader;

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

    public void initAar(InitListener listener) {
        Bugfender.init(this, "apHTg4GcGupkx86oSnew7mm8cpbnwhAX", BuildConfig.DEBUG);
        Bugfender.enableCrashReporting();
        Bugfender.enableUIEventLogging(this);
        Bugfender.enableLogcatLogging();

        ConfigService configService = new ConfigService();
        configService.setMid("f5a31563e8ce850305948529268bd970");
        configService.setToken("fa7dfd5b2711f0f18ef6b00feb815874");
        if (idDriver == 1) {
    //        configService.setTid("1234");
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Please grant permission", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    AarDeviceId aarDeviceId = new AarDeviceId(this);
                    String deviceId = "";
                    try {
                        deviceId = aarDeviceId.getDeviceId();
                        Log.d("deviceId", deviceId);
                    } catch (DeviceNotRegisteredException e) {
                        aarDeviceId.init(accessToken, DeviceEnvironment.PROD);
                    }

                    configService.setTid(deviceId);

                    MDDService initialize = MDDService.with(this, configService);
                    if (initialize.isValid()) {
                        reader = initialize.getInitReader();
                        listener.onInitReaderComplete(reader);
                        Log.d("reader", String.valueOf(reader));
                        Log.d("MyApp", "--> Build Version");
                        Toast.makeText(this, "Aktivasi library sukses", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "Library belum aktif", Toast.LENGTH_SHORT).show();
                    }
                }
            } else {
                AarDeviceId aarDeviceId = new AarDeviceId(this);
                String deviceId = "";
                try {
                    deviceId = aarDeviceId.getDeviceId();
                } catch (DeviceNotRegisteredException e) {
                    aarDeviceId.init(accessToken, DeviceEnvironment.PROD);
                }

                configService.setTid(deviceId);

                MDDService initialize = MDDService.with(this, configService);
                if (initialize.isValid()) {
                    Log.d("MyApp", "--> aardeviceid");
                    reader = initialize.getInitReader();
                    Toast.makeText(this, "Aktivasi library sukses", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Library belum aktif", Toast.LENGTH_SHORT).show();
                }
            }

        } else {
            Log.d("MyApp", "--> Use Nfc Native");
            new RXReader().initEdcReader(this, nativeLib.USE_NFC_NATIVE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SingleObserver<InitReader>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onSuccess(InitReader initReader) {
                            listener.onInitReaderComplete(initReader);
                            reader = initReader;
                            Log.e("initReader", String.valueOf(initReader));
                        }


                        @Override
                        public void onError(Throwable e) {

                        }
                    });
            }
        }

    public InitReader getReader() {
        return reader;
    }
}
