package com.fun.funnyprank.activity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.applovin.sdk.AppLovinSdk;
import com.fun.funnyprank.FunnyPrankSkin;
import com.fun.funnyprank.R;
import com.fun.funnyprank.tools.Adcallback;
import com.fun.funnyprank.tools.CustomProgressBar;
import com.fun.funnyprank.tools.Mytools;

import java.util.List;

public class SplashActivity extends AppCompatActivity {
    private CountDownTimer countDownTimer;

    private static List<MaxInterstitialAd> adsList;
    private boolean isAlreadyShow = false;
    private float i = 0;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        EdgeToEdge.enable(this);
        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;
        });

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

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

            @Override
            public void onFinish() {
                if (!isAlreadyShow) {
                    showAdOrStartView(true);
                }
            }
        };
        registerBroadcastReceiver();
    }

    private void showAdOrStartView(boolean go) {
        MaxInterstitialAd cachedAd = Mytools.onCache(adsList);
        if (cachedAd == null) {
            isAlreadyShow = false;
            if (go) {
                starMainactivity();
            }
        } else {
            isAlreadyShow = true;
            Mytools.setCallBcak(cachedAd, new Adcallback() {
                @Override
                public void onShowFail(MaxAd ad) {
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                        countDownTimer = null;
                    }
                    starMainactivity();
                }

                @Override
                public void onAdHidden() {
                    isAlreadyShow = true;
                    starMainactivity();
                }
            });
            cachedAd.showAd();
        }
    }

    private void registerBroadcastReceiver() {
        if (!FunnyPrankSkin.initSDK) {
            BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
//                    AppLovinSdk.getInstance( SplashActivity.this ).showMediationDebugger();
                    Log.e("zzj", "------------loadAndStartAd--------success");
                    loadAndStartAd();
                }
            };

            LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(FunnyPrankSkin.AD_BROADCAST));
        } else {
            Log.e("zzj", "------------loadAndStartAd--------fail");
            loadAndStartAd();
        }
    }

    private void loadAndStartAd() {
        adsList = Mytools.getAllAd();
        countDownTimer.start();
    }

    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();
            countDownTimer = null;
        }
    }

}