package com.key.vibekeyboard.Activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.databinding.ActivitySplashBinding;


public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private static final long TOTAL_TIME = 3000;
    private CountDownTimer countDownTimer;

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

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

        ProgressBar progressBar = binding.progressBar;

        Glide.with(this)
                .load(R.mipmap.ic_launcher_foreground)
                .transform(new RoundedCorners(16))
                .into(binding.splashImage);

        countDownTimer = new CountDownTimer(TOTAL_TIME, 100) { // 每100ms更新一次
            @Override
            public void onTick(long millisUntilFinished) {
                // 更新进度条
                int percentage = (int) ((TOTAL_TIME - millisUntilFinished) * 100 / TOTAL_TIME);
                progressBar.setProgress(percentage);
            }

            @Override
            public void onFinish() {
                progressBar.setProgress(100);

                startMain();
            }
        };

        countDownTimer.start();
    }

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

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