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

import com.ffii.fpsms.modules.common.SettingNames
import com.ffii.fpsms.modules.common.mail.pojo.MailRequest
import com.ffii.fpsms.modules.settings.service.SettingsService
import com.ffii.fpsms.modules.user.service.UserService
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.stereotype.Service
import java.time.LocalDate
import java.time.format.DateTimeFormatter

data class TableRow(
    val staffId: String,
    val name: String,
    val missingDates: MutableList<LocalDate>,
)

@Service
open class MailReminderService(
    val mailService: MailService,
    val userService: UserService,
    val settingsService: SettingsService,
) {
    protected val logger: Log = LogFactory.getLog(javaClass)
    private val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

    private fun isSettingsConfigValid(): Boolean {
        try {
            val username = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
            val pw = settingsService.findByName(SettingNames.MAIL_SMTP_PASSWORD).orElseThrow().value
            return !username.isNullOrEmpty() && !pw.isNullOrEmpty()
        } catch (e: Error) {
            return false
        }
    }

    open fun createEmailRequest(content: String, emailTo: List<String>) {
        if (!isSettingsConfigValid()) return;
//        val subject = settingsService.findByName(SettingNames.TIMESHEET_MAIL_SUBJECT).orElseThrow().value
//        val template = settingsService.findByName(SettingNames.TIMESHEET_MAIL_TEMPLATE).orElseThrow().value
//        val cc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_CC).orElseThrow().value.split(",")
//        val bcc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_BCC).orElseThrow().value.split(",")
        val mailRequest = MailRequest.Builder()
            .subject("Rejected PO: PFP002PO25050178")
            .templateContent("<p>Dear AJI-NO-CHINMI COMPANY (HONG KONG) LIMITED,</p><p>The email serves to notify you that quality issue has been found in the material lot delivered on {completeDate}. The rejection details are as follows:</p><table style=\"width: 557px\"><colgroup><col style=\"width: 169px\"><col style=\"width: 388px\"></colgroup><tbody><tr><td colspan=\"1\" rowspan=\"1\" colwidth=\"169\"><p>PO No.</p></td><td colspan=\"1\" rowspan=\"1\" colwidth=\"388\"><p>PFP002PO25050178</p></td></tr></tbody></table><p></p><table style=\"width: 777px\"><colgroup><col style=\"width: 171px\"><col style=\"width: 205px\"><col style=\"width: 179px\"><col style=\"width: 222px\"></colgroup><tbody><tr><th colspan=\"1\" rowspan=\"1\" colwidth=\"171\"><p style=\"text-align: left\">Item Name</p></th><th colspan=\"1\" rowspan=\"1\" colwidth=\"205\"><p style=\"text-align: right\">Purchase Qty.</p></th><th colspan=\"1\" rowspan=\"1\" colwidth=\"179\"><p style=\"text-align: right\">Reject Qty.</p></th><th colspan=\"1\" rowspan=\"1\" colwidth=\"222\"><p style=\"text-align: left\">UoM</p></th></tr><tr><td colspan=\"1\" rowspan=\"1\" colwidth=\"171\"><p style=\"text-align: left\">日本蛋</p></td><td colspan=\"1\" rowspan=\"1\" colwidth=\"205\"><p style=\"text-align: right\">40</p></td><td colspan=\"1\" rowspan=\"1\" colwidth=\"179\"><p style=\"text-align: right\">10</p></td><td colspan=\"1\" rowspan=\"1\" colwidth=\"222\"><p style=\"text-align: left\">1箱X150個</p></td></tr></tbody></table><p>Best Regards,</p><p>{senderEmail}</p>")
//            .subject(subject)
//            .templateContent(template)
            .args(
                mapOf(
                    Pair("date", content),
                )
            )
            .addTo(emailTo)
//            .addCc(cc)
//            .addBcc(bcc)
            .build()
        val mailRequestList = mutableListOf<MailRequest>()
        mailRequestList += mailRequest
        mailService.send(mailRequestList)
    }

}