-- License: BSD3 (see LICENSE)
-- Author: Dino Morelli <dino@ui3.info>

{-# LANGUAGE FlexibleContexts #-}

{- | Module for pretty-printing epub metadata info

   Used internally by Codec.Epub.Format
-}
module Codec.Epub.Format.Metadata
   ( formatMetadata
   )
   where

import Control.Monad.Writer.Lazy
import Data.Foldable ( toList )
import Text.Printf

import Codec.Epub.Format.Util
import Codec.Epub.Data.Metadata


tellTitle :: MonadWriter (Seq Char) m => Title -> m ()
tellTitle (Title Nothing Nothing Nothing text) =
   tellSeq $ printf "title: %s\n" text
tellTitle title =
   tellSeq $ printf "title\n%s%s%s%s"
      (formatSubline "text" (Just $ titleText title))
      (formatSubline "lang" (titleLang title))
      (formatSubline "title-type" (titleType title))
      (formatSubline "display-seq" (show `fmap` titleSeq title))


tellCreator :: MonadWriter (Seq Char) m => Creator -> m ()
tellCreator (Creator Nothing Nothing Nothing creator) =
   tellSeq $ printf "creator: %s\n" creator
tellCreator (Creator role fileAs dseq creator) =
   tellSeq $ printf "creator\n%s%s%s%s"
      (formatSubline "text" (Just creator))
      (formatSubline "file-as" fileAs)
      (formatSubline "role" role)
      (formatSubline "display-seq" (show `fmap` dseq))


tellContributor :: MonadWriter (Seq Char) m => Creator -> m ()
tellContributor (Creator Nothing Nothing Nothing contributor) =
   tellSeq $ printf "contributor: %s\n" contributor
tellContributor (Creator role fileAs dseq contributor) =
   tellSeq $ printf "contributor\n%s%s%s%s"
      (formatSubline "text" (Just contributor))
      (formatSubline "file-as" fileAs)
      (formatSubline "role" role)
      (formatSubline "display-seq" (show `fmap` dseq))


tellDate :: MonadWriter (Seq Char) m => Date -> m ()
tellDate (Date Nothing date) =
   tellSeq $ printf "date: %s\n" date
tellDate (Date event date) =
   tellSeq $ printf "date\n%s%s"
      (formatSubline "event" event)
      (formatSubline "text" (Just date))


tellId :: MonadWriter (Seq Char) m => Identifier -> m ()
tellId ident =
   tellSeq $ printf "identifier\n%s%s%s%s"
      (formatSubline "id" (idId ident))
      (formatSubline "identifier-type" (idType ident))
      (formatSubline "scheme" (idScheme ident))
      (formatSubline "text" (Just . idText $ ident))


tellDescription :: MonadWriter (Seq Char) m => Description -> m ()
tellDescription (Description Nothing text) =
   tellSeq $ printf "description: %s\n" text
tellDescription (Description lang text) =
   tellSeq $ printf "description\n%s%s"
      (formatSubline "lang" lang)
      (formatSubline "text" (Just text))


tellSimpleString :: MonadWriter (Seq Char) m => String -> String -> m ()
tellSimpleString label = tellSeq . (printf "%s: %s\n" label)


tellSimpleMbString :: MonadWriter (Seq Char) m => String
   -> Maybe String -> m ()
tellSimpleMbString _     Nothing  = return ()
tellSimpleMbString label (Just s) = tellSimpleString label s


tellMetadata :: MonadWriter (Seq Char) m => Metadata -> m ()
tellMetadata (Metadata ids titles langs contributors creators dates modified source mType coverage desc format publisher relation rights subjects) = do
   mapM_ tellId ids
   mapM_ tellTitle titles
   mapM_ (tellSimpleString "language") langs
   mapM_ tellContributor contributors
   mapM_ tellCreator creators
   mapM_ tellDate dates
   tellSimpleMbString "modified" modified
   tellSimpleMbString "source" source
   tellSimpleMbString "type" mType
   mapM_ (tellSimpleString "coverage") coverage
   mapM_ tellDescription desc
   mapM_ (tellSimpleString "format") format
   mapM_ (tellSimpleString "publisher") publisher
   mapM_ (tellSimpleString "relation") relation
   mapM_ (tellSimpleString "rights") rights
   mapM_ (tellSimpleString "subject") subjects


{- | Format an epub Metadata structure for pretty printing
-}
formatMetadata :: Metadata -> String
formatMetadata meta = toList . execWriter
   $ tellMetadata meta