package com.key.coolkeyboard.activity;

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

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 com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.databinding.ActivitySplashBinding;
import com.key.coolkeyboard.tool.Adcallback;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.topon.AdManager;

import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

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

    private boolean isAlreadyShow = false;
    private long totaltime = 1000L;

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

        EdgeToEdge.enable(this);
        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;
        });
        binding.splashTitle.setTypeface(Typeface.createFromAsset(getAssets(), "Baumans-Regular.ttf"));

        countDownTimer = AdManager.showWelcomeAd(this, totaltime, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                Float percentage = 100 - (float) aLong / totaltime * 100;
                binding.splashPg.setProgress(percentage);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                starMainactivity();
                return null;
            }
        });
        countDownTimer.start();

    }

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

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