package com.anand.firebaseapp;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.Context;
import android.util.Log;

import com.anand.geofence.NotificationGeofence;
import com.anand.geofence.reference.AppSharedpreferences;
import com.anand.sampleads.impression.ImpressionTask;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    AppSharedpreferences appSharedpreferences;
    Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        appSharedpreferences = AppSharedpreferences.getInstance(context);
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.e("newToken", token);
        appSharedpreferences.saveString("token", token);
        getSharedPreferences("_", MODE_PRIVATE).edit().putString("fcm_token", token).apply();
    }

    @SuppressLint("WrongThread")
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Gson gson2 = new Gson();
        String json2 = gson2.toJson(remoteMessage);

        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");
        String imp_url = remoteMessage.getData().get("imp_url");
        String click_url = remoteMessage.getData().get("click_url");
        String ad_tag = remoteMessage.getData().get("adtag");
        String ad_type = remoteMessage.getData().get("adtype");
        Log.i("remoteMessage", "remoteMessage : " + json2);
        Log.i("s2", "s2 : " + title+" "+body+" "+imp_url+" "+click_url+" "+ad_tag+" "+ad_type);

        if (ad_type.equalsIgnoreCase("Banner_GEO")) {
            appSharedpreferences.saveString("adtag_interstitial", "");
            appSharedpreferences.saveString("adtag_banner", ad_tag);
            Log.i("banner_geo_ad",""+ad_tag);
        } else if (ad_type.equalsIgnoreCase("GEO_AD_INT")) {
            appSharedpreferences.saveString("adtag_banner", "");
            appSharedpreferences.saveString("adtag_interstitial", ad_tag);
            Log.i("intersrtitial_geo_ad",""+ad_tag);
        }else if (ad_type.equalsIgnoreCase("Notification_GEO")){
            Log.i("imp_triggered","Imp_Triggered");
            new ImpressionTask().execute(imp_url);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            NotificationGeofence.sendGeofenceEnteredNotification(notificationManager, this, ad_tag,title, body, click_url);
        }

      /*  if (remoteMessage.getData().size() > 0) {
            String title = remoteMessage.getData().get("title");
            String message = remoteMessage.getData().get("body");
           Log.i("r123456",message);
        }

        // Handle notification messages if the app is in the foreground
        if (remoteMessage.getNotification() != null) {
            String message = remoteMessage.getData().get("body");
            Log.i("r1234567","message");
            //showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }



       */

/*
        if (ad_type.equalsIgnoreCase("Banner_GEO")) {
            appSharedpreferences.saveString("adtag_interstitial", "");
            appSharedpreferences.saveString("clickUrl_banner", click_url);
            appSharedpreferences.saveString("adtag_banner", ad_tag);
            Log.i("adtag_banner","adtag_banner"+ad_tag);
        }

        if (ad_type.equalsIgnoreCase("GEO_AD_INT")){
            appShar
            edpreferences.saveString("adtag_banner", "");
            appSharedpreferences.saveString("clickUrl_interstitial", click_url);
            appSharedpreferences.saveString("adtag_interstitial", ad_tag);
            Log.i("adtag_interstitial","adtag_interstitial"+ad_tag);
        }*/


       /* appSharedpreferences.saveString("impUrl",imp_url);
        appSharedpreferences.saveString("clickUrl",click_url);*/

       /* if (ad_tag.equalsIgnoreCase("")){
            Log.i("imp_triggered","Imp_Triggered");
            new ImpressionTask().execute(imp_url);
        }*/

       /* if (title != null && !title.equalsIgnoreCase("")) {
            Log.i("remoteMessageTitle", title);
            Log.i("remoteMessageBody", body);
            // Send a notification
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                NotificationGeofence.sendGeofenceEnteredNotification(notificationManager, this, title, body, click_url);
            }
        }*/


    }

}
