package com.study.sketch.learning.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ad.maxlibrary.WelComManager;
import com.study.sketch.learning.R;
import com.study.sketch.learning.databinding.ActivitySplashBinding;
import com.study.sketch.learning.manager.Common;

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

public class ActivitySplash extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private long time = 14000;
    private CountDownTimer countDownTimer;



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

        countDownTimer = WelComManager.initTimer(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);
                binding.tvProgress.setText(getString(R.string.text_progress,Percentage));
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                enterMain();
                return null;
            }
        });

    }
    private void enterMain() {
        binding.tvProgress.setText(getString(R.string.text_progress,100));
        binding.loadingPb.setProgress(100);
        Intent intent = new Intent(ActivitySplash.this, ActivityMain.class);
        startActivity(intent);
        finish();
    }

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