-- Tree.hs
-- General (not binary) trees

module Data.Sifflet.Tree 
    (T.Tree(..), tree, leaf, isLeaf, treeSize, treeDepth
    , Repr(..)
    -- Since String is not an instance of Repr,
    -- we need to convert (Tree String) to (Tree Name)
    , Name(..), nameTree
    , putTree, putTreeR, putTreeRs, putTreeS
    , drawTreeShow
    )

where

import Data.Tree as T
import Text.Sifflet.Repr

-- Makers

tree :: e -> Forest e -> T.Tree e
tree root subtrees = T.Node {rootLabel = root, subForest = subtrees}

leaf :: e -> T.Tree e
leaf x = tree x []

isLeaf :: T.Tree e -> Bool
isLeaf (T.Node _root []) = True
isLeaf _ = False

treeSize :: T.Tree e -> Int
treeSize (T.Node _root subtrees) = 1 + sum (map treeSize subtrees)

treeDepth :: T.Tree e -> Int
treeDepth (T.Node _root []) = 1
treeDepth (T.Node _root subtrees) = 1 + maximum (map treeDepth subtrees)

{- tree_map or treeMap:
   removed; use (Functor) fmap instead
-}

{- tree_mapM:
   removed, use Data.Traversable.mapM instead.
-}

nameTree :: T.Tree String -> T.Tree Name
nameTree = fmap Name

putTree :: (Show e) => T.Tree e -> IO ()
putTree = putTreeS

-- putTreeR -- using repr (first part)
putTreeR :: (Repr e) => T.Tree e -> IO()
putTreeR = putStrLn . drawTree . fmap repr

-- putTreeRs -- using reprs (all parts)
putTreeRs :: (Repr e) => T.Tree e -> IO()
putTreeRs = putStrLn . drawTree . fmap reprs


-- putTreeS -- using show
putTreeS :: (Show e) => T.Tree e -> IO ()
putTreeS = putStrLn . drawTreeShow

drawTreeShow :: (Show e) => T.Tree e -> String
drawTreeShow = drawTree . fmap show