package com.wall.photography.picture.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ad.tradpluslibrary.TPAdManager;
import com.wall.photography.picture.MyApp;
import com.wall.photography.picture.databinding.ActivitySplashBinding;
import com.wall.photography.picture.tool.CommonUtils;

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

public class Splash extends AppCompatActivity {

    private CountDownTimer countDownTimer;
    private ActivitySplashBinding vb;

    private long totalTime = 13000;

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

        TPAdManager.INSTANCE.init(
                this,
                MyApp.TAG,
                "EA88C446DC518123368740F48AFF2211",
                "0010352AA641462D65A2B02050D47212",
                "0D33D11E182C3FBF8F7DB6BC84CD1312",
                "ECA0F6C0B3B407E78D5BCAD9483AA112", new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        return null;
                    }
                }
        ) ;
        countDownTimer = TPAdManager.INSTANCE.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(Splash.this, Main.class);
                startActivity(intent);
                finish();
                return null;
            }
        }) ;
        countDownTimer.start();

    }



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