package com.tool.applockpro.activity;

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

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

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.tool.applockpro.ApplockSkin;
import com.tool.applockpro.R;
import com.tool.applockpro.data.MyDataBase;
import com.tool.applockpro.data.Mydata;
import com.tool.applockpro.databinding.ActivitySplashBinding;
import com.tool.applockpro.tool.Adcallback;
import com.tool.applockpro.tool.Mytools;
import com.tool.applockpro.topon.AdManager;

import java.util.List;

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

public class SplashActivity extends AppCompatActivity {
    ActivitySplashBinding binding;
    private CountDownTimer countDownTimer;
    private float i = 0;

    private long time = 12000;

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

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_splash), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        countDownTimer = AdManager.showWelcomeAd(SplashActivity.this, time, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                Float percentage = 100 - (float) aLong / time * 100;
                binding.splashPg.setProgress(percentage);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {

                starMainactivity();
                return null;
            }
        });
        countDownTimer.start();

    }


    private void starMainactivity() {
        binding.splashPg.setProgress(100);
        if (Mytools.hasUsagePermission(this) && Mytools.hasOverlayPermission(this)) {
            if(ApplockSkin.getPWD().isEmpty()){
                Intent intent = new Intent(SplashActivity.this, SetPWDActivity.class);
                startActivity(intent);
            }else {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);

            }
        } else {
            Intent intent = new Intent(SplashActivity.this, PermissionActivity.class);
            startActivity(intent);
        }
        finish();
    }

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