package com.airlock.iam.login.app.misc;

import com.airlock.iam.common.api.domain.model.persistency.exception.PersisterException;
import com.airlock.iam.core.misc.cronto.CrontoHandler;
import com.airlock.iam.core.misc.cronto.CrontoPushNotificationIdRequest;
import com.airlock.iam.core.misc.impl.cronto.pushnotification.PushAppHandler;
import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.login.app.misc.cronto.application.CrontoApplicationResponseFormatter;
import com.airlock.iam.login.app.misc.cronto.application.CrontoMobileApplicationFormatterFactory;
import com.airlock.iam.plugin.api.config.ConfigurationContext;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

public class CrontoPushNotificationIdRegistrationServlet extends CrontoAppCommunicationServlet {
   private static final ELogger LOG = ELoggerFactory.getLogger(CrontoPushNotificationIdRegistrationServlet.class);

   @Override
   protected void handleCrontoRequest(
      HttpServletRequest request, HttpServletResponse response, ConfigurationContext context, Optional<CrontoHandler> crontoHandler
   ) throws IOException {
      CrontoApplicationResponseFormatter formatter = CrontoMobileApplicationFormatterFactory.forRequest(request);
      if (crontoHandler.isEmpty()) {
         LOG.debug("Discarding push notification ID registration request because there is no Cronto self-service configuration available.");
         formatter.formatErrorResponse(response, "Notification Identifier registration failed because the server does not support this feature.");
      } else if (!crontoHandler.get().isPushNotificationEnabled()) {
         LOG.debug("Discarding push notification ID registration request because this feature is disabled.");
         formatter.formatErrorResponse(response, "Login with push notification is disabled on the server.");
      } else {
         PushAppHandler pushAppHandler = crontoHandler.get().getPushAppHandler();

         try {
            Optional<CrontoPushNotificationIdRequest> notificationIdRequest = pushAppHandler.parsePushNotificationIdRequest(request);
            if (notificationIdRequest.isEmpty()) {
               LOG.debug("Ignoring push notification id registration request because it is invalid.");
               formatter.formatErrorResponse(response, "Notification Identifier registration failed because the registration request is invalid.");
               return;
            }

            pushAppHandler.registerPushNotificationId(notificationIdRequest.get().getDevice(), notificationIdRequest.get().getPushNotificationId());
            String responseBody = formatter.formatSuccessResponse(response);
            LOG.debug("Successfully finished handling of push notification ID registration request " + notificationIdRequest.get() + ".");
            LOG.debug("Response body: " + responseBody);
         } catch (RuntimeException e) {
            LOG.error(com.airlock.iam.elog.api.LogTypes.ILLEGAL_ARGUMENT, "Registration of push notification ID failed: " + e.getMessage());
            formatter.formatErrorResponse(response, "Notification Identifier registration failed.");
         } catch (PersisterException e) {
            LOG.error(com.airlock.iam.elog.api.LogTypes.DATABASE_ACCESS, "Notification Identifier registration failed: " + e.getMessage());
            formatter.formatErrorResponse(response, "Notification Identifier registration failed.");
         }
      }
   }
}
