package com.wall.exquisite.wallpaper;

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

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.ad.tradpluslibrary.TPAdManager;

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

public class StartuppageActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    // 倒计时总时长（毫秒），这里设置1500ms=1.5秒，和你原来的逻辑一致
    private static final long TOTAL_COUNT_DOWN_TIME = 1500;
    // 倒计时间隔（毫秒），每15ms更新一次进度，和原逻辑匹配
    private static final long COUNT_DOWN_INTERVAL = 15;
    private CountDownTimer countDownTimer;
    private long totalTime = 14000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_startuppage);

        // 初始化控件
        progressBar = findViewById(R.id.progressBar);
        TPAdManager.INSTANCE.init(
                this,
                WallpaperApplication.TAG,
                "11AFE68410E60AB7D990BD9CBA814411",
                "83D1FF0E14FCB01E5F26038F91114512",
                "BB1B407D3DC75FE8C6E3205D2E6B3E12",
                "FB5CA4DF6B924ADF06C3C0C25C880212",
                new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        return null;
                    }
                }
        ) ;

        // 初始化并启动CountDownTimer倒计时
        initCountDownTimer();

        // 适配系统状态栏（保留原有逻辑）
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    /**
     * 初始化CountDownTimer，实现倒计时和进度条更新
     */
    private void initCountDownTimer() {
        // CountDownTimer参数说明：
        // 第一个参数：总倒计时时长（ms）
        // 第二个参数：倒计时间隔（ms），每隔这个时间回调一次onTick
        countDownTimer = TPAdManager.INSTANCE.showWelcomeAd(this, totalTime, aLong -> {
            Long progressPercentage = (100 * aLong) / totalTime;
            Long percentage = 100 - progressPercentage;
            progressBar.setProgress(Integer.parseInt(percentage.toString()));
            return null;
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                // 倒计时结束，设置进度条为100
                progressBar.setProgress(100);
                // 跳转到MainActivity2
                Intent intent = new Intent(StartuppageActivity.this, FirstMainActivity.class);
                startActivity(intent);
                finish(); // 关闭启动页，避免返回键回到此页面
                return null;
            }
        }) ;
        countDownTimer.start();
    }

    /**
     * 防止内存泄漏：页面销毁时取消倒计时
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel(); // 取消倒计时
        }
    }
}