package com.phone.wall.paper.view;

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.phone.wall.paper.R;
import com.phone.wall.paper.databinding.UiWelcomeBinding;
import com.phone.wall.paper.tools.Utils;

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

public class Welcome extends AppCompatActivity {

    private CountDownTimer countDownTimer;
    private UiWelcomeBinding vb;

    private long totalTime = 12000;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = UiWelcomeBinding.inflate(getLayoutInflater());
        Utils.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);
                String string = getString(R.string.tv_wel_progress, countdownPercentage);
                vb.tvProgress.setText(string);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                vb.progressbar.setProgress(100);
                String string = getString(R.string.tv_wel_progress, 100);
                vb.tvProgress.setText(string);
                Intent intent = new Intent(Welcome.this, Main.class);
                startActivity(intent);
                finish();
                return null;
            }
        });
        countDownTimer.start();

    }



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