package com.key.simplekeyboard.activity;

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

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.key.simplekeyboard.R;
import com.key.simplekeyboard.SimpleKeyboardSkins;
import com.key.simplekeyboard.tools.Adcallback;
import com.key.simplekeyboard.tools.CustomProgressBar;
import com.key.simplekeyboard.tools.Mytools;

import java.util.List;

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

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

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.splash_activity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        ads = SimpleKeyboardSkins.lodAd();

        CustomProgressBar customProgressBar = findViewById(R.id.splash_pg);

        countDownTimer = new CountDownTimer(10100, 100) {
            @Override
            public void onTick(long l) {
                i++;
                customProgressBar.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, MainActivity.class);
        startActivity(intent);
        finish();
    }
}