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.AppManager;
import com.example.applock.allmanager.Datamanager;
import com.example.applock.allmanager.LockAction;
import com.example.applock.allmanager.MyWorker;
import com.example.applock.allmanager.SoftLockPermission;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;
import com.example.applock.value.Adcallback;
import com.example.applock.value.MyAllValues;
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 AppEntity entity;
    private CustomProgressBar customProgressBar;

    private long countTime = 13100;
    private String lockpackname = MyAllValues.softWareLockApplication.getPackageName();

    @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(countTime, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                float percentage = 100 -   (float)millisUntilFinished  / countTime * 100;
                int round = (int)percentage;
                customProgressBar.setProgress(round);
                if (!isAlreadyShow) {
                    showAd(false);
                }
            }

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


    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);
        }
    }

    public void intoActivity() {
        String firstkey = Datamanager.getTopKey();
        if (firstkey.isEmpty()) {
            Intent intent = new Intent(IntoActivity.this, KeyWordActivity.class);
            startActivity(intent);
            finish();
        } else {
            if (SoftLockPermission.allAllowPermission(this)){
                SoftWareLockRoom.RUNIO(new Runnable() {
                    @Override
                    public void run() {
                        entity = AppDataBase.getDatabaseInstance().getAppEntityDao().queryByPagName(lockpackname);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                entity.setApplocked(true);
                                SoftWareLockRoom.RUNIO(new Runnable() {
                                    @Override
                                    public void run() {
                                        AppDataBase.getDatabaseInstance().getAppEntityDao().updateData(entity);
                                    }
                                });
                                startActivity(new Intent(IntoActivity.this, MainActivity.class));
                                finish();
                            }
                        });
                    }
                });
            }else {
                startActivity(new Intent(IntoActivity.this, MainActivity.class));
                finish();
            }
        }
    }

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

}