package com.diggime.modules.email.model.impl;

import com.diggime.modules.email.model.EMail;
import com.diggime.modules.email.model.MailContact;
import org.json.JSONObject;

import java.time.LocalDateTime;
import java.util.List;

import static org.foilage.utils.checkers.NullChecker.notNull;

public class PostmarkEMail implements EMail {

    private final int id;

    private final String messageId;

    private final LocalDateTime sentDate;

    private final MailContact sender;

    private final List<MailContact> receivers;

    private final List<MailContact> carbonCopies;

    private final List<MailContact> blindCarbonCopies;

    private final String subject;

    private final String tag;

    private final String htmlBody;

    private final String textBody;

    public PostmarkEMail(LocalDateTime sentDate, MailContact sender, List<MailContact> receivers, List<MailContact> carbonCopies, List<MailContact> blindCarbonCopies, String subject, String tag, String htmlBody, String textBody) {
        this.id = 0;
        this.messageId = "";
        this.sentDate = notNull(sentDate);
        this.sender = notNull(sender);
        this.receivers = notNull(receivers);
        this.carbonCopies = notNull(carbonCopies);
        this.blindCarbonCopies = notNull(blindCarbonCopies);
        this.subject = notNull(subject);
        this.tag = notNull(tag);
        this.htmlBody = notNull(htmlBody);
        this.textBody = notNull(textBody);
    }

    public PostmarkEMail(String messageId, LocalDateTime sentDate, MailContact sender, List<MailContact> receivers, List<MailContact> carbonCopies, List<MailContact> blindCarbonCopies, String subject, String tag, String htmlBody, String textBody) {
        this.id = 0;
        this.messageId = notNull(messageId);
        this.sentDate = notNull(sentDate);
        this.sender = notNull(sender);
        this.receivers = notNull(receivers);
        this.carbonCopies = notNull(carbonCopies);
        this.blindCarbonCopies = notNull(blindCarbonCopies);
        this.subject = notNull(subject);
        this.tag = notNull(tag);
        this.htmlBody = notNull(htmlBody);
        this.textBody = notNull(textBody);
    }

    public PostmarkEMail(EMail mail, String messageId) {
        this.id = mail.getId();
        this.messageId = notNull(messageId);
        this.sentDate = mail.getSentDate();
        this.sender = mail.getSender();
        this.receivers = mail.getReceivers();
        this.carbonCopies = mail.getCarbonCopies();
        this.blindCarbonCopies = mail.getBlindCarbonCopies();
        this.subject = mail.getSubject();
        this.tag = mail.getTag();
        this.htmlBody = mail.getHtmlBody();
        this.textBody = mail.getTextBody();
    }

    public PostmarkEMail(int id, String messageId, LocalDateTime sentDate, MailContact sender, List<MailContact> receivers, List<MailContact> carbonCopies, List<MailContact> blindCarbonCopies, String subject, String tag, String htmlBody, String textBody) {
        this.id = id;
        this.messageId = notNull(messageId);
        this.sentDate = notNull(sentDate);
        this.sender = notNull(sender);
        this.receivers = notNull(receivers);
        this.carbonCopies = notNull(carbonCopies);
        this.blindCarbonCopies = notNull(blindCarbonCopies);
        this.subject = notNull(subject);
        this.tag = notNull(tag);
        this.htmlBody = notNull(htmlBody);
        this.textBody = notNull(textBody);
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getMessageId() {
        return messageId;
    }

    @Override
    public LocalDateTime getSentDate() {
        return sentDate;
    }

    @Override
    public MailContact getSender() {
        return sender;
    }

    @Override
    public List<MailContact> getReceivers() {
        return receivers;
    }

    @Override
    public List<MailContact> getCarbonCopies() {
        return carbonCopies;
    }

    @Override
    public List<MailContact> getBlindCarbonCopies() {
        return blindCarbonCopies;
    }

    @Override
    public String getSubject() {
        return subject;
    }

    @Override
    public String getTag() {
        return tag;
    }

    @Override
    public String getHtmlBody() {
        return htmlBody;
    }

    @Override
    public String getTextBody() {
        return textBody;
    }

    @Override
    public JSONObject getJSONObject() {

        JSONObject obj = new JSONObject();

        obj.put("From", sender.getName()+" <"+sender.getAddress()+">");

        addReceivers(obj, receivers, "To");
        addReceivers(obj, carbonCopies, "Cc");
        addReceivers(obj, blindCarbonCopies, "Bcc");

        obj.put("Subject", subject);

        if(tag.length()>0) {

            obj.put("Tag", tag);
        }

        if(htmlBody.length()>0) {

            obj.put("HtmlBody", htmlBody);

        } else {

            obj.put("HtmlBody", textBody);
        }

        if(textBody.length()>0) {

            obj.put("TextBody", textBody);

        }

        return obj;
    }

    private void addReceivers(JSONObject obj, List<MailContact> sendList, String jsonType) {

        boolean first = true;

        StringBuilder sb = new StringBuilder();

        for(MailContact contact: sendList) {

            if(first) {

                first = false;

            } else {

                sb.append(", ");
            }

            sb.append(contact.getName());
            sb.append(" <");
            sb.append(contact.getAddress());
            sb.append(">");

        }

        if(sendList.size()>0) {

            obj.put(jsonType, sb.toString());
        }
    }
}
