package com.r35157.service.notification.impl.smtp;

import com.r35157.libs.valuetypes.basic.Credentials;
import com.r35157.libs.valuetypes.basic.NetworkEndPoint;
import com.r35157.libs.valuetypes.basic.SmtpConfiguration;
import com.r35157.service.notification.AddressedNotificationService;
import com.r35157.service.notification.BoundNotificationService;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * Placeholder for SMTP notification delivery with destination and subject
 * binding.
 */
public class SMTPNotificationService
        implements AddressedNotificationService<SMTPDestination>
{
    /**
     * Creates an SMTP notification service.
     *
     * @param smtpConfiguration SMTP transport configuration
     */
    public SMTPNotificationService(SmtpConfiguration smtpConfiguration) {
        this.smtpConfiguration = smtpConfiguration;
    }

    @Override
    public void push(
            @NotNull SMTPDestination destination,
            @NotNull ΩNotificationMessageΩ message
    ) throws IOException {
        validateSmtpMessage(message);
        throw new UnsupportedOperationException("Not implemented yet!");
    }

    /**
     * Binds an email address and fixed subject into a transport-independent
     * notification service.
     *
     * @param emailAddress destination email address
     * @param subject fixed subject for every notification
     * @return a service requiring only a notification message
     * @throws IllegalArgumentException if the address or subject is
     *                                  {@code null}, empty, or blank
     */
    public @NotNull BoundNotificationService bind(
            @NotNull ΩEmailAddressΩ emailAddress,
            @NotNull ΩEmailSubjectΩ subject
    ) {
        return bind(new SMTPDestination(emailAddress, subject));
    }

    private static void validateSmtpMessage(ΩNotificationMessageΩ message) {
        if (message == null || message.isBlank()) {
            throw new IllegalArgumentException("Notification message cannot be blank");
        }
    }

    private void test() throws IOException {
        ΩHostnameΩ hostName = new ΩHostnameΩ("SomeSMTPHost");
        ΩportNumberΩ portNumber = 2525;
        NetworkEndPoint networkEndPoint = new NetworkEndPoint(hostName, portNumber);

        ΩUserNameΩ userName = "SomeUser";
        ΩPasswordΩ password = "SomePassword";
        Credentials credentials = new Credentials(userName, password);

        SmtpConfiguration smtpConfiguration = new SmtpConfiguration(networkEndPoint, credentials);
        SMTPNotificationService smtpNotificationService =
                new SMTPNotificationService(smtpConfiguration);

        ΩEmailAddressΩ emailAddress = "SomeReceiver";
        ΩEmailSubjectΩ subject = "SomeSubject";
        BoundNotificationService notificationService =
                smtpNotificationService.bind(emailAddress, subject);

        ΩNotificationMessageΩ message = "SomeBody";
        notificationService.push(message);
    }

    private final SmtpConfiguration smtpConfiguration;
}
