package com.base.datarecovery.fcm;

import android.util.Log;

import androidx.annotation.NonNull;

import com.base.datarecovery.utils.AppPreferences;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FcmService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        long pushStayTime = remoteMessage.getData().get("push_stay_time") != null ? Long.parseLong(remoteMessage.getData().get("push_stay_time")) : 0;
        int open = remoteMessage.getData().get("open") != null ? Integer.parseInt(remoteMessage.getData().get("open")) : 0;
        int num = remoteMessage.getData().get("num") != null ? Integer.parseInt(remoteMessage.getData().get("num")) : 0;
        long delay = remoteMessage.getData().get("delay") != null ? Long.parseLong(remoteMessage.getData().get("delay")) : 0L;
        int actionS = remoteMessage.getData().get("actionS") != null ? Integer.parseInt(remoteMessage.getData().get("actionS")) : 0;
        int lockS = remoteMessage.getData().get("lockS") != null ? Integer.parseInt(remoteMessage.getData().get("lockS")) : 0;
        int adClickCount = remoteMessage.getData().get("adClickCount") != null ? Integer.parseInt(remoteMessage.getData().get("adClickCount")) : 0;
        int adShowCount = remoteMessage.getData().get("adShowCount") != null ? Integer.parseInt(remoteMessage.getData().get("adShowCount")) : 0;
        int adInterval = remoteMessage.getData().get("adInterval") != null ? Integer.parseInt(remoteMessage.getData().get("adInterval")) : 0;
        int interval = remoteMessage.getData().get("notificationInterval") != null ? Integer.parseInt(remoteMessage.getData().get("notificationInterval")) : 0;
        int timerS = remoteMessage.getData().get("timerS") != null ? Integer.parseInt(remoteMessage.getData().get("timerS")) : 1;
        int timerDelay = remoteMessage.getData().get("timerDelay") != null ? Integer.parseInt(remoteMessage.getData().get("timerDelay")) : 1;
        int timerInterval = remoteMessage.getData().get("timerInterval") != null ? Integer.parseInt(remoteMessage.getData().get("timerInterval")) : 5;

        AppPreferences.getInstance().put("actionS", actionS);
        AppPreferences.getInstance().put("open", open);
        AppPreferences.getInstance().put("num", num);
        AppPreferences.getInstance().put("delay", delay);
        AppPreferences.getInstance().put("lockS", lockS);
        AppPreferences.getInstance().put("notification_interval", interval);
        AppPreferences.getInstance().put("timerS", timerS);
        AppPreferences.getInstance().put("timerDelay", timerDelay);
        AppPreferences.getInstance().put("timerInterval", timerInterval);

        if (timerS == 0) {
            RecoveryTimerManager.getInstance().stopTaskTimer();
        } else {
            if (!RecoveryTimerManager.getInstance().isTaskTimerActive()) {
                RecoveryTimerManager.getInstance().scheduleTask(timerDelay * 60000, timerInterval * 60000);
            }
        }

//        AdmobUtils.INSTANCE.setAdDisplayInterval(adInterval);
//        AdDisplayUtils.getInstance().setMaxAdDisplayCount(adShowCount);
//        AdDisplayUtils.getInstance().setMaxAdClickCount(adClickCount);
//
//        EventUtils.INSTANCE.event("FCM_Received",null,null,false);
//
//        NotificationUtil.sendNotification(MyApplication.context);

        Log.d("FcmService", remoteMessage.getData().toString());

    }
}
