package com.app.rewardapp.adsManager;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.adcolony.sdk.AdColony;
import com.adcolony.sdk.AdColonyAdOptions;
import com.adcolony.sdk.AdColonyAppOptions;
import com.adcolony.sdk.AdColonyInterstitial;
import com.adcolony.sdk.AdColonyInterstitialListener;
import com.adcolony.sdk.AdColonyZone;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.facebook.ads.RewardedVideoAd;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
import com.startapp.sdk.adsbase.Ad;
import com.startapp.sdk.adsbase.StartAppAd;
import com.startapp.sdk.adsbase.adlisteners.AdEventListener;
import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAds;
import com.unity3d.ads.UnityAdsShowOptions;

public class RewardAds implements AdImp {
    public static class Builder {
        private static final String TAG = "AdNetwork__Rewarded";
        private String admob_au, max_au, unity_au, adcolonyZone, adcolony_Appid;
        boolean admob_reward, unity_reward, adcolony_reward, applovin_reward, statartapp_reward, unityadLoaded, adnotLoadedCredit, instantReload;
        private final int interval = 1;
        private int retryAttempt = 1;
        private final int counter = 1;
        public boolean isCompleted, rewardMode;
        Activity a;
        RewardedAd ar;
        RewardedVideoAd fr;
        MaxRewardedAd mr;
        StartAppAd sr;
        boolean isAdLoaded;
        private AdColonyInterstitial adcolonyInt;
        private AdColonyInterstitialListener adColonyInterstitialListener;

        public Builder(Activity act) {
            this.a = act;
        }

        public String getAdmob_au() {
            return admob_au;
        }

        public void setAdmob_au(String admob_au) {
            this.admob_au = admob_au;
        }

        public String getMax_au() {
            return max_au;
        }

        public void setMax_au(String max_au) {
            this.max_au = max_au;
        }

        public String getUnity_au() {
            return unity_au;
        }

        public void setUnity_au(String unity_au) {
            this.unity_au = unity_au;
        }

        public boolean isAdLoaded() {
            return isAdLoaded;
        }

        public void setAdLoaded(boolean adLoaded) {
            isAdLoaded = adLoaded;
        }

        public boolean isUnity_reward() {
            return unity_reward;
        }

        public void setUnity_reward(boolean unity_reward) {
            this.unity_reward = unity_reward;
        }

        public boolean isAdcolony_reward() {
            return adcolony_reward;
        }

        public void setAdcolony_reward(boolean adcolony_reward) {
            this.adcolony_reward = adcolony_reward;
        }

        public boolean isInstantReload() {
            return instantReload;
        }

        public void setInstantReload(boolean instantReload) {
            this.instantReload = instantReload;
        }

        public boolean isApplovin_reward() {
            return applovin_reward;
        }

        public void setApplovin_reward(boolean applovin_reward) {
            this.applovin_reward = applovin_reward;
        }

        public boolean isRewardMode() {
            return rewardMode;
        }

        public void setRewardMode(boolean rewardMode) {
            this.rewardMode = rewardMode;
        }

        public boolean isStatartapp_reward() {
            return statartapp_reward;
        }

        public void setStatartapp_reward(boolean statartapp_reward) {
            this.statartapp_reward = statartapp_reward;
        }

        public int getInterval() {
            return interval;
        }

        public int getRetryAttempt() {
            return retryAttempt;
        }

        public void setRetryAttempt(int retryAttempt) {
            this.retryAttempt = retryAttempt;
        }

        public int getCounter() {
            return counter;
        }

        public Activity getA() {
            return a;
        }

        public RewardedAd getAr() {
            return ar;
        }

        public void setAr(RewardedAd ar) {
            this.ar = ar;
        }

        public RewardedVideoAd getFr() {
            return fr;
        }

        public void setFr(RewardedVideoAd fr) {
            this.fr = fr;
        }

        public MaxRewardedAd getMr() {
            return mr;
        }

        public void setMr(MaxRewardedAd mr) {
            this.mr = mr;
        }

        public StartAppAd getSr() {
            return sr;
        }

        public void setSr(StartAppAd sr) {
            this.sr = sr;
        }

        public Builder buildAd() {
            loadReward();
            return this;
        }

        public boolean isCompleted() {
            return isCompleted;
        }

        public void setCompleted(boolean completed) {
            isCompleted = completed;
        }

        public void setA(Activity a) {
            this.a = a;
        }

        public boolean isAdmob_reward() {
            return admob_reward;
        }

        public void setAdmob_reward(boolean admob_reward) {
            this.admob_reward = admob_reward;
        }

        public String getAdcolonyZone() {
            return adcolonyZone;
        }

        public String getAdcolony_Appid() {
            return adcolony_Appid;
        }

        public boolean isUnityadLoaded() {
            return unityadLoaded;
        }

        public void setAdcolonyZone(String adcolonyZone) {
            this.adcolonyZone = adcolonyZone;
        }

        public void setAdcolony_Appid(String adcolony_Appid) {
            this.adcolony_Appid = adcolony_Appid;
        }

        public void setUnityadLoaded(boolean unityadLoaded) {
            this.unityadLoaded = unityadLoaded;
        }

        public boolean isAdnotLoadedCredit() {
            return adnotLoadedCredit;
        }

        public void setAdnotLoadedCredit(boolean adnotLoadedCredit) {
            this.adnotLoadedCredit = adnotLoadedCredit;
        }

