{-# LANGUAGE TemplateHaskell #-}
-- | Defines types and functions to gather all the information needed for
-- printing.
module Language.Drasil.Printing.PrintingInformation (
    PrintingInformation
  , Notation(..)
  , sysdb, refTable, stg, notation, lbldCntnt
  , piSys, refFind
) where

import Control.Lens (makeLenses, (^.))
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)

import Drasil.Database (UID, ChunkDB)
import Language.Drasil (Stage(..), Reference, LabelledContent)

-- | Notation can be scientific or for engineering.
data Notation = Scientific
              | Engineering

-- | Printing information contains a database, a stage, and a printing configuration.
data PrintingInformation =
  PI { PrintingInformation -> ChunkDB
_sysdb :: ChunkDB
     , PrintingInformation -> Map UID Reference
_refTable :: M.Map UID Reference
     , PrintingInformation -> Stage
_stg :: Stage
     , PrintingInformation -> Notation
_notation :: Notation
     , PrintingInformation -> [LabelledContent]
_lbldCntnt :: [LabelledContent]
     }
makeLenses ''PrintingInformation

-- | Builds a document's printing information based on the system information.
piSys :: ChunkDB -> M.Map UID Reference -> Stage -> Notation -> [LabelledContent] -> PrintingInformation
piSys :: ChunkDB
-> Map UID Reference
-> Stage
-> Notation
-> [LabelledContent]
-> PrintingInformation
piSys = ChunkDB
-> Map UID Reference
-> Stage
-> Notation
-> [LabelledContent]
-> PrintingInformation
PI

refFind :: UID -> PrintingInformation -> Reference
refFind :: UID -> PrintingInformation -> Reference
refFind UID
u PrintingInformation
pinfo = Reference -> Maybe Reference -> Reference
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> Reference
forall a. HasCallStack => [Char] -> a
error ([Char] -> Reference) -> [Char] -> Reference
forall a b. (a -> b) -> a -> b
$ [Char]
"`" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ UID -> [Char]
forall a. Show a => a -> [Char]
show UID
u [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"` not found in Reference table!!!")
  (Maybe Reference -> Reference) -> Maybe Reference -> Reference
forall a b. (a -> b) -> a -> b
$ UID -> Map UID Reference -> Maybe Reference
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup UID
u (Map UID Reference -> Maybe Reference)
-> Map UID Reference -> Maybe Reference
forall a b. (a -> b) -> a -> b
$ PrintingInformation
pinfo PrintingInformation
-> Getting
     (Map UID Reference) PrintingInformation (Map UID Reference)
-> Map UID Reference
forall s a. s -> Getting a s a -> a
^. Getting (Map UID Reference) PrintingInformation (Map UID Reference)
Lens' PrintingInformation (Map UID Reference)
refTable