module System.Cron.Internal.Describe.Utils
(
  viewHour
, viewMinute
, viewMinuteRange
, viewHourList
, allWords
, cap
, joinWords
) where

import           Data.Char               (toUpper)
import           Data.List.NonEmpty      (NonEmpty)
import           System.Cron.Types
import           System.Cron.Internal.Describe.Time

viewHour :: HourSpec -> Maybe Hour
viewHour :: HourSpec -> Maybe Hour
viewHour = (Int -> Hour) -> CronField -> Maybe Hour
forall a. (Int -> a) -> CronField -> Maybe a
viewSpecificTime Int -> Hour
Hour (CronField -> Maybe Hour)
-> (HourSpec -> CronField) -> HourSpec -> Maybe Hour
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HourSpec -> CronField
hourSpec


viewMinute :: MinuteSpec -> Maybe Minute
viewMinute :: MinuteSpec -> Maybe Minute
viewMinute = (Int -> Minute) -> CronField -> Maybe Minute
forall a. (Int -> a) -> CronField -> Maybe a
viewSpecificTime Int -> Minute
Minute (CronField -> Maybe Minute)
-> (MinuteSpec -> CronField) -> MinuteSpec -> Maybe Minute
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MinuteSpec -> CronField
minuteSpec


viewMinuteRange :: MinuteSpec -> Maybe (Minute, Minute)
viewMinuteRange :: MinuteSpec -> Maybe (Minute, Minute)
viewMinuteRange = CronField -> Maybe (Minute, Minute)
viewRange (CronField -> Maybe (Minute, Minute))
-> (MinuteSpec -> CronField)
-> MinuteSpec
-> Maybe (Minute, Minute)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MinuteSpec -> CronField
minuteSpec
  where viewRange :: CronField -> Maybe (Minute, Minute)
viewRange (Field (RangeField' RangeField
rf)) = (Minute, Minute) -> Maybe (Minute, Minute)
forall a. a -> Maybe a
Just (Int -> Minute
Minute (Int -> Minute) -> Int -> Minute
forall a b. (a -> b) -> a -> b
$ RangeField -> Int
rfBegin RangeField
rf,
                                                   Int -> Minute
Minute (Int -> Minute) -> Int -> Minute
forall a b. (a -> b) -> a -> b
$ RangeField -> Int
rfEnd RangeField
rf)
        viewRange CronField
_                        = Maybe (Minute, Minute)
forall a. Maybe a
Nothing


viewHourList :: HourSpec -> Maybe (NonEmpty BaseField)
viewHourList :: HourSpec -> Maybe (NonEmpty BaseField)
viewHourList = CronField -> Maybe (NonEmpty BaseField)
viewList (CronField -> Maybe (NonEmpty BaseField))
-> (HourSpec -> CronField)
-> HourSpec
-> Maybe (NonEmpty BaseField)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HourSpec -> CronField
hourSpec
  where viewList :: CronField -> Maybe (NonEmpty BaseField)
viewList (ListField NonEmpty BaseField
ne) = NonEmpty BaseField -> Maybe (NonEmpty BaseField)
forall a. a -> Maybe a
Just NonEmpty BaseField
ne
        viewList CronField
_              = Maybe (NonEmpty BaseField)
forall a. Maybe a
Nothing


viewSpecificTime :: (Int -> a) -> CronField -> Maybe a
viewSpecificTime :: forall a. (Int -> a) -> CronField -> Maybe a
viewSpecificTime Int -> a
f (Field (SpecificField' SpecificField
s)) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> (Int -> a) -> Int -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
f (Int -> Maybe a) -> Int -> Maybe a
forall a b. (a -> b) -> a -> b
$ SpecificField -> Int
specificField SpecificField
s
viewSpecificTime Int -> a
_ CronField
_                          = Maybe a
forall a. Maybe a
Nothing


allWords :: [String] -> String
allWords :: [String] -> String
allWords = [String] -> String
unwords ([String] -> String)
-> ([String] -> [String]) -> [String] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)


cap :: String -> String
cap :: String -> String
cap []     = []
cap (Char
x:String
xs) = Char -> Char
toUpper Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs


joinWords :: [String] -> String
joinWords :: [String] -> String
joinWords [] = []
joinWords [String
x] = String
x
joinWords [String
x, String
y] = String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" and " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
y
joinWords (String
x:[String]
xs) = String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
joinWords [String]
xs