package net.taehui.twilight

import jakarta.mail.*
import jakarta.mail.internet.InternetAddress
import jakarta.mail.internet.MimeMessage
import net.taehui.twilight.system.Configure
import org.slf4j.LoggerFactory
import java.util.*

class QwilightLogging(private val loggerID: String) : Logger {
    override fun logInfo(toNotify: String) {
        LoggerFactory.getLogger(javaClass).info("[{}] {}", loggerID, toNotify)
        logFuture {
            Transport.send(MimeMessage(Session.getDefaultInstance(Properties().apply {
                put("mail.smtp.host", "localhost")
            }, object : Authenticator() {
                override fun getPasswordAuthentication(): PasswordAuthentication {
                    return PasswordAuthentication(Configure.fax.avatar, Configure.fax.auth)
                }
            })).apply {
                setFrom(InternetAddress("Qwilight"))
                addRecipient(
                    Message.RecipientType.TO,
                    InternetAddress(Configure.fax.remote)
                )
                subject = loggerID
                setText(toNotify)
            })
        }
    }

    override fun logFault(e: Throwable) {
        LoggerFactory.getLogger(javaClass).error("[{}] {}", loggerID, Utility.getFaultText(e))
    }
}