package com.ar.arsketch.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ar.arsketch.R;
import com.ar.arsketch.databinding.ActivitySplashBinding;
import com.ar.arsketch.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 TOTAL_TIME = 11000;
    private CountDownTimer countDownTimer;

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

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

        AdManager.loadAllAd();

        ProgressBar progressBar = binding.progressBar;

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

        AdManager.setAlreadyShow();

        countDownTimer = AdManager.showWelcomeAd(this, TOTAL_TIME, millisUntilFinished -> {
                    int percentage = (int) (100 - (float) millisUntilFinished / TOTAL_TIME * 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();
        }
        binding = null;
    }
}
