package com.datarecovery.master;

import android.content.Context;

import com.atmob.app.lib.base.BaseApplication;
import com.atmob.channelreader.ChannelReader;
import com.atmob.common.runtime.ProcessUtil;
import com.atmob.compliance.AtmobCompliance;
import com.atmob.sdk.appconvert.AppConvert;
import com.atmob.sdk.gravity_engine.GravityEngine;
import com.atmob.user.AtmobUser;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.sdk.ad.AtmobAdHelper;
import com.datarecovery.master.sdk.bugly.BuglyHelper;
import com.datarecovery.master.sdk.central.AtmobCentralHelper;
import com.datarecovery.master.sdk.umeng.UmengHelper;
import com.datarecovery.master.utils.SplashHandler;
import com.datarecovery.master.utils.ToastUtil;
import com.didichuxing.doraemonkit.DoKit;

import java.util.Objects;

import dagger.hilt.android.HiltAndroidApp;

@HiltAndroidApp
public class App extends BaseApplication {


    private static App INSTANCE;


    public static App getInstance() {
        return INSTANCE;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        INSTANCE = this;
    }


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

    private void initDokit() {
        if (!BuildConfig.DEBUG) {
            return;
        }
        new DoKit.Builder(this)
                .build();
    }

    private void checkServerRuntime() {
        if (BuildConfig.DEBUG) {
            return;
        }
        if (!Objects.equals(BuildConfig.ENV, BuildConfig.PROD)) {
            ToastUtil.show("当前环境不是正式环境!", ToastUtil.LENGTH_LONG);
        }
    }

    @Override
    protected boolean isDebug() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected String defaultChannel() {
        return Constants.App_DefaultChannel;
    }

    @Override
    protected int defaultAppId() {
        return Constants.App_DefaultAppId;
    }

    @Override
    protected int defaultTgPlatformId() {
        return Constants.App_DefaultTgPlatformId;
    }

    @Override
    protected int complianceStrategy() {
        return AtmobUser.CHINA;
    }

    @Override
    protected void initCommon(boolean isMainProcess) {
        if (isMainProcess) {
            initBugly();
            initUmeng();
            initCentral();
            initAppConvert();
            initGravityEngine();
            initAtmobAd();
            initSplash();
        }
    }

    @Override
    public void initAfterGrant(boolean isMainProcess) {

    }

    private void initCentral() {
        ChannelReader.default4Test(
                Constants.App_DefaultChannel,
                Constants.App_DefaultAppId,
                Constants.App_DefaultTgPlatformId
        );
        String channel = ChannelReader.getChannel(this);
        int appId = ChannelReader.getAppId(this);
        int tgPlatformId = ChannelReader.getTgPlatformId(this);
        AtmobCentralHelper.init(this, channel, appId, tgPlatformId);
    }

    private void initUmeng() {
        UmengHelper.init(this);
    }

    private void initBugly() {
        BuglyHelper.init(this);
    }

    private void initAppConvert() {
        AppConvert.init();
    }

    private void initGravityEngine() {
        GravityEngine.init(BuildConfig.GRAVITY_ACCESS_TOKEN, BuildConfig.DEBUG);
    }

    public void firstPrivacyRelated() {
        AtmobUser.recordPolicyGrant(true);
        AtmobCentralHelper.onPolicyGranted(true);
        UmengHelper.initAfterGrantedAgreement(getApplicationContext());
        UmengHelper.submitPolicyGrantResult(getApplicationContext(), true);
        BuglyHelper.initAfterGrantedAgreement();
        initAfterGrant(ProcessUtil.isMainProcess(this));
    }

    private void initAtmobAd() {
        AtmobCompliance.addPolicyGrantListener(isGranted -> {
            if (isGranted) {
                AtmobAdHelper.init(this);
            }
        });
    }

    private void initSplash() {
        SplashHandler.init();
    }
}
