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

import Import

import Utils.Form
import Utils.Schedule.Types.ScheduleOffset
import Utils.Schedule.Types.ScheduleView


data ScheduleOptionsAction = ScheduleSetView
                           | ScheduleSetOffset ScheduleOffset
                           | ScheduleSetDefault
  deriving (Eq, Ord, Show, Read, Generic, Typeable)

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


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

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

instance Button UniWorX ScheduleOptions where
  btnClasses = const [BCIsButton]
  btnLabel ScheduleOptions{..} = case scheduleOptionsAction of
    ScheduleSetDefault  -> i18n MsgScheduleReset
    ScheduleSetView     -> case scheduleView of
      ScheduleViewWeek  -> i18n MsgScheduleViewWeek
    ScheduleSetOffset o -> case scheduleView of
      ScheduleViewWeek  -> let iconTooltipMessage i m = iconTooltip (i18n m) (Just i) True
                               o' = offsetInDays o
                            in if | o' <= (-7) -> iconTooltipMessage IconFastBackward   MsgScheduleOffsetWeekBackwardWeek
                                  | o' <   0   -> iconTooltipMessage IconBackward     . MsgScheduleOffsetWeekBackwardDays $ abs o'
                                  | o' ==  0   -> iconTooltipMessage IconCurrent        MsgScheduleOffsetWeekCurrent
                                  | o' <   7   -> iconTooltipMessage IconForward      . MsgScheduleOffsetWeekForwardDays  $ abs o'
                                  | otherwise  -> iconTooltipMessage IconFastForward    MsgScheduleOffsetWeekForwardWeek
