package com.newloanapp.instantloanapp.loancash.loanguide.loanapp.personalloanapply.loan.star.loanapply.activities;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxAdView;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.applovin.sdk.AppLovinSdk;
import com.facebook.ads.AudienceNetworkAds;
import com.newloanapp.instantloanapp.loancash.loanguide.loanapp.personalloanapply.loan.star.loanapply.Constant;
import com.newloanapp.instantloanapp.loancash.loanguide.loanapp.personalloanapply.loan.star.loanapply.R;

public class HomeActivity extends AppCompatActivity {

    private Dialog dialog;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_home);
        AudienceNetworkAds.initialize(this);
        AppLovinSdk.getInstance( this ).setMediationProvider("max");
        AppLovinSdk.initializeSdk( this, configuration -> {
            MaxAdView maxAdView = new MaxAdView(getString(R.string.banner_id),this);
            ((LinearLayout)findViewById(R.id.adView)).addView(maxAdView);
            maxAdView.loadAd();
        });
        dialog = new Dialog(this,R.style.transparent_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog);
        findViewById(R.id.startBtn).setOnClickListener(view -> {
            dialog.show();
            MaxInterstitialAd interstitialAd = new MaxInterstitialAd(getString(R.string.interstitial_id),this);
            interstitialAd.setListener(new MaxAdListener() {
                @Override
                public void onAdLoaded(MaxAd ad) {
                    dialog.dismiss();
                    interstitialAd.showAd();
                }

                @Override
                public void onAdDisplayed(MaxAd ad) {

                }

                @Override
                public void onAdHidden(MaxAd ad) {
                    startActivity(new Intent(HomeActivity.this,MainActivity.class));
                }

                @Override
                public void onAdClicked(MaxAd ad) {

                }

                @Override
                public void onAdLoadFailed(String adUnitId, MaxError error) {
                    dialog.dismiss();
                    startActivity(new Intent(HomeActivity.this,MainActivity.class));
                }

                @Override
                public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                    dialog.dismiss();
                    startActivity(new Intent(HomeActivity.this,MainActivity.class));
                }
            });
            interstitialAd.loadAd();
        });
        findViewById(R.id.shareBtn).setOnClickListener(view -> startActivity(new Intent(Intent.ACTION_SEND)
                .setType("text/plain").putExtra(Intent.EXTRA_TEXT,getString(R.string.app_share_text) + getPackageName())));
        findViewById(R.id.rateBtn).setOnClickListener(view -> startActivity(new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()))));
        findViewById(R.id.privacyPolicy).setOnClickListener(view -> startActivity(new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse("https://iplevent.com/policy/?" + getString(R.string.app_name)))));
    }

    @Override
    public void onBackPressed() {
        Constant.Ads ads = Constant.HomeAdsInitialization();
        Dialog dialog = new Dialog(this,R.style.transparent_dialog);
        dialog.setContentView(R.layout.layout_exit_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        ImageView adImg = dialog.findViewById(R.id.img);
        TextView adTxt = dialog.findViewById(R.id.txt);
        ImageView adBnr = dialog.findViewById(R.id.bnr);
        adTxt.setText(ads.getTitle());
        adImg.setImageDrawable(ResourcesCompat.getDrawable(getResources(),ads.getLogo(),null));
        adBnr.setImageDrawable(ResourcesCompat.getDrawable(getResources(),ads.getBanner(),null));
        dialog.findViewById(R.id.click).setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(ads.getLink()))));
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.findViewById(R.id.txt_rate).setOnClickListener(view -> startActivity(new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()))));
        dialog.findViewById(R.id.txt_later).setOnClickListener(view -> {
            startActivity(new Intent(this, ExitActivity.class));
            finish();
        });
        dialog.show();
    }
}
