package com.ar.ardrawingboard.ui.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.ardrawingboard.R;
import com.ar.ardrawingboard.databinding.ActivitySplashBinding;
import com.ar.ardrawingboard.topon.AdManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

public class SplashActivity extends AppCompatActivity {
    private ActivitySplashBinding binding;
    private static final long totalTime = 11000;
    private CountDownTimer countDownTimer;

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

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

        AdManager.loadAllAd();
        AdManager.setAlreadyShow();

        Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .transform(new CenterCrop(),new RoundedCorners(32))
                .placeholder(R.mipmap.placeholder)
                .error(R.mipmap.placeholder)
                .into(binding.splashImage);

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