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;

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

    private static final long totalTime = 3000; // 倒计时总时间
    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 = new CountDownTimer(totalTime,100) {
            @Override
            public void onTick(long millisUntilFinished) {
                int percentage = (int) (100 - (float) millisUntilFinished / totalTime * 100);
                progressBar.setProgress(percentage);
            }

            @Override
            public void onFinish() {
                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");
        }
    }
}
