package com.exquisite.activity;

import androidx.activity.EdgeToEdge;
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 android.view.View;

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.exquisite.KeyboardSkin;
import com.exquisite.callback.Adcallback;
import com.exquisite.R;

import com.exquisite.tools.CustomProgressBar;
import com.exquisite.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);
        EdgeToEdge.enable(this);
        View splashView = findViewById(R.id.splashactivity);
        ViewCompat.setOnApplyWindowInsetsListener(splashView, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

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

        ads = KeyboardSkin.lodAd();

        countDownTimer = new CountDownTimer(11000, 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) {
                goMain();
            }
        } else {
            if (mInterstitialAd.isAdReady()) {
                isAlreadyShow = true;
                Mytools.setCallback(mInterstitialAd, new Adcallback() {
                    @Override
                    public void onShowFail(AdError ad) {
//                        isAlreadyShow = false;
                        if (countDownTimer != null) {
                            countDownTimer.cancel();
                            countDownTimer = null;
                        }
                        goMain();
                    }

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


    }

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


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