package com.draw.painting.test.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

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.draw.painting.test.Adcallback;
import com.draw.painting.test.PaintingApp;
import com.draw.painting.test.databinding.ActivityAlaucherBinding;
import com.draw.painting.test.manager.Utils;

import java.util.List;

public class ALaucherActivity extends AppCompatActivity {

    private ActivityAlaucherBinding binding;
    private long time = 11000;
    private CountDownTimer countDownTimer;
    private static List<MaxInterstitialAd> adsList;
    public static boolean isAlreadyShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAlaucherBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Utils.setStatusBarTextColor(this, true);


        countDownTimer = new CountDownTimer(time, 200) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (!isAlreadyShow) {
                    showAdOrStartView(false);
                }
            }

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

            }
        };
        registerBroadcastReceiver();
    }

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

                }

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

    private void registerBroadcastReceiver() {
        if (!PaintingApp.initSDK) {
            BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    loadAndStartAd();
                }
            };

            LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(PaintingApp.AD_BROADCAST));
        } else {
            loadAndStartAd();
        }
    }

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


    private void enterMain() {
        Intent intent = new Intent(ALaucherActivity.this, HomeActivity.class);
        startActivity(intent);
        finish();
    }

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