{-# LANGUAGE OverloadedStrings #-}
-------------------------------------------------------------------------------
-- |
-- Module       : Generator.Printer.Section.Decode
-- Copyright    : (c) 2016 Michael Carpenter
-- License      : BSD3
-- Maintainer   : Michael Carpenter <oldmanmike.dev@gmail.com>
-- Stability    : experimental
-- Portability  : portable
--
-------------------------------------------------------------------------------
module Generator.Printer.Section.Decode
  ( docPacketGetter
  , docGetFields
  ) where

import qualified  Data.Text.Lazy as TL
import            Generator.Types
import            Generator.Printer.Types
import            Text.PrettyPrint.Leijen.Text


docPacketGetter :: Packet -> Doc
docPacketGetter packet = vsep
    [ indent 6 ((string . TL.fromStrict . pId $ packet)
                <+> "-> do")
    , docGetFields (pFields packet)
    , indent 8 (string "return $"
                <+> (string . pName $ packet)
                <+> (docFieldNames . pFields $ packet))
    ]


docGetFields :: Maybe [Field] -> Doc
docGetFields maybeFields = undefined
{-
    case maybeFields of
      Just fields -> vsep $
        fmap  (\x -> do
          case fieldType x of
            SwitchType _  -> vsep $
                [ indent 8 (string "let"
                            <+> string (TL.fromStrict (fieldName x))
                            <+> equals)
                , indent 10 (string "case"
                              <+> string (TL.fromStrict (fieldCompareTo x))
                              <+> string "of")
                , indent 12 (vsep $
                    fmap
                      (\(a,_) -> string (TL.fromStrict a) <+> string "->")
                      (fieldSwitches x))
                , indent 12 (string "_" <+> string "->")
                ]
            _         -> do
              indent 8 (string (TL.fromStrict (fieldName x))
                <+> "<-"
                <+> docFieldGetter (fieldType x))
        ) fields
      Nothing -> empty
-}

docFieldNames :: Maybe [Field] -> Doc
docFieldNames maybeFields =
    case maybeFields of
      Just fields -> hsep (fmap (text . fieldName) fields)
      Nothing     -> empty
