package com.haven.wallpapers.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.haven.wallpapers.R;
import com.haven.wallpapers.Utils.ReadFile;
import com.haven.wallpapers.topon.AdManager;
import com.haven.wallpapers.topon.CountAction;
import com.haven.wallpapers.topon.GoMainAction;

public class InitialActivity extends AppCompatActivity {
    private ProgressBar progressBar;
    private CountDownTimer countDownTimer;

    private static final long totalTime = 11000; // 倒计时总时间
    private static final String TAG = "InitialActivity"; // 日志标签

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_initial);

        ReadFile.initFull(InitialActivity.this, false);
        progressBar = findViewById(R.id.progressBar);

        progressBar.setProgress(0);

        countDownTimer = AdManager.showWelcomeAd(InitialActivity.this, totalTime, new CountAction() {
            @Override
            public void onCount(long millisUntilFinished) {
                int percentage = (int) (100 - (float) millisUntilFinished / totalTime * 100);
                progressBar.setProgress(percentage);
            }
        }, new GoMainAction() {
            @Override
            public void onGo() {
                startMain();
            }
        });
        countDownTimer.start();
    }

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

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在页面销毁时，清理倒计时资源
        if (countDownTimer != null) {
            countDownTimer.cancel();
            Log.d(TAG, "onDestroy: Countdown timer cancelled");
        }
    }
}
