package eu.eudat.logic.services.utilities;

import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
import eu.eudat.data.dao.entities.DMPDao;
import eu.eudat.data.dao.entities.InvitationDao;
import eu.eudat.data.entities.DMP;
import eu.eudat.data.entities.Invitation;
import eu.eudat.data.entities.UserInfo;
import eu.eudat.models.data.mail.SimpleMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import types.LoggingType;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;


@Service("invitationService")
public class InvitationServiceImpl implements InvitationService {
    private static final Logger logger = LoggerFactory.getLogger(InvitationServiceImpl.class);
//    private Logger logger;
    private Environment environment;

    @Autowired
    public InvitationServiceImpl(/*Logger logger,*/ Environment environment) {
//        this.logger = logger;
        this.environment = environment;
    }

    @Override
    public void assignToDmp(DMPDao dmpDao, List<eu.eudat.data.entities.UserDMP> users, DMP dmp) {
        for (eu.eudat.data.entities.UserDMP user : users) {
            dmp.getUsers().add(user);
        }
        dmpDao.createOrUpdate(dmp);
    }

    @Override
    public void assignToDmp(DMPDao dmpDao, eu.eudat.data.entities.UserDMP user, DMP dmp) {
        if (!dmp.getUsers().stream().map(x -> x.getUser().getId()).collect(Collectors.toList()).contains(user.getId())) {
            dmp.getUsers().add(user);
            dmpDao.createOrUpdate(dmp);
        }
    }

    @Override
    public void createInvitations(InvitationDao invitationDao, MailService mailService, List<eu.eudat.data.entities.UserInfo> users, DMP dmp, eu.eudat.data.entities.UserInfo creator) throws MessagingException {
        for (eu.eudat.data.entities.UserInfo userInfo : users) {
            Invitation invitation = new Invitation();
            invitation.setDmp(dmp);
            invitation.setInvitationEmail(userInfo.getEmail());
            invitation.setUser(creator);
            invitation.setToken(UUID.randomUUID());
            invitation.setAcceptedInvitation(false);
            invitationDao.createOrUpdate(invitation);
            sendInvitationAsync(dmp, invitation, userInfo.getName(), mailService);
        }
    }

    @Override
    public CompletableFuture sendInvitationAsync(DMP dmp, Invitation invitation, String recipient, MailService mailService) {
        return CompletableFuture.runAsync(() -> {
            SimpleMail mail = new SimpleMail();
            mail.setSubject(createSubject(dmp, mailService.getMailTemplateSubject()));
            mail.setContent(createContent(invitation.getId(), dmp, recipient, mailService.getMailTemplateContent("classpath:email.html")));
            mail.setTo(invitation.getInvitationEmail());
            try {
                mailService.sendSimpleMail(mail);
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            }
        });
    }

    private String createSubject(DMP dmp, String templateSubject) {
        String subject = templateSubject.replace("{dmpname}", dmp.getLabel());
        return subject;
    }

    private String createContent(UUID invitationID, DMP dmp, String recipient, String templateContent) {
        String content = templateContent.replace("{dmpname}", dmp.getLabel());
        content = content.replace("{invitationID}", invitationID.toString());
        content = content.replace("{recipient}", recipient);
        content = content.replace("{host}", this.environment.getProperty("dmp.domain"));

        return content;
    }
}
