package com.ar.ardrawing.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ar.ardrawing.R;
import com.ar.ardrawing.databinding.ActivitySplashBinding;
import com.ar.ardrawing.topon.AdManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private static final long totalTime = 11000; // 倒计时总时间
    private CountDownTimer countDownTimer;

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

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

        AdManager.loadAllAd();

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

        AdManager.setAlreadyShow();

        countDownTimer = AdManager.showWelcomeAd(this, totalTime, millisUntilFinished -> {
                    int percentage = (int) (100 - (float) millisUntilFinished / totalTime * 100);
                    progressBar.setProgress(percentage);
                },
                this::startMain // 倒计时结束或广告展示结束时，跳转主界面
        );

        countDownTimer.start();
    }

    // 跳转到主界面的方法
    private void startMain() {
        // 确保进度条更新至满值
        binding.progressBar.setProgress(100);

        // 跳转到主界面
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

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

