package io.corbel.notifications.service;

import io.corbel.notifications.model.Domain;
import io.corbel.notifications.model.NotificationTemplate;
import io.corbel.notifications.repository.DomainRepository;
import io.corbel.notifications.repository.NotificationRepository;
import io.corbel.notifications.template.NotificationFiller;
import io.corbel.notifications.utils.DomainNameIdGenerator;

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 {

    private static final String LANGUAGE_SEPARATOR = ":";

    private final NotificationFiller notificationFiller;
    private final NotificationsDispatcher notificationsDispatcher;
    private final NotificationRepository notificationRepository;
    private final DomainRepository domainRepository;
    private final String languageProperty;
    private final String defaultLanguage;

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

    @Override
    public void sendNotification(String domainId, String notificationId, Map<String, String> customProperties, String ... recipients) {
        String currentNotificationId = DomainNameIdGenerator.generateNotificationTemplateId(domainId, notificationId);

        Domain domain = domainRepository.findOne(domainId);

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

        boolean internationalizated = Optional.ofNullable(domain).map(Domain::getInternationalizations)
                                      .map(i -> i.get(currentNotificationId)).orElse(false);

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

        if(internationalizated) {
            String lang = properties.getOrDefault(languageProperty, defaultLanguage);
            notificationTemplateId = notificationTemplateId + LANGUAGE_SEPARATOR + lang;
        }

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

    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;
    }


}

@@ -1984,127 +1984,8 @@
 s) %7B
-%0A        String currentNotificationId = DomainNameIdGenerator.generateNotificationTemplateId(domainId, notificationId);
 %0A%0A  
@@ -2210,32 +2210,25 @@
 emplate.get(
-currentN
+n
 otificationI
@@ -2259,227 +2259,86 @@
 lse(
-currentNotificationId);%0A%0A        boolean int
+DomainNameIdGen
 er
-n
 at
-ionalizated = Optional.ofNullable(domain).map(Domain::getInternationalizations)%0A                                      .map(i -%3E i.get(currentNotificationId)).orElse(false
+or.generateNotificationTemplateId(domainId, notificationId)
 );%0A%0A
@@ -2532,24 +2532,193 @@
 operties);%0A%0A
+        boolean internationalizated = Optional.ofNullable(domain).map(Domain::getInternationalizations)%0A                .map(i -%3E i.get(notificationId)).orElse(false);%0A%0A
         if(i

Fix problem in translate notifications