package com.prank.sounds.fart.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.prank.sounds.fart.tools.MyUtils;
import com.prank.sounds.fart.databinding.SplActBinding;
import com.prank.sounds.fart.topon.AdManager;

public class SplAct extends AppCompatActivity {

    private final long time = 11000;
    private SplActBinding binding;
    private CountDownTimer countDownTimer;

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

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

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

        countDownTimer.start();
    }

    private void welcomeMain() {
        binding.loadingPb.setProgress(100);
        Intent intent = new Intent(this, MainAct.class);
        startActivity(intent);
        finish();
    }

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