package com.friendvilla.OfferG;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.squareup.picasso.Picasso;

public class DetailViewActivity extends AppCompatActivity {

    private TextView Title;
    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_view);
        Title = findViewById(R.id.Title);
        TextView name = findViewById(R.id.title);
        ImageView img = findViewById(R.id.img);
        TextView category = findViewById(R.id.Category);
        LinearLayout back = findViewById(R.id.back);
        LinearLayout dots = findViewById(R.id.dots);
        Toolbar toolbar = findViewById(R.id.toolBar);
        WebView webView = findViewById(R.id.WebView);
        setSupportActionBar(toolbar);
        Bundle bundle = getIntent().getExtras();
        String imgUri = "http://offerg.friendvilla.in/upload/thumbs/" + bundle.getString("Image");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mInterstitialAd != null) {
                    mInterstitialAd.show(DetailViewActivity.this);
                } else {
                    finish();
                }
                DetailViewActivity.super.onBackPressed();
            }
        });
        dots.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog shareDialog = new Dialog(DetailViewActivity.this);
                shareDialog.setContentView(R.layout.share_dialog);
                Window window = shareDialog.getWindow();
                WindowManager.LayoutParams wlp = window.getAttributes();
                wlp.gravity = Gravity.TOP|Gravity.END;
                wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                window.setAttributes(wlp);
                shareDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                shareDialog.setCancelable(true);
                CardView btn = (CardView) shareDialog.findViewById(R.id.bb);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareDialog.dismiss();
                        Intent intnt = new Intent(Intent.ACTION_SEND);
                        intnt.setType("text/plain");
                        intnt.putExtra(Intent.EXTRA_TEXT,"*** *" + Title.getText() + "* ***" + "\n\nGet Details About This Offer And Many More Excining Offers Like This Only One *OfferG*.\n\nDownload the App Now- https://bit.ly/OfferG");
                        startActivity(Intent.createChooser(intnt,"Share Via"));
                    }
                });
                shareDialog.show();
            }
        });
        name.setText(bundle.getString("Title"));
        Title.setText(bundle.getString("Title"));
        switch (bundle.getString("Category")) {
            case "8":
                category.setText("Recharge Offers - " + bundle.getString("Date"));
                break;
            case "9":
                category.setText("Shopping Offers - " + bundle.getString("Date"));
                break;
            case "10":
                category.setText("App Offers - " + bundle.getString("Date"));
                break;
            case "11":
                category.setText("Special Offers - " + bundle.getString("Date"));
                break;
            case "12":
                category.setText("Other Offers - " + bundle.getString("Date"));
                break;
            default:
                category.setText("OfferG Special - " + bundle.getString("Date"));
                break;
        }
        Picasso.get().load(imgUri).placeholder(R.drawable.view).fit().into(img);
        LoadIntertitialAd();
        webView.setBackgroundColor(Color.TRANSPARENT);
        webView.setFocusableInTouchMode(false);
        webView.setFocusable(false);
        WebSettings webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultFontSize(16);
        String mimeType = "text/html; charset=UTF-8";
        String encoding = "utf-8";
        SharedPreferences SPRNCE = getSharedPreferences("Festy", Context.MODE_PRIVATE);
        String htmlText;
        if (SPRNCE.contains("ThemeMode") && SPRNCE.getString("ThemeMode","Night").equals("Night")) {
           htmlText = "<body style=\"color:#7A7878;\">" + bundle.getString("Details") + "</body>";
        } else {
            htmlText = "<body style=\"color:#1A1818;\">" + bundle.getString("Details") + "</body>";
        }
        webView.loadDataWithBaseURL(null, htmlText,mimeType,encoding,null);
    }

    private void LoadIntertitialAd() {
        MobileAds.initialize(DetailViewActivity.this);
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(this,getResources().getString(R.string.interstitial_id),adRequest,new InterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                super.onAdLoaded(interstitialAd);
                mInterstitialAd = interstitialAd;
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                super.onAdFailedToLoad(loadAdError);
                mInterstitialAd = null;
            }
        });
    }


    @Override
    public void onBackPressed() {
        if(mInterstitialAd != null) {
            mInterstitialAd.show(this);
        } else {
            DetailViewActivity.super.onBackPressed();
        }
    }
}
