package com.tool.applockpro.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.tool.applockpro.ApplockSkin;
import com.tool.applockpro.R;
import com.tool.applockpro.databinding.ActivitySplashBinding;
import com.tool.applockpro.tool.Adcallback;
import com.tool.applockpro.tool.Mytools;

import java.util.List;

public class SplashActivity extends AppCompatActivity {
    ActivitySplashBinding binding;
    private CountDownTimer countDownTimer;
    private List<ATInterstitial> ads;

    private boolean isAlreadyShow = false;
    private float i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_splash), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        ads = ApplockSkin.lodAd();
        countDownTimer = new CountDownTimer(1000, 100) {
            @Override
            public void onTick(long l) {
                i++;
                binding.splashPg.setProgress(i);
                if (!isAlreadyShow) {
                    showAd(false);
                }
            }

            @Override
            public void onFinish() {
                if (!isAlreadyShow) {
                    showAd(true);
                }
            }
        };

        countDownTimer.start();


    }

    private void showAd(boolean go) {
        ATInterstitial mInterstitialAd = Mytools.onCache(ads);
        if (mInterstitialAd == null) {
            isAlreadyShow = false;
            if (go) {
                starMainactivity();
            }
        } else {
            isAlreadyShow = true;
            Mytools.setCallback(mInterstitialAd, new Adcallback() {
                @Override
                public void onShowFail(AdError ad) {
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                        countDownTimer = null;
                    }
                    starMainactivity();
                }

                @Override
                public void onAdHidden() {
                    isAlreadyShow = true;
                    starMainactivity();
                }
            });
            mInterstitialAd.show(this);
        }


    }

    private void starMainactivity() {
        Intent intent = new Intent(SplashActivity.this, PermissionActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}