        private void loadReward() {
            if (isAdmob_reward() && getAdmob_au() != null) {
                AdRequest adRequest = new AdRequest.Builder().build();
                RewardedAd.load(a, admob_au,
                        adRequest, new RewardedAdLoadCallback() {
                            @Override
                            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                Log.d("ADMOB_REWARD__", loadAdError.toString());
                            }

                            @Override
                            public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
                                setAdLoaded(true);
                                ar=rewardedAd;
                                rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                                    @Override
                                    public void onAdClicked() {
                                    }

                                    @Override
                                    public void onAdDismissedFullScreenContent() {
                                    }

                                    @Override
                                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                                        Log.e("TAG", "Ad failed to show fullscreen content.");
                                    }

                                    @Override
                                    public void onAdImpression() {
                                        // Called when an impression is recorded for an ad.
                                        Log.d("TAG", "Ad recorded an impression.");
                                    }

                                    @Override
                                    public void onAdShowedFullScreenContent() {
                                        // Called when ad is shown.
                                        Log.d("TAG", "Ad showed fullscreen content.");
                                    }
                                });
                                Log.d("TAG", "Ad was loaded.");
                            }
                        });
            }

            if (!isAdLoaded && isUnity_reward() && getUnity_au() != null) {
                UnityAds.load(unity_au, new IUnityAdsLoadListener() {
                    @Override
                    public void onUnityAdsAdLoaded(String placementId) {
                        setAdLoaded(true);
                        setUnityadLoaded(true);
                        Log.d(TAG, "unity interstitial ad loaded");
                    }

                    @Override
                    public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
                        Log.e(TAG, "Unity Ads failed to load ad for " + placementId + " with error: [" + error + "] " + message);
                    }
                });
            }

            if (!isAdLoaded && isApplovin_reward() && getMax_au() != null) {
                mr = MaxRewardedAd.getInstance(max_au, a);
                mr.setListener(new MaxRewardedAdListener() {
                    @Override
                    public void onRewardedVideoStarted(MaxAd ad) {

                    }

                    @Override
                    public void onRewardedVideoCompleted(MaxAd ad) {

                    }

                    @Override
                    public void onUserRewarded(MaxAd ad, MaxReward reward) {
                    }

                    @Override
                    public void onAdLoaded(MaxAd ad) {
                        setAdLoaded(true);
                    }

                    @Override
                    public void onAdDisplayed(MaxAd ad) {
                        new Handler().postDelayed(() -> {
                            if (rewardMode) {
                               setCompleted(true);
                            }

                        }, 5000);
                    }

                    @Override
                    public void onAdHidden(MaxAd ad) {

                    }

                    @Override
                    public void onAdClicked(MaxAd ad) {

                    }

                    @Override
                    public void onAdLoadFailed(String adUnitId, MaxError error) {
                        Log.e(TAG, "onAdLoadFailed: " + error);
                    }

                    @Override
                    public void onAdDisplayFailed(MaxAd ad, MaxError error) {

                    }
                });
                mr.loadAd();
            }

            if (!isAdLoaded && isAdcolony_reward() && getAdcolony_Appid() != null && getAdcolonyZone() != null) {
                AdColonyAppOptions appOptions = new AdColonyAppOptions().setGDPRConsentString("1")
                        .setKeepScreenOn(true).setGDPRRequired(true);
                AdColony.configure(a, appOptions, adcolony_Appid, adcolonyZone);
                AdColonyAdOptions options = new AdColonyAdOptions().enableConfirmationDialog(false).enableResultsDialog(false);
                adColonyInterstitialListener = new AdColonyInterstitialListener() {
                    @Override
                    public void onRequestFilled(AdColonyInterstitial adColonyInterstitial) {
                        setAdLoaded(true);
                        adcolonyInt = adColonyInterstitial;
                    }

                    @Override
                    public void onRequestNotFilled(AdColonyZone zone) {
                        Log.e(TAG, "onRequestNotFilled: " + zone);
                    }

                    @Override
                    public void onClosed(AdColonyInterstitial ad) {
                        if (rewardMode) {
                            setCompleted(true);
                        }

                    }
                };
                AdColony.requestInterstitial(adcolonyZone, adColonyInterstitialListener, options);
            }

            if (isStatartapp_reward()) {
                sr = new StartAppAd(a);
                sr.setVideoListener(() -> {
                    if (rewardMode) {
                        setCompleted(true);
                    }
                });
                sr.loadAd(StartAppAd.AdMode.REWARDED_VIDEO, new AdEventListener() {
                    @Override
                    public void onReceiveAd(Ad ad) {
                    }

                    @Override
                    public void onFailedToReceiveAd(Ad ad) {
                    }
                });
            }
        }

        public void showReward() {
            if (isAdmob_reward() && ar != null && getAdmob_au() != null) {
                ar.show(a, rewardItem -> {
                    if (rewardMode) {
                        setCompleted(true);
                    }
                });
            } else if (isUnity_reward() && isUnityadLoaded() && getUnity_au() != null) {
                UnityAds.show(a, unity_au, new UnityAdsShowOptions(), new IUnityAdsShowListener() {
                    @Override
                    public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
                    }

                    @Override
                    public void onUnityAdsShowStart(String placementId) {
                        new Handler().postDelayed(() -> {
                            if (rewardMode) {
                                setCompleted(true);
                            }

                        }, 7000);}

                    @Override
                    public void onUnityAdsShowClick(String placementId) {

                    }

                    @Override
                    public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {

                    }
                });

            } else if (isApplovin_reward() && mr.isReady() && getMax_au() != null) {
                mr.showAd(max_au);
            } else if (isAdcolony_reward() && adcolonyInt != null && getAdcolonyZone() != null && getAdcolony_Appid() != null) {
                adcolonyInt.show();
            } else if (isStatartapp_reward() && sr.isReady()) {
                sr.showAd();
            } else {
                if (rewardMode) {
                    if (isAdnotLoadedCredit()) {
                        setCompleted(true);
                    } else {
                        Toast.makeText(a, "No Ads Available Try Again !!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }
}
