package com.atmob.voiceai.sdk.atmob.checker;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;

import com.atmob.checker.AtmobCheckCallback;
import com.atmob.checker.AtmobChecker;
import com.atmob.checker.AtmobCheckerConfig;
import com.atmob.checker.AtmobCheckerSdk;
import com.atmob.common.logging.AtmobLog;
import com.atmob.common.runtime.ContextUtil;
import com.atmob.user.AtmobUser;
import com.atmob.voiceai.BuildConfig;
import com.atmob.voiceai.data.consts.Constants;
import com.atmob.voiceai.sdk.kochava.KochavaHelper;

import java.util.concurrent.atomic.AtomicBoolean;

public class AtmobCheckerHelper {

    private static final String TAG = "AtmobCheckerHelper";

    private static final String KEY_ATMOB_CHECKER_RISK_LEVEL = "atmob_checker_risk_level";
    private static final AtomicBoolean isInit = new AtomicBoolean(false);
    private static Integer lastRiskLevel;

    public static boolean isInit() {
        return isInit.get();
    }

    public static void init(Context applicationContext) {
        if (!isInit.compareAndSet(false, true)) {
            return;
        }
        AtmobCheckerSdk.preInit(applicationContext, new AtmobCheckerConfig.Builder()
                .setDebug(BuildConfig.DEBUG)
                .setChannelName(AtmobUser.getAtmobChannel())
                .setHostUrl(Constants.Atmob_Server_Base_URL)
                .setAttributionInfoProvider(attributionInfoCallback ->
                        KochavaHelper.registerAttributionResultCallback(attributionInfoCallback::onAttributionInfoReceived))
                .build());
        AtmobCheckerSdk.initAfterGrantedAgreement();

        SharedPreferences sharedPreferences = applicationContext.getSharedPreferences("atmob_checker", Context.MODE_PRIVATE);
        lastRiskLevel = sharedPreferences.getInt(KEY_ATMOB_CHECKER_RISK_LEVEL, AtmobChecker.RiskLevel.SAFE);

        checkRuntime((riskLevel, riskInfo) -> {
            AtmobCheckerHelper.lastRiskLevel = riskLevel;
            cacheRiskLevel(applicationContext, riskLevel);
        });
    }

    private static void cacheRiskLevel(Context applicationContext, int riskLevel) {
        applicationContext.getSharedPreferences("atmob_checker", Context.MODE_PRIVATE)
                .edit()
                .putInt(KEY_ATMOB_CHECKER_RISK_LEVEL, riskLevel)
                .apply();
    }

    public static void checkRuntime(AtmobCheckCallback callback) {
        if (Constants.isOpenDeviceCheckPolicy) {
            Application application = ContextUtil.getApplication();
            AtmobCheckerSdk.getAtmobChecker().checkRuntime(application, (riskLevel, riskInfo) -> {
                AtmobLog.d(TAG, "onCheckResult: riskLevel = " + riskLevel + ", riskInfo = " + riskInfo);
                if (callback != null) {
                    callback.onCheckResult(riskLevel, riskInfo);
                }
            });
        } else {
            callback.onCheckResult(AtmobChecker.RiskLevel.SAFE, null);
        }
    }

    public static void onLocationPermissionGranted() {
        if (isInit()) {
            Application application = ContextUtil.getApplication();
            AtmobCheckerSdk.getAtmobChecker().onLocationPermissionGranted(application);
        }
    }

    @NonNull
    @AtmobChecker.RiskLevel
    public static Integer getLastRiskLevel(Context context) {
        if (lastRiskLevel == null) {
            lastRiskLevel = context.getSharedPreferences("atmob_checker", Context.MODE_PRIVATE)
                    .getInt(KEY_ATMOB_CHECKER_RISK_LEVEL, AtmobChecker.RiskLevel.SAFE);
        }
        AtmobLog.d(TAG, "getLastRiskLevel: lastRiskLevel = " + lastRiskLevel);
        return lastRiskLevel;
    }
}
