package com.key.simplekeyboard.activity;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.app.wallpaper.max.WelComManager;
import com.key.simplekeyboard.R;
import com.key.simplekeyboard.tools.CustomProgressBar;

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

public class SplashActivity extends AppCompatActivity {


    private long timer = 12000;
    private CountDownTimer countDownTimer;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.splash_activity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        CustomProgressBar customProgressBar = findViewById(R.id.splash_pg);

         countDownTimer = WelComManager.initTimer(this, timer, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                float l1 = (float) aLong / timer;
                float v = 100 - l1 * 100;
                customProgressBar.setProgress((int) v);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                starMainactivity();
                return null;
            }
        });

    }


    private void starMainactivity() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

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