package io.corbel.notifications.service;

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.ApnsServiceBuilder;
import io.corbel.notifications.model.Domain;
import io.corbel.notifications.model.NotificationTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Map;

/**
 * Created by Alberto J. Rubio
 */
public class IosPushNotificationsService implements NotificationsService {

    private static final Logger LOG = LoggerFactory.getLogger(IosPushNotificationsService.class);

    private final Map<String, ApnsService> apnsServices = Collections.emptyMap();

    @Override
    public void send(Domain domain, NotificationTemplate notificationTemplate, String... recipients) {
        try {
            if (!apnsServices.containsKey(domain.getId())) {
                createApnsService(domain);
            }
            String payload = APNS.newPayload().badge(1).alertTitle(notificationTemplate.getTitle())
                    .alertBody(notificationTemplate.getText()).build();
            apnsServices.get(domain.getId()).push(Arrays.asList(recipients), payload);
            LOG.info("Ios push notification sent to: " + Arrays.toString(recipients));
        } catch (Exception e) {
            LOG.error("Sending Ios push notification error: {}", e.getMessage(), e);
        }
    }

    private void createApnsService(Domain domain) {
        byte[] certificate = domain.getIosNotificationsCertificate().getBytes(StandardCharsets.UTF_8);
        ApnsServiceBuilder apnsServiceBuilder = APNS.newService()
                .withCert(new ByteArrayInputStream(Base64.getDecoder().decode(certificate)),
                domain.getIosNotificationsPassword());
        if (domain.isProductionEnvironment()) {
            apnsServiceBuilder.withProductionDestination();
        } else {
            apnsServiceBuilder.withSandboxDestination();
        }
        apnsServices.put(domain.getId(), apnsServiceBuilder.build());
    }
}

@@ -408,91 +408,9 @@
 til.
-Arrays;%0Aimport java.util.Base64;%0Aimport java.util.Collections;%0Aimport java.util.Map
+*
 ;%0A%0A/
@@ -684,28 +684,21 @@
 s = 
-Collections.empty
+new Hash
 Map
+%3C%3E
 ();%0A

Fix problem with Collections.emptyMap()