package com.datarecovery.master.module.splash;


import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.common.runtime.ActivityUtil;
import com.atmob.user.AtmobUser;
import com.datarecovery.master.App;
import com.datarecovery.master.data.consts.AdFuncId;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.databinding.ActivitySplashBinding;
import com.datarecovery.master.dialog.AgreementDialog;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.main.MainActivity;
import com.datarecovery.master.sdk.ad.AtmobAdHelper;
import com.datarecovery.master.sdk.ad.SplashListenerAdapter;
import com.gyf.immersionbar.ImmersionBar;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends BaseActivity<ActivitySplashBinding> {

    private AgreementDialog agreementDialog;

    @Override
    protected boolean shouldImmersion() {
        return true;
    }

    @Override
    protected void configImmersion(@NonNull ImmersionBar immersionBar) {
        immersionBar.statusBarDarkFont(false);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPolicy();
    }

    private void checkPolicy() {
        if (AtmobUser.isPolicyGranted()) {
            nextStep();
        } else {
            showPolicyDialog();
        }
    }

    private void showPolicyDialog() {
        if (agreementDialog == null) {
            agreementDialog = new AgreementDialog(this);
            agreementDialog.setAgreementAction(new AgreementDialog.AgreementAction() {
                @Override
                public void onAgree() {
                    binding.getRoot().post(() -> {
                        App.getInstance().firstPrivacyRelated();
                        nextStep();

                        EventHelper.report(EventId.EVENT_3030000);
                    });
                }

                @Override
                public void onDisagree() {
                    finishAfterTransition();
                }
            });
        }
        agreementDialog.show();
    }

    private void nextStep() {
        if (ActivityUtil.isActivityExist(MainActivity.class)) {
            // hot start
            hotLaunchAd();
        } else {
            // cold start
            coldLaunchAd();
        }
    }

    private void coldLaunchAd() {
        showSplashAd(AdFuncId.SPLASH_COLD_LAUNCH_1, () -> showSplashAd(AdFuncId.SPLASH_COLD_LAUNCH_2, this::goMain));
    }

    private void hotLaunchAd() {
        showSplashAd(AdFuncId.SPLASH_HOT_LAUNCH, this::goMain);
    }

    private void showSplashAd(int adFuncId, Runnable action) {
        AtmobAdHelper.showSplash(adFuncId, binding.clLayout, new SplashListenerAdapter() {
            @Override
            public void action() {
                super.action();
                if (action != null) {
                    action.run();
                }
            }
        });
    }

    private void goMain() {
        if (!ActivityUtil.isActivityExist(MainActivity.class)) {
            MainActivity.start(this);
        }
        finishAfterTransition();
    }

    @Override
    public void onBackPressed() {
    }
}
