{-# LANGUAGE MultiWayIf #-}

module Octavo.Controller where

import System.IO
import Octavo.StrParse

data Command = Unknown | Quit | Help | Load

bannerPrompt :: IO ()
bannerPrompt = do
    putStrLn "Octavo reference/citation system v2016.01."
    putStrLn "Copyright (c) Antony V 2016, see LICENSE."
    putStrLn "Type \"help\" at the REPL to view help."

commandHelp :: IO ()
commandHelp = do
    putStrLn ""
    putStrLn "Possible commands are: "
    putStrLn "        help            - Prints this message"
    putStrLn "        quit            - Exit the program"

loadRepo :: [String] -> IO ()
loadRepo args = do
    putStrLn "Loading Repository..."

readCommandAtPrompt :: IO (Command,[String])
readCommandAtPrompt = do
    putStr("octavo> ")
    hFlush stdout
    l <- getLine
    let (strCmd:opts) = splitQuotes l
    let cmd = if | strCmd == "quit" -> Quit
                 | strCmd == "help" -> Help
                 | strCmd == "load" -> Load
                 | otherwise        -> Unknown
    return (cmd,opts)

repl :: IO ()
repl = do
    (cmd,opts) <- readCommandAtPrompt
    case cmd of
        Quit -> return ()
        Help -> commandHelp
        Load -> loadRepo opts
        Unknown -> putStrLn $ "Unknown command"
    repl
