package com.datarecovery.master.module.splash;


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

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

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.user.AtmobUser;
import com.datarecovery.master.App;
import com.datarecovery.master.databinding.ActivitySplashBinding;
import com.datarecovery.master.dialog.AgreementDialog;
import com.datarecovery.master.module.main.MainActivity;
import com.gyf.immersionbar.ImmersionBar;

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

    private AgreementDialog agreementDialog;
    private final int delayMillis = 2000;

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

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

    private void nextStep() {
        showMain();
    }

    private void showMain() {
        new Handler().postDelayed(this::goMain, delayMillis);
    }

    private void goMain() {
        MainActivity.start(this);
        finishAfterTransition();
    }

    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();
                    });
                }

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

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

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

}
