package com.prank.sound.funnyapp.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.prank.sound.funnyapp.databinding.ActivitySplashBinding;
import com.prank.sound.funnyapp.max.MaxManager;
import com.prank.sound.funnyapp.max.WelComManager;
import com.prank.sound.funnyapp.mytool.Common;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

public class SplashActivity extends AppCompatActivity {

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

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

        MaxManager.onLoadAd();

        countDownTimer = WelComManager.initTimer(this, time, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                int percentage = (int) (100 - (float) aLong / time * 100);
                binding.loadingPb.setProgress(percentage);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                welcomeMain();
                return null;
            }
        });

        countDownTimer.start();
    }

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

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