package com.newapp.paperwall.uiview;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ad.toponlibrary.AdManager;
import com.newapp.paperwall.databinding.ActivityStartBinding;
import com.newapp.paperwall.tools.UiUtils;

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

public class StartActivity extends AppCompatActivity {

    private CountDownTimer countDownTimer;
    private ActivityStartBinding vb;

    private long totalTime = 12000;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityStartBinding.inflate(getLayoutInflater());
        UiUtils.initFull(this,true);
        setContentView(vb.getRoot());

        countDownTimer = AdManager.showWelcomeAd(this, totalTime, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                int progressPercentage = (int) ((100 * aLong) / totalTime);

                int countdownPercentage = 100 - progressPercentage;
                vb.progressbar.setProgress(countdownPercentage);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                vb.progressbar.setProgress(100);
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                return null;
            }
        });
        countDownTimer.start();

    }



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