module RPS.Utils (
  getResponse,
  readHand,
  askHand,
  readYesOrNo,
  askRematch
) where

import Data.Char
import System.IO

import RPS.Game

getResponse :: String -> IO Char
getResponse question = do
  putStr question
  hFlush stdout
  input <- getChar
  putStrLn ""
  return (input)

readHand :: Char -> Maybe Hand
readHand input = case (toUpper input) of
  'R' -> Just Rock
  'P' -> Just Paper
  'S' -> Just Scissors
  _   -> Nothing

askHand :: IO Hand
askHand = do
  resp <- getResponse "[R]ock, [P]aper, or [S]cissors: "
  case (readHand resp) of
    Nothing -> do
      putStrLn "Illegal hand."
      askHand
    Just hand -> return (hand)

readYesOrNo :: Char -> Maybe Bool
readYesOrNo input = case (toUpper input) of
  'Y' -> Just True
  'N' -> Just False
  _   -> Nothing

askRematch :: IO Bool
askRematch = do
  resp <- getResponse "Play again? (y or n): "
  case (readYesOrNo resp) of
    Nothing -> do
      putStrLn "Illegal response."
      askRematch
    Just yesOrNo -> return (yesOrNo)
