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.key.simplekeyboard.R;
import com.key.simplekeyboard.tools.CustomProgressBar;

public class SplashActivity extends AppCompatActivity {
    private CountDownTimer countDownTimer;


    private boolean isAlreadyShow = false;
    private float i = 0;

    @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 = new CountDownTimer(10100, 100) {
            @Override
            public void onTick(long l) {
                i++;
                customProgressBar.setProgress(i);
                if (!isAlreadyShow) {
                    showAd(false);
                }
            }

            @Override
            public void onFinish() {
                if (!isAlreadyShow) {
                    showAd(true);
                }
            }
        };
        countDownTimer.start();

    }

    private void showAd(boolean go) {
        if(go){
            starMainactivity();
        }



    }

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