package com.team.app.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.team.app.Constant;
import com.team.app.Functions;
import com.team.app.R;
import com.team.app.models.News;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.squareup.picasso.Picasso;

import java.util.Map;

import static com.team.app.Constant.APP_URL_MAIN;

public class NewsActivity extends AppCompatActivity {

    ImageView imageView;
    WebView longDescription;
    TextView shortDescription;
    AdView adView;
    public static News news;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        MobileAds.initialize(this, initializationStatus -> {
            Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
            for (String adapterClass : statusMap.keySet()) {
                AdapterStatus status = statusMap.get(adapterClass);
            }
            FrameLayout adContainerView0 = findViewById(R.id.bannerAdView);
            if(Constant.adShown && !Constant.BANNER_ADS_ID.equals("")) {
                adView = new com.google.android.gms.ads.AdView(this);
                adView.setAdUnitId(Constant.BANNER_ADS_ID);
                adContainerView0.addView(adView);
                adView.setAdSize(Functions.getAdSize(this));
                adView.loadAd(new AdRequest.Builder().build());
                adContainerView0.setVisibility(View.VISIBLE);
            }
        });
        findViewById(R.id.back).setOnClickListener(view -> NewsActivity.this.onBackPressed());
        findViewById(R.id.gifImage).setOnClickListener(view -> startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse(getResources().getString(R.string.fantasy_power_11_link)))));
        this.imageView = findViewById(R.id.imageView);
        this.longDescription = findViewById(R.id.longDescription);
        this.shortDescription = findViewById(R.id.shortDescription);
        String imgurl = APP_URL_MAIN + "images/";
        Picasso.get().load(imgurl+news.getThumb()).placeholder(R.drawable.logo).fit().into(imageView);
        this.shortDescription.setText(news.getTitle());
        this.longDescription.setFocusableInTouchMode(false);
        this.longDescription.setFocusable(false);
        WebSettings webSettings = this.longDescription.getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultFontSize(16);
        String mimeType = "text/html; charset=UTF-8";
        String encoding = "utf-8";
        this.longDescription.loadDataWithBaseURL(null,news.getDetail(),mimeType,encoding,null);
        this.longDescription.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
    }

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