package com.picture.ar.drawing.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ad.toponlibrary.AdManager;
import com.picture.ar.drawing.databinding.ActivityWelcomeBinding;
import com.picture.ar.drawing.utils.ComUtils;

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

public class WelcomeActivity extends AppCompatActivity {

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


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


        countDownTimer =  AdManager.showWelcomeAd(this, time, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                int progressPercentage = (int) ((100 * aLong) / time);
                int Percentage = 100 - progressPercentage;
                binding.loadingPb.setProgress(Percentage);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                enterMain();
                return null;
            }
        });
        countDownTimer.start();
    }
    private void enterMain() {
        binding.loadingPb.setProgress(100);
        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

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