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

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

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.R;

import java.util.Objects;

public class StartActivity extends AppCompatActivity {

    private Dialog dialog;
    private LinearLayout first,fifth;
    private GridLayout second,third,four;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_start);
        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();
        });
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(view -> finish());
        dialog = new Dialog(this,R.style.transparent_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog);
        first = findViewById(R.id.first);
        fifth = findViewById(R.id.fifth);
        second = findViewById(R.id.second);
        third = findViewById(R.id.third);
        four = findViewById(R.id.four);

        EditText fnam = findViewById(R.id.fnaam);
        EditText lnam = findViewById(R.id.lnaam);
        EditText dob = findViewById(R.id.dob);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(view -> {
            if(fnam.getText().toString().equals("") || lnam.getText().toString().equals("")) {
                Toast.makeText(this, "fill box first",Toast.LENGTH_SHORT).show();
            } else {
                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) {
                        first.setVisibility(View.GONE);
                        third.setVisibility(View.GONE);
                        four.setVisibility(View.GONE);
                        second.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAdClicked(MaxAd ad) {

                    }

                    @Override
                    public void onAdLoadFailed(String adUnitId, MaxError error) {
                        dialog.dismiss();
                        first.setVisibility(View.GONE);
                        third.setVisibility(View.GONE);
                        four.setVisibility(View.GONE);
                        second.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                        dialog.dismiss();
                        first.setVisibility(View.GONE);
                        third.setVisibility(View.GONE);
                        four.setVisibility(View.GONE);
                        second.setVisibility(View.VISIBLE);
                    }
                });
                interstitialAd.loadAd();
            }
        });
        findViewById(R.id.not).setOnClickListener(view -> UserPayment());
        findViewById(R.id.cheque).setOnClickListener(view -> UserPayment());
        findViewById(R.id.cash).setOnClickListener(view -> UserPayment());
        findViewById(R.id.bnk).setOnClickListener(view -> UserPayment());

        findViewById(R.id.ss).setOnClickListener(view -> UserStatus());
        findViewById(R.id.se).setOnClickListener(view -> UserStatus());
        findViewById(R.id.st).setOnClickListener(view -> UserStatus());
        findViewById(R.id.otr).setOnClickListener(view -> UserStatus());

        findViewById(R.id.m0).setOnClickListener(view -> UserIncome());
        findViewById(R.id.m5).setOnClickListener(view -> UserIncome());
        findViewById(R.id.m10).setOnClickListener(view -> UserIncome());
        findViewById(R.id.m50).setOnClickListener(view -> UserIncome());
        findViewById(R.id.m100).setOnClickListener(view -> UserIncome());

        findViewById(R.id.gbtn).setOnClickListener(view -> {
            if(dob.getText().toString().equals("")) {
                Toast.makeText(this, "fill your date of birth",Toast.LENGTH_SHORT).show();
            } else {
                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(StartActivity.this,HomeActivity.class));
                        finish();
                    }

                    @Override
                    public void onAdClicked(MaxAd ad) {

                    }

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

                    @Override
                    public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                        dialog.dismiss();
                        startActivity(new Intent(StartActivity.this,HomeActivity.class));
                        finish();
                    }
                });
                interstitialAd.loadAd();
            }
        });
    }

    private void UserStatus() {
        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) {
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                four.setVisibility(View.GONE);
                third.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdClicked(MaxAd ad) {

            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                dialog.dismiss();
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                four.setVisibility(View.GONE);
                third.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                dialog.dismiss();
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                four.setVisibility(View.GONE);
                third.setVisibility(View.VISIBLE);
            }
        });
        interstitialAd.loadAd();
    }

    private void UserIncome() {
        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) {
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                third.setVisibility(View.GONE);
                four.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdClicked(MaxAd ad) {

            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                dialog.dismiss();
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                third.setVisibility(View.GONE);
                four.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                dialog.dismiss();
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                third.setVisibility(View.GONE);
                four.setVisibility(View.VISIBLE);
            }
        });
        interstitialAd.loadAd();
    }

    private void UserPayment() {
        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) {
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                third.setVisibility(View.GONE);
                four.setVisibility(View.GONE);
                fifth.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdClicked(MaxAd ad) {

            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                dialog.dismiss();
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                third.setVisibility(View.GONE);
                four.setVisibility(View.GONE);
                fifth.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                dialog.dismiss();
                first.setVisibility(View.GONE);
                second.setVisibility(View.GONE);
                third.setVisibility(View.GONE);
                four.setVisibility(View.GONE);
                fifth.setVisibility(View.VISIBLE);

            }
        });
        interstitialAd.loadAd();
    }
}
