module Main(main) where

import Data.Tree
import Random
import Control.Monad
import Char
import IConv
import CWString
import Word
import qualified LocaleIO

--mPutStrLn s = putStrLn $ map (chr . fromIntegral) $ toUTF s 
--mPutStrLn s = stringToBytes s >>= \xs -> putStrLn (map (chr . fromIntegral) xs)
mGetContents = do
    c <- getContents 
    bytesToString (map (fromIntegral . ord) c :: [Word8])

mPutStrLn s = (withCString s peekCAString) >>= putStrLn 

lTee     = chr 0x251C
vLine    = chr 0x2502

--mPutStrLn s = (withIConv "" "UTF-32" $ \ic -> convertRaw ic s) >>= \xs -> putStrLn (map (chr . fromIntegral) (xs::[Word8]))  
--mGetStr :: IO String
--mGetStr = do
--    c <- getContents 
--    withIConv "UTF-32" "" $ \ic -> do
--    convertRaw ic (map (fromIntegral . ord) c :: [Word8])

--randomTree :: Int -> IO (Tree Char)
randomTree max = do
    --max <- return $ maximum [0,max]
    n <- randomRIO (0,max)          
    s <- replicateM n (randomTree $ max - 1 )
    n <- randomRIO (1,10)
    v <- replicateM n $ randomRIO ('a','z');
    return (Node v s)

main = do
    s <- mGetContents
    print s
    mPutStrLn s
    t <- randomTree 7
    mPutStrLn (unlines $ draw $  t)

draw :: Tree String -> [String]
draw (Node x ts0) = x : drawSubTrees ts0
  where drawSubTrees [] = []
        drawSubTrees [t] =
                {-[vLine] :-} shift [chr 0x2570, chr 0x2574] "  " (draw t)
        drawSubTrees (t:ts) =
                {-[vLine] :-} shift [lTee, chr 0x2574] [vLine,  ' '] (draw t) ++ drawSubTrees ts

        shift first other = zipWith (++) (first : repeat other)
        --vLine = chr 0x254F

