module InputReader (boolToString, validate, formMatrixFromInput) where

import Search(word_exist)
import SearchParallel(word_exist_par)
import Data.Array
import Data.Char
import System.Exit(die)
import System.Environment(getArgs)
import Control.Applicative

-- Convert boolean to String
boolToString :: Bool -> String
boolToString True = "True"
boolToString False = "False"

wordsWhen :: String -> [Char]
wordsWhen s =  case dropWhile isDelimiter s of
                      "" -> []
                      s' -> (head w) : wordsWhen s''
                            where
                              (w, s'') = break isDelimiter s'
                    where
                      isDelimiter = (liftA2 (||) (==',') isSpace)


-- Check if string has all lowercase letters
validate :: String -> Bool
validate s = all isLower s


-- Forms grid of characters
formMatrixFromInput:: String -> Int -> Int -> Array (Int,Int) Char
formMatrixFromInput contents rows columns = array ((1,1), (rows, columns)) ([ ((i,j), list !! (columns*(i-1) + (j-1))) | i <- [1..rows], j <- [1..columns]])
                                    where
                                      list = wordsWhen contents
