package com.auto.clicker.autoclicker.ui.activity.welcome;

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

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.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.databinding.ActivitySplashBinding;
import com.auto.clicker.autoclicker.ui.activity.main.MainActivity;
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 TOTAL_TIME = 100;
    private CountDownTimer countDownTimer;

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

        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        EdgeToEdge.enable(this);
        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;
        });

        Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .transform(new RoundedCorners(32))
                .into(binding.image);

        countDownTimer = new CountDownTimer(TOTAL_TIME,100) {
            @Override
            public void onTick(long millisUntilFinished) {
                int percentage = (int) (100 - (float) millisUntilFinished / TOTAL_TIME * 100);
                binding.progress.setProgress(percentage);
            }

            @Override
            public void onFinish() {
                startMain();
            }
        };

        countDownTimer.start();
    }

    private void startMain() {
        binding.progress.setProgress(100);

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

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