-- SPDX-FileCopyrightText: 2022 Gregor Kleen <gregor.kleen@ifi.lmu.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

module Jobs.Handler.SendTestEmail
  ( dispatchJobSendTestEmail
  ) where

import Import

import Handler.Utils.Mail
import Handler.Utils.DateTime
import Text.Hamlet
-- import Handler.Utils.I18n
-- import Text.Blaze.Internal

dispatchJobSendTestEmail :: Email -> MailContext -> JobHandler UniWorX
dispatchJobSendTestEmail jEmail jMailContext = JobHandlerException . mailT jMailContext $ do
  _mailTo .= [Address Nothing jEmail]
  replaceMailHeader "Auto-Submitted" $ Just "auto-generated"
  setSubjectI MsgMailTestSubject
  now <- liftIO getCurrentTime
  nDT <- formatTimeMail SelFormatDateTime now
  nD <- formatTimeMail SelFormatDate now
  nT <- formatTimeMail SelFormatTime now
  addHtmlMarkdownAlternatives' "part1" $ \(MsgRenderer mr) -> [shamlet|
    <h2>
      #{mr MsgMailTestContent}

    <p>
      #{mr MsgMailTestDateTime}
      <ul>
        <li>#{nDT}
        <li>#{nD}
        <li>#{nT}
    |]
  addHtmlMarkdownAlternatives' "part2" $ \(MsgRenderer _mr) -> [shamlet|
    <h2>Second part, just for testing
    <p>
      Please ignore this part of the message.
    |]
  -- Compiles as well: let trdmsg :: HtmlUrlI18n _ (Route UniWorX) = [ihamlet|
  let trdmsg :: HtmlUrlI18n UniWorXJobsHandlerMessage (Route UniWorX) = [ihamlet|
    <h2>Third part, again only for tests
    <p>
      _{MsgMailTestDateTime}
      <ul>
        <li>#{nDT}
        <li>#{nD}
        <li>#{nT}
    <p>
      Message was sent to you by
        <a href=@{NewsR}>
          FRADrive
    |]
  addHtmlMarkdownAlternatives' "part3" trdmsg
  -- Html == Markup == MarkupM ()
  --test <- liftHandler $ withUrlRenderer $(i18nHamletFile "test")
  test :: Html <- liftHandler $ withUrlRenderer $(hamletFile "templates/i18n/test/en-eu.hamlet")
  addHtmlMarkdownAlternatives test
  -- 
  --test2 <- liftHandler $(i18nHamletFile "test")
  --addHtmlMarkdownAlternatives test2