package com.sound.prankparty.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import com.app.wallpaper.max.WelComManager;
import com.sound.prankparty.databinding.ActivitySplashBinding;
import com.sound.prankparty.max.MaxManager;

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

public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private long timer = 12000;
    private CountDownTimer countDownTimer;

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

        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        MaxManager.onLoadAd();

        // 使用 ViewBinding 引用 progressBar
        ProgressBar progressBar = binding.progressBar;


        countDownTimer = WelComManager.initTimer(this, timer, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                float l1 = (float) aLong / timer;
                float v = 100 - l1 * 100;
                progressBar.setProgress((int) v);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                startMainActivity();
                return null;
            }
        });
    }

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

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


}
