-- SPDX-FileCopyrightText: 2022 Steffen Jost <jost@tcs.ifi.lmu.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

{-# LANGUAGE OverloadedStrings #-}
-- usage: 
--  > npm run build
--  > stack ghci -- testdata/test_letters.hs

-- Also see: https://stackoverflow.com/questions/62006705/pandoc-output-in-markdown-how-to-add-the-metadata

import Import
import qualified Data.Text as T
import qualified Data.ByteString.Lazy as LBS

import qualified Text.Pandoc         as P
import qualified Text.Pandoc.PDF     as P
import qualified Text.Pandoc.Builder as P

import Model.Types.Markup
import Utils.Print 
import Handler.PrintCenter


mdTmpl :: Text 
mdTmpl = "---\nfoo: fooOrg\nbar: barOrg\n---\nHere is some text\n - foo: $foo$\n - bar: $bar$\nbody\n$body$\nend\n"

-- Current Function found in Handler.PrintCenter, but is no longer exported!
mprToMeta :: MetaPinRenewal -> P.Meta
mprToMeta MetaPinRenewal{..} = P.Meta $ mconcat
  [ toMeta "examinee"  mppExaminee
  , toMeta "address"   (mppAddress & html2textlines)
  , toMeta "login"      mppLogin
  , toMeta "pin"        mppPin
  , mbMeta "url"       (mppURL  <&> tshow)
  , toMeta "date"      (mppDate  &  tshow) -- TODO: render according to user preference
  , toMeta "lang"       mppLang
  , mbMeta keyOpening   mppOpening
  , mbMeta keyClosing   mppClosing
  ]
  where
    deOrEn     = if isDe mppLang then "de" else "en"
    keyOpening = deOrEn <> "-opening"
    keyClosing = deOrEn <> "-closing"
    mbMeta :: P.ToMetaValue a => Text -> Maybe a -> Map Text P.MetaValue
    mbMeta   = foldMap . toMeta
    toMeta :: P.ToMetaValue a => Text ->       a -> Map Text P.MetaValue
    toMeta k = singletonMap k . P.toMetaValue
    html2textlines :: StoredMarkup -> [Text]
    html2textlines sm = T.lines . LT.toStrict $ markupInput sm

test :: IO T.Text
test = do 
  res <- P.runIO $ reTemplateLetter (Handler.PrintCenter.mprToMeta def) (markdownToStoredMarkup templateRenewal)
  return $ case res of 
    Left err -> P.renderError err 
    Right t -> t

test1 = appMeta setIsDeFromLang $ addMeta (mprToMeta def) mempty

test2 = P.runIOorExplode $ do 
  let readerOpts = def { P.readerExtensions    = P.pandocExtensions
                       , P.readerStripComments = True
                       , P.readerStandalone    = True 
                       }
  P.readMarkdown readerOpts templateRenewal  

test3 = do 
  doc1 <- test2 
  let doc2 = P.setMeta (T.pack "foooooo") (T.pack "baaaaaaar") $ appMeta setIsDeFromLang $ addMeta (mprToMeta def) doc1 
      writerOpts = def { P.writerExtensions = P.enableExtension P.Ext_yaml_metadata_block P.pandocExtensions}
  P.runIOorExplode $ P.writeMarkdown writerOpts doc2 

test4 = do
  pdf <- P.runIOorExplode $ pdfRenewal' $ mprToMeta def 
  LBS.writeFile "./testgen.pdf" pdf


t5meta :: P.Meta
t5meta = P.setMeta "lang" ("de-de"::Text) $ P.setMeta "foo" ("HERE"::Text) mempty

t5meta2 :: P.Meta
t5meta2 = P.setMeta "lang" ("en-gb"::Text) $ P.setMeta "bar" ("XYZ"::Text) mempty


t5redoc :: IO Text
t5redoc = P.runIOorExplode $ reTemplateLetter' t5meta mdTmpl

t5tmpl :: IO (P.Template Text)
t5tmpl = P.runIOorExplode $ compileTemplate mdTmpl

t5doc :: IO P.Pandoc
t5doc = P.runIOorExplode $ P.readMarkdown defReaderOpts mdTmpl

t5reDoc2 :: IO Text 
t5reDoc2 = do 
  t <- t5tmpl
  d <- t5doc
  let P.Pandoc _ di = d 
      -- d2 = P.Pandoc t5meta di -- this works
      d2 = addMeta t5meta d
  P.runIOorExplode $ P.writeMarkdown (defWriterOpts t) d2