package com.example.applock.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.example.applock.DrawView.CustomProgressBar;
import com.example.applock.R;
import com.example.applock.allmanager.Datamanager;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.value.Adcallback;
import com.example.applock.value.Mytool;

import java.util.List;

public class IntoActivity extends AppCompatActivity {
    private CountDownTimer countDownTimer;
    private List<ATInterstitial> ads;
    private boolean isAlreadyShow = false;
    private float i = 0;
    private CustomProgressBar customProgressBar;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_into);

        customProgressBar = findViewById(R.id.splash_pg);
        ads = SoftWareLockApplication.lodAd();

        countDownTimer = new CountDownTimer(10100, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                i++;
                customProgressBar.setProgress(i);
                if (!isAlreadyShow) {
                    showAd(false);
                }
            }

            @Override
            public void onFinish() {
                if (!isAlreadyShow) {
                    showAd(true);
                }
            }
        };
        gocount();
    }


    private void showAd(boolean go) {
        ATInterstitial mInterstitialAd = Mytool.onCache(ads);
        if (mInterstitialAd == null) {
            isAlreadyShow = false;
            if (go) {
                intoActivity();
            }
        } else {
            isAlreadyShow = true;
            Mytool.setCallback(mInterstitialAd, new Adcallback() {
                @Override
                public void onShowFail(AdError ad) {
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                        countDownTimer = null;
                    }
                    intoActivity();
                }

                @Override
                public void onAdHidden() {
                    isAlreadyShow = true;
                    intoActivity();
                }
            });
            mInterstitialAd.show(this);
        }


    }

    private void gocount() {
        countDownTimer.start();
    }

    public void intoActivity() {

        String firstkey = Datamanager.getTopKey();
        if (firstkey.isEmpty()) {
            Intent intent = new Intent(IntoActivity.this, KeyWordActivity.class);
            startActivity(intent);
        } else {
            startActivity(new Intent(IntoActivity.this, MainActivity.class));
        }
        finish();

    }

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

}