{-# LANGUAGE RecordWildCards #-}

module System.Cron.Internal.Describe.Types where

import Data.List  (intercalate)
import Data.Maybe (catMaybes)


data Descriptor = Descriptor {
    Descriptor -> String
pluralDesc         :: String
  , Descriptor -> String
singularDesc       :: String
  , Descriptor -> String
rangePrefix        :: String
  , Descriptor -> String
rangeSuffix        :: String
  , Descriptor -> String
rangeJoiner        :: String
  , Descriptor -> Int -> String
displayItem        :: Int -> String
  , Descriptor -> String
specificPrefix     :: String
  , Descriptor -> String
specificSuffix     :: String
  , Descriptor -> Int -> Maybe String
stepSpecificSuffix :: Int -> Maybe String
  , Descriptor -> String
listPrefix         :: String
  , Descriptor -> Maybe String
listSuffix         :: Maybe String
  }


data Month = January | February | March     | April   | May      | June     |
             July    | August   | September | October | November | December
             deriving (Int -> Month
Month -> Int
Month -> [Month]
Month -> Month
Month -> Month -> [Month]
Month -> Month -> Month -> [Month]
(Month -> Month)
-> (Month -> Month)
-> (Int -> Month)
-> (Month -> Int)
-> (Month -> [Month])
-> (Month -> Month -> [Month])
-> (Month -> Month -> [Month])
-> (Month -> Month -> Month -> [Month])
-> Enum Month
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Month -> Month
succ :: Month -> Month
$cpred :: Month -> Month
pred :: Month -> Month
$ctoEnum :: Int -> Month
toEnum :: Int -> Month
$cfromEnum :: Month -> Int
fromEnum :: Month -> Int
$cenumFrom :: Month -> [Month]
enumFrom :: Month -> [Month]
$cenumFromThen :: Month -> Month -> [Month]
enumFromThen :: Month -> Month -> [Month]
$cenumFromTo :: Month -> Month -> [Month]
enumFromTo :: Month -> Month -> [Month]
$cenumFromThenTo :: Month -> Month -> Month -> [Month]
enumFromThenTo :: Month -> Month -> Month -> [Month]
Enum, Month
Month -> Month -> Bounded Month
forall a. a -> a -> Bounded a
$cminBound :: Month
minBound :: Month
$cmaxBound :: Month
maxBound :: Month
Bounded, Int -> Month -> ShowS
[Month] -> ShowS
Month -> String
(Int -> Month -> ShowS)
-> (Month -> String) -> ([Month] -> ShowS) -> Show Month
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Month -> ShowS
showsPrec :: Int -> Month -> ShowS
$cshow :: Month -> String
show :: Month -> String
$cshowList :: [Month] -> ShowS
showList :: [Month] -> ShowS
Show)


safeIntToMonth :: Int -> Month
safeIntToMonth :: Int -> Month
safeIntToMonth = Int -> Month
forall a. Enum a => Int -> a
toEnum (Int -> Month) -> (Int -> Int) -> Int -> Month
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Num a => a -> a -> a
subtract Int
1 (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
12 (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1


data Weekday = Sunday | Monday | Tuesday | Wednesday |
               Thursday | Friday | Saturday | Sunday2
               deriving (Int -> Weekday
Weekday -> Int
Weekday -> [Weekday]
Weekday -> Weekday
Weekday -> Weekday -> [Weekday]
Weekday -> Weekday -> Weekday -> [Weekday]
(Weekday -> Weekday)
-> (Weekday -> Weekday)
-> (Int -> Weekday)
-> (Weekday -> Int)
-> (Weekday -> [Weekday])
-> (Weekday -> Weekday -> [Weekday])
-> (Weekday -> Weekday -> [Weekday])
-> (Weekday -> Weekday -> Weekday -> [Weekday])
-> Enum Weekday
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Weekday -> Weekday
succ :: Weekday -> Weekday
$cpred :: Weekday -> Weekday
pred :: Weekday -> Weekday
$ctoEnum :: Int -> Weekday
toEnum :: Int -> Weekday
$cfromEnum :: Weekday -> Int
fromEnum :: Weekday -> Int
$cenumFrom :: Weekday -> [Weekday]
enumFrom :: Weekday -> [Weekday]
$cenumFromThen :: Weekday -> Weekday -> [Weekday]
enumFromThen :: Weekday -> Weekday -> [Weekday]
$cenumFromTo :: Weekday -> Weekday -> [Weekday]
enumFromTo :: Weekday -> Weekday -> [Weekday]
$cenumFromThenTo :: Weekday -> Weekday -> Weekday -> [Weekday]
enumFromThenTo :: Weekday -> Weekday -> Weekday -> [Weekday]
Enum, Weekday
Weekday -> Weekday -> Bounded Weekday
forall a. a -> a -> Bounded a
$cminBound :: Weekday
minBound :: Weekday
$cmaxBound :: Weekday
maxBound :: Weekday
Bounded, Int -> Weekday -> ShowS
[Weekday] -> ShowS
Weekday -> String
(Int -> Weekday -> ShowS)
-> (Weekday -> String) -> ([Weekday] -> ShowS) -> Show Weekday
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Weekday -> ShowS
showsPrec :: Int -> Weekday -> ShowS
$cshow :: Weekday -> String
show :: Weekday -> String
$cshowList :: [Weekday] -> ShowS
showList :: [Weekday] -> ShowS
Show)


safeIntToWeekDay :: Int -> Weekday
safeIntToWeekDay :: Int -> Weekday
safeIntToWeekDay Int
n
  | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
7    = Weekday
Sunday
  | Bool
otherwise = Int -> Weekday
forall a. Enum a => Int -> a
toEnum (Int -> Weekday) -> (Int -> Int) -> Int -> Weekday
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
6 (Int -> Weekday) -> Int -> Weekday
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n


data Verbosity = Verbose | NotVerbose


data TimeFormat = Hour24 | Hour12


data DescribedValue = Concrete String
                    | Every String


instance Show DescribedValue where
  show :: DescribedValue -> String
show (Concrete String
s) = String
s
  show (Every String
s)    = String
s


data Time = ConcreteTime String
          | Other (Maybe DescribedValue) (Maybe DescribedValue)


instance Show Time where
  show :: Time -> String
show (ConcreteTime String
s) = String
s
  show (Other Maybe DescribedValue
md1 Maybe DescribedValue
md2)  = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ([String] -> String)
-> ([DescribedValue] -> [String]) -> [DescribedValue] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                          (DescribedValue -> String) -> [DescribedValue] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map DescribedValue -> String
forall a. Show a => a -> String
show         ([DescribedValue] -> String) -> [DescribedValue] -> String
forall a b. (a -> b) -> a -> b
$ [Maybe DescribedValue] -> [DescribedValue]
forall a. [Maybe a] -> [a]
catMaybes [Maybe DescribedValue
md1, Maybe DescribedValue
md2]


data Description = Desc {
    Description -> Time
_time   :: Time
  , Description -> Maybe DescribedValue
_dom    :: Maybe DescribedValue
  , Description -> Maybe DescribedValue
_month  :: Maybe DescribedValue
  , Description -> Maybe DescribedValue
_dow    :: Maybe DescribedValue
  }


instance Show Description where
  show :: Description -> String
show Desc{Maybe DescribedValue
Time
_time :: Description -> Time
_dom :: Description -> Maybe DescribedValue
_month :: Description -> Maybe DescribedValue
_dow :: Description -> Maybe DescribedValue
_time :: Time
_dom :: Maybe DescribedValue
_month :: Maybe DescribedValue
_dow :: Maybe DescribedValue
..} = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ([String] -> String)
-> ([DescribedValue] -> [String]) -> [DescribedValue] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                  (:) (Time -> String
forall a. Show a => a -> String
show Time
_time) ([String] -> [String])
-> ([DescribedValue] -> [String]) -> [DescribedValue] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                  (DescribedValue -> String) -> [DescribedValue] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map DescribedValue -> String
forall a. Show a => a -> String
show         ([DescribedValue] -> String) -> [DescribedValue] -> String
forall a b. (a -> b) -> a -> b
$ [Maybe DescribedValue] -> [DescribedValue]
forall a. [Maybe a] -> [a]
catMaybes [Maybe DescribedValue
_dom, Maybe DescribedValue
_dow, Maybe DescribedValue
_month]