module Handler.Utils.Schedule.Types.ScheduleOptions
  ( ScheduleView(..)
  , ScheduleOffset(..)
  , ScheduleOptionsAction(..)
  , ScheduleOptions(..)
  ) where

import Import

import Utils.Form


data ScheduleView = ScheduleViewWeek
  deriving (Eq, Ord, Enum, Show, Read, Generic, Typeable)

instance Bounded ScheduleView where
  minBound = maxBound
  maxBound = ScheduleViewWeek
instance Finite ScheduleView
instance Universe ScheduleView

derivePathPiece ''ScheduleView (camelToPathPiece' 1) "_"


data ScheduleOffset = ScheduleOffsetNone
                    | ScheduleOffsetDays Int
  deriving (Eq, Ord, Show, Read, Generic, Typeable)

derivePathPiece ''ScheduleOffset (camelToPathPiece' 1) "_"


data ScheduleOptionsAction = ScheduleSetView
                           | ScheduleSetOffset Int -- TODO: use ScheduleOffset instead of Int
                           | ScheduleSetDefault
  deriving (Eq, Ord, Show, Read, Generic, Typeable)

derivePathPiece ''ScheduleOptionsAction (camelToPathPiece' 1) "_"


data ScheduleOptions = ScheduleOptions
                       { scheduleView   :: ScheduleView
                       , scheduleOffset :: ScheduleOffset
                       , scheduleOptionsAction :: ScheduleOptionsAction
                       }
  deriving (Generic, Typeable)

derivePathPiece ''ScheduleOptions (camelToPathPiece' 1) "__"

instance Button UniWorX ScheduleOptions where
  btnClasses = const [BCIsButton]
  btnLabel ScheduleOptions{..} = case scheduleOptionsAction of
    ScheduleSetDefault    -> [whamlet| _{MsgScheduleReset} |]
    ScheduleSetView       -> case scheduleView of
      ScheduleViewWeek    -> [whamlet| _{MsgScheduleViewWeek} |]
    ScheduleSetOffset d   -> case scheduleView of
      ScheduleViewWeek    -> let iconTooltipMessage i m = iconTooltip [whamlet| _{m} |] (Just i) True in case d of
        (-7) -> [whamlet| ^{iconTooltipMessage IconFastBackward MsgScheduleOffsetWeekBackwardWeek} |]
        (-1) -> [whamlet| ^{iconTooltipMessage IconBackward     MsgScheduleOffsetWeekBackwardDay} |]
        0    -> [whamlet| ^{iconTooltipMessage IconCurrent      MsgScheduleOffsetWeekCurrent} |]
        1    -> [whamlet| ^{iconTooltipMessage IconForward      MsgScheduleOffsetWeekForwardDay} |]
        7    -> [whamlet| ^{iconTooltipMessage IconFastForward  MsgScheduleOffsetWeekForwardWeek} |]
        _    -> [whamlet| #{tshow d} |]
