package com.bq.corbel.notifications.service;

import com.bq.corbel.notifications.model.Domain;
import com.bq.corbel.notifications.model.NotificationTemplate;
import com.bq.corbel.notifications.repository.DomainRepository;
import com.bq.corbel.notifications.repository.NotificationRepository;
import com.bq.corbel.notifications.template.NotificationFiller;
import com.bq.corbel.notifications.utils.DomainNameIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author Cristian del Cerro
 */
public class DefaultSenderNotificationsService implements SenderNotificationsService {

    NotificationFiller notificationFiller;
    NotificationsDispatcher notificationsDispatcher;
    NotificationRepository notificationRepository;
    DomainRepository domainRepository;
    private static final Logger LOG = LoggerFactory.getLogger(DefaultSenderNotificationsService.class);

    public DefaultSenderNotificationsService(NotificationFiller notificationFiller,
                                             NotificationsDispatcher notificationsDispatcher,
                                             NotificationRepository notificationRepository,
                                             DomainRepository domainRepository) {
        this.notificationFiller = notificationFiller;
        this.notificationsDispatcher = notificationsDispatcher;
        this.notificationRepository = notificationRepository;
        this.domainRepository = domainRepository;
    }

    @Override
    public void sendNotification(String domainId, String notificationId, Map<String, String> customProperties, String recipient) {
        LOG.debug("DefaultSenderNotificationsService send notification : domainId = "+ domainId + " recipient = " + recipient);

        String currentNotificationId = DomainNameIdGenerator.generateNotificationTemplateId(domainId, notificationId);

        Domain domain = domainRepository.findOne(domainId);

        String notificationTemplateId = Optional.ofNullable(domain)
                .map(currentDomain -> currentDomain.getTemplates())
                .map(currentTemplate -> currentTemplate.get(currentNotificationId))
                .orElse(currentNotificationId);

        Map<String, String> properties = Optional.ofNullable(domain)
                .map(currentDomain -> getProperties(currentDomain, customProperties))
                .orElse(customProperties);


        NotificationTemplate notificationTemplate = notificationRepository.findOne(notificationTemplateId);
        if (notificationTemplate != null) {
            NotificationTemplate notificationTemplateFilled = notificationFiller.fill(notificationTemplate, properties);
            notificationsDispatcher.send(notificationTemplateFilled, recipient);
        }

    }

    private Map<String, String> getProperties(Domain domain, Map<String, String> customProperties) {
        Map<String, String> propertiesByDomain = domain.getProperties() != null ? domain.getProperties() : Collections.emptyMap();

        Map<String, String> properties = new HashMap<>();
        properties.putAll(propertiesByDomain);

        if(customProperties != null) {
            properties.putAll(customProperties);
        }

        return  properties;
    }


}

@@ -1732,137 +1732,8 @@
 ) %7B%0A
-        LOG.debug(%22DefaultSenderNotificationsService send notification : domainId = %22+ domainId + %22 recipient = %22 + recipient);%0A%0A
     
@@ -2732,16 +2732,206 @@
 pient);%0A
+            LOG.debug(%22Template with id: %22+ notificationTemplateId + %22 was sent%22);%0A        %7D%0A        else%7B%0A            LOG.error(%22Template with id: %22+ notificationTemplateId +%22 not found%22);%0A
         

Add log to send notification