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;

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);
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        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");
        Gson gson2 = new Gson();
        String json2 = gson2.toJson(remoteMessage);
        Log.i("remoteMessage", "remoteMessage : " + json2);
        Log.i("ad_tag", "ad_tag123 : " + ad_tag);

        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("Banner_GEO")) {
            appSharedpreferences.saveString("adtag_banner", "");
            appSharedpreferences.saveString("adtag_interstitial", ad_tag);
            Log.i("intersrtitial_geo_ad",""+ad_tag);
        }

/*
        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);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            NotificationGeofence.sendGeofenceEnteredNotification(notificationManager, this, ad_tag,title, body, click_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);
            }
        }*/


    }

}
