package com.ffii.fpsms.modules.common.mail.pojo

import jakarta.mail.internet.InternetAddress

open class MailRequest() {

    val PRIORITY_HIGHEST: Int = 1
    val PRIORITY_HIGH: Int = 2
    val PRIORITY_NORMAL: Int = 3
    val PRIORITY_LOW: Int = 4
    val PRIORITY_LOWEST: Int = 5

    open var from: InternetAddress? = null

    open var to: List<InternetAddress> = mutableListOf()

    open var subject: String? = null

    open var template: String? = null

    open var templateContent: String? = null

    open var args: Map<String, Any?> = mapOf()

    open var priority: Int? = null

    open var replyTo: InternetAddress? = null

    open var cc: List<InternetAddress> = mutableListOf()

    open var bcc: List<InternetAddress> = mutableListOf()

    open var attachments: Map<String, ByteArray> = mapOf()

    open fun builder(): Builder {
        return Builder()
    }

    open fun addAttachment(attachmentFilename: String, byteArray: ByteArray) {
        this.attachments += Pair(attachmentFilename, byteArray)
    }

    open fun addTo(to: InternetAddress) {
        this.to += to
    }

    open fun addCc(cc: InternetAddress) {
        this.cc += cc
    }

    open fun addBcc(bcc: InternetAddress) {
        this.bcc += bcc
    }

    open class Builder() {
        open var mailRequest:MailRequest = MailRequest()

        init {
            this.mailRequest = MailRequest()
        }

        open fun build(): MailRequest {
            return this.mailRequest
        }

        open fun addAttachment(attachmentFilename: String, byteArray: ByteArray): Builder {
            mailRequest.addAttachment(attachmentFilename, byteArray)
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addToInternetAddress")
        open fun addTo(to: InternetAddress): Builder {
            mailRequest.addTo(to)
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addToString")
        open fun addTo(to: String): Builder {
            mailRequest.addTo(InternetAddress(to))
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addToList")
        open fun addTo(to: List<String>): Builder {
            to.forEach {
                if (it.isNotEmpty()) {
                    mailRequest.addTo(InternetAddress(it))
                }
            }
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addCcInternetAddress")
        open fun addCc(cc: InternetAddress): Builder {
            mailRequest.addCc(cc)
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addCcString")
        open fun addCc(cc: String): Builder {
            mailRequest.addCc(InternetAddress(cc))
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addCcStringList")
        open fun addCc(cc: List<String>): Builder {
            cc.forEach {
                if (it.isNotEmpty()) {
                    mailRequest.addCc(InternetAddress(it))
                }
            }
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addBccInternetAddress")
        open fun addBcc(bcc: InternetAddress): Builder {
            mailRequest.addBcc(bcc)
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addBccString")
        open fun addBcc(bcc: String): Builder {
            mailRequest.addBcc(InternetAddress(bcc))
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("addBccStringList")
        open fun addBcc(bcc: List<String>): Builder {
            bcc.forEach {
                if (it.isNotEmpty()) {
                    mailRequest.addBcc(InternetAddress(it))
                }
            }
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("fromInternetAddress")
        open fun from(from: InternetAddress): Builder {
            mailRequest.from = from
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("fromString")
        open fun from(from: String): Builder {
            mailRequest.from = InternetAddress(from)
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("toListInternetAddress")
        open fun to(to: List<InternetAddress>): Builder {
            mailRequest.to = to
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("toListString")
        open fun to(to: List<String>): Builder {
            mailRequest.to = to.map { InternetAddress(it) }
            return this
        }

        open fun subject(subject: String?): Builder {
            mailRequest.subject = subject
            return this
        }

        open fun template(template: String?): Builder {
            mailRequest.template = template
            return this
        }

        open fun templateContent(templateContent: String?): Builder {
            mailRequest.templateContent = templateContent
            return this
        }

        open fun args(args: Map<String, Any?>): Builder {
            mailRequest.args = args
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("replyToInternetAddress")
        open fun replyTo(replyTo: InternetAddress?): Builder {
            mailRequest.replyTo = replyTo
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("replyToString")
        open fun replyTo(replyTo: String?): Builder {
            mailRequest.replyTo = InternetAddress(replyTo)
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("ccListInternetAddress")
        open fun cc(cc: List<InternetAddress>): Builder {
            mailRequest.cc = cc
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("ccListString")
        open fun cc(cc: List<String>): Builder {
            mailRequest.cc = cc.map { InternetAddress(it) }
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("bccListInternetAddress")
        open fun bcc(bcc: List<InternetAddress>): Builder {
            mailRequest.bcc = bcc
            return this
        }

        @Suppress("INAPPLICABLE_JVM_NAME")
        @JvmName("bccListString")
        open fun bcc(bcc: List<String>): Builder {
            mailRequest.bcc = bcc.map { InternetAddress(it) }
            return this
        }

        open fun attachments(attachments: Map<String, ByteArray>): Builder {
            mailRequest.attachments = attachments
            return this
        }

        open fun priority(priority: Int?): Builder {
            mailRequest.priority = priority
            return this
        }
    }
}