package com.anand.sampleads;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.ad.sdk.adserver.BannerImageAD;
import com.ad.sdk.adserver.BannerPosition;
import com.ad.sdk.adserver.Interstitial;
import com.ad.sdk.adserver.Listener.BannerListener;
import com.ad.sdk.adserver.Listener.InterstitialAdShowListener;
import com.ad.sdk.adserver.Listener.InterstitialLoadAdListener;
import com.anand.firebaseapp.MainActivity;
import com.anand.firebaseapp.R;
import com.anand.geofence.MapsActivity;
import com.anand.geofence.model.response.geofence.Geofence;
import com.anand.geofence.model.response.geofence.GetGeoLocationResponse;
import com.anand.geofence.reference.AppSharedpreferences;
import com.anand.geofence.retrofit.ApiClient;
import com.anand.geofence.sample.RetrofitCall;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeActivity extends AppCompatActivity {
    /*private ApiClient apiClient;*/
    private AppSharedpreferences appSharedpreferences;
    TextView token;
    private ApiClient apiClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        appSharedpreferences = AppSharedpreferences.getInstance(HomeActivity.this);
        String geoLocationJson = appSharedpreferences.getString("geoLocation");

        // Log.i("GeoLocations","GeoLocations : "+appSharedpreferences.getData("AllGeoLocations"));
        // getAllGeoLocations();
        apiClient = ApiClient.getInstance();
        FirebaseMessaging.getInstance().setAutoInitEnabled(true);
        String impUrl = appSharedpreferences.getString("impUrl_banner");
        String clickUrl = appSharedpreferences.getString("clickUrl_banner");

        String adtag_banner = appSharedpreferences.getString("adtag_banner");
        String adtag_interstitial = appSharedpreferences.getString("adtag_interstitial");
        String zoneid = appSharedpreferences.getString("publisherId");

        Log.i("adtag_banner", "adtag_banner :" + adtag_banner);
        Log.i("adtag_interstitial123", "adtag_interstitial : " + adtag_interstitial);


// Parse the JSON string into a list of Geofence objects
        List<Geofence> geofenceList = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(geoLocationJson);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Geofence geofence = new Geofence();
                geofence.setIdentifier(jsonObject.getString("identifier"));
                geofence.setLat(jsonObject.getString("lat"));
                geofence.setLog(jsonObject.getString("log"));
                geofence.setRadius(jsonObject.getString("radius"));
                geofenceList.add(geofence);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.i("LocationList", "LocationList : " + geofenceList);

// Iterate through the list of Geofence objects and extract the latitude and longitude values
        List<String> latitudes = new ArrayList<>();
        List<String> longitudes = new ArrayList<>();
        List<String> latLong = new ArrayList<>();
        for (Geofence geofence : geofenceList) {
            latitudes.add(geofence.getLat());
            longitudes.add(geofence.getLog());
            latLong.add(geofence.getLat() + "," + geofence.getLog());
        }
        Log.i("Latitudes Count", String.valueOf(latitudes.size()));
        Log.i("LatLong Count", String.valueOf(latLong.size()));
        Gson gson2 = new Gson();
        String json2 = gson2.toJson(latLong);
        Log.i("LatLong", json2);

        if (!adtag_banner.isEmpty() && !adtag_banner.equalsIgnoreCase("")) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    BannerImageAD.show(HomeActivity.this, adtag_banner, "HomeActivity", BannerPosition.BOTTOM, new BannerListener() {
                        @Override
                        public void onAdsAdLoaded() {

                        }

                        @Override
                        public void onAdsAdFailed() {

                        }
                    });
                }
            }, 1);
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    Interstitial.load(HomeActivity.this, adtag_interstitial, new InterstitialLoadAdListener() {
                        @Override
                        public void onAdsAdLoaded() {

                        }

                        @Override
                        public void onAdsAdFailed() {

                        }
                    });

                }
            }, 100);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    String interstitial_iamge_geo = appSharedpreferences.getString("adtag_interstitial");
                    Log.i("InterstitialImage", "InterstitialImage : " + interstitial_iamge_geo);
                    Interstitial.show(HomeActivity.this, "Interstitisl_image", "HomeActivity", interstitial_iamge_geo, new InterstitialAdShowListener() {
                        @Override
                        public void onAdsShowFailure() {

                        }

                        @Override
                        public void onAdsShowStart() {

                        }

                        @Override
                        public void onAdsShowClick() {

                        }

                        @Override
                        public void onAdsShowComplete() {

                        }

                        @Override
                        public void onAdsDismissed() {

                        }
                    });
                }
            }, 100);
        }


        //Banner Image
        ((LinearLayout) findViewById(R.id.bannerImage)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HomeActivity.this, BannerImageAd.class));
                finish();
            }
        });


        //Top Banner
        ((LinearLayout) findViewById(R.id.topBanner)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, TopBanner.class));
                finish();
            }
        });


        //Redirect Ad
        ((LinearLayout) findViewById(R.id.redirectAd)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, Direct_link_ad.class));
                finish();
            }
        });


        //Html Ad
        ((LinearLayout) findViewById(R.id.htmlAd)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, HTML_Ads.class));
                finish();
            }
        });

        //Html 5 Ad
        ((LinearLayout) findViewById(R.id.html5Ad)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, HTML_5_Ads.class));
                finish();
            }
        });

        //Popup Ad
        ((LinearLayout) findViewById(R.id.popupAds)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, PopUpAd.class));
                finish();
            }
        });


        //Interstitial Ad
        ((LinearLayout) findViewById(R.id.interstitial_Ads)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, InterstitialActivity.class));
                finish();
            }
        });

        //Rewarded Ads
        ((LinearLayout) findViewById(R.id.rewardedAd)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, RewardActivity.class));
                finish();
            }
        });

        //Bottom Slider
        ((LinearLayout) findViewById(R.id.bottomSlider)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, BottomSlider.class));
                finish();
            }
        });

        //InArticle
        ((LinearLayout) findViewById(R.id.inArticle)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, InArticleVideo.class));
                finish();
            }
        });

        //GeoLocation
        ((LinearLayout) findViewById(R.id.location)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*Intent in = new Intent(HomeActivity.this, MainActivity.class);
                startActivity(in);*/
                getAllGeoLocations(zoneid);
            }
        });

     /*   //Geofence Ads
        ((LinearLayout) findViewById(R.id.geoFence)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(HomeActivity.this, GeofenceAds.class));
                finish();
            }
        });*/


    }

    private void getAllGeoLocations(String zoneid) {
        apiClient.getApiInterface().getAllGeoLocations(zoneid).enqueue(new Callback<GetGeoLocationResponse>() {
            @Override
            public void onResponse(Call<GetGeoLocationResponse> call, Response<GetGeoLocationResponse> response) {
                Log.d("retrocall","retrocall : success : "+response.body());
                // List<GetGeoLocationResponse> getGeoLocationResponseList = response.body();
                List<Geofence> geofenceList = response.body().getGeofence();
                Gson gson = new Gson();
                String json = gson.toJson(geofenceList);
                Log.i("jsonLocation",json);
                appSharedpreferences.saveString("geoLocation",json);
                Intent in = new Intent(HomeActivity.this, MapsActivity.class);
                startActivity(in);

            }

            @Override
            public void onFailure(Call<GetGeoLocationResponse> call, Throwable t) {

            }
        });
    }

   /* private void getAllGeoLocations() {
            apiClient.getApiInterface().getAllGeoLocations("36").enqueue(new Callback<GetGeoLocationResponse>() {
                @Override
                public void onResponse(Call<GetGeoLocationResponse> call, Response<GetGeoLocationResponse> response) {
                    if (response.isSuccessful()){
                        if(response.body() != null){
                            GetGeoLocationResponse getGeoLocationResponse = response.body();
                            appSharedpreferences.saveData("AllGeoLocations",getGeoLocationResponse);
                        }
                    }

                }

                @Override
                public void onFailure(Call<GetGeoLocationResponse> call, Throwable t) {

                }
            });
    }*/

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(HomeActivity.this, ZoneActivity.class));
        finish();
    }


}
