package com.trace.ar.drawing.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.trace.ar.drawing.databinding.ActivityWelcomeBinding;
import com.trace.ar.drawing.helper.Utils;
import com.trace.ar.drawing.topon.AdManager;

public class MySplashActivity extends AppCompatActivity {

    private ActivityWelcomeBinding binding;
    private long time = 11000;
    private CountDownTimer countDownTimer;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Utils.setStatusBarTextColor(this, true);

        AdManager.loadAllAd();

        ProgressBar progressBar = binding.loadingPb;

        AdManager.setAlreadyShow();

        countDownTimer = AdManager.showWelcomeAd(this, time, millisUntilFinished -> {
                    int percentage = (int) (100 - (float) millisUntilFinished / time * 100);
                    progressBar.setProgress(percentage);
                },
                this::enterMain
        );

        countDownTimer.start();
    }
    private void enterMain() {
        binding.loadingPb.setProgress(100);
        Intent intent = new Intent(MySplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

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