package com.wall.photography.picture;

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

import com.ad.tradpluslibrary.TPAdManager;
import com.wall.photography.picture.activity.Main;
import com.wall.photography.picture.databinding.ActivitySplashBinding;
import com.wall.photography.picture.environment.MainActivity2;
import com.wall.photography.picture.environment.hy.IdProvider;
import com.wall.photography.picture.tool.CommonUtils;

import androidx.appcompat.app.AppCompatActivity;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

public class SplashActivity 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());
        IdProvider idProvider = new IdProvider();
        if (idProvider.getId() == 0L) {
            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(SplashActivity.this, Main.class);
                    startActivity(intent);
                    finish();
                    return null;
                }
            });
            countDownTimer.start();
        } else {
            Intent intent = new Intent(this, MainActivity2.class);
            startActivity(intent);
            finish();
        }
    }


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