module MasterPlan.Internal.Debug ( debugSys , debugProj) where
import           Control.Monad   (forM_, replicateM_, void)
import qualified Data.Map        as M
import           MasterPlan.Data
import           Text.Printf     (printf)
debugSys ∷ ProjectSystem → IO ()
debugSys sys@(ProjectSystem bs) = void $ M.traverseWithKey printBinding bs
  where
    printBinding key b = do putStrLn "-------------------"
                            putStr $ getProjectKey key ++ " = "
                            case b of
                              BindingExpr _ e -> putStr "\n" >> debugProj sys e
                              BindingAtomic _ (Cost c) (Trust t) (Progress p) ->
                                putStrLn $ printf "(c:%.2f,t:%.2f,p:%2.f)" c t p
debugProj ∷ ProjectSystem → ProjectExpr → IO ()
debugProj sys = print' 0
   where
     ident ∷ Int → IO ()
     ident il = replicateM_ il $ putStr " |"
     print' ∷ Int → ProjectExpr → IO ()
     print' il p@(Reference (ProjectKey n))  = ident il >> putStr ("-" ++ n ++ "   ") >> ctp p
     print' il p@(Sum ps) = ident il >> putStr "-+   " >> ctp p >> forM_ ps (print' $ il+1)
     print' il p@(Sequence ps) = ident il >> putStr "->   " >> ctp p >> forM_ ps (print' $ il+1)
     print' il p@(Product ps) = ident il >> putStr "-*   " >> ctp p >> forM_ ps (print' $ il+1)
     ctp p = putStrLn $ printf " c=%.2f  t=%.2f  p=%.2f"
                               (getCost $ cost sys p)
                               (getTrust $ trust sys p)
                               (getProgress $ progress sys p)