module Handler.Utils.SheetType
  (
    gradeSummaryWidget
  ) where

import Import
import Data.Monoid (Sum(..))
import Utils.Lens

addBonusToPoints :: SheetTypeSummary -> SheetTypeSummary
addBonusToPoints sts =
    sts & _normalSummary . _achievedPasses %~ (min passmax . (passbonus +))
        & _normalSummary . _achievedPoints %~ (min ptsmax  . (ptsbonus  +))
  where
    passmax   = sts ^. _normalSummary . _numMarkedPasses
    passbonus = sts ^. _bonusSummary  . _achievedPasses
    ptsmax    = sts ^. _normalSummary . _sumMarkedPoints
    ptsbonus  = sts ^. _bonusSummary  . _achievedPoints

gradeSummaryWidget ::  RenderMessage UniWorX msg => (Integer -> msg) -> SheetTypeSummary -> Widget
gradeSummaryWidget title sts =
  let SheetTypeSummary{..} = addBonusToPoints sts
      sumSummaries    = normalSummary <> bonusSummary <> informationalSummary & _numSheets %~ (<> numNotGraded)
      hasPasses       = positiveSum $ numSheetsPasses sumSummaries
      hasMarkedPasses = positiveSum $ numMarkedPasses sumSummaries
      hasPoints       = positiveSum $ numSheetsPoints sumSummaries
      hasMarkedPoints = positiveSum $ numMarkedPoints sumSummaries
      rowWdgts =  [ $(widgetFile "widgets/grading-summary/grading-summary-row")
                  | (sumHeader,summary) <-
                    [ (MsgSheetTypeNormal'        ,normalSummary)
                    , (MsgSheetTypeBonus'         ,bonusSummary)
                    , (MsgSheetTypeInformational' ,informationalSummary)
                  ] ]
  in if 0 == numSheets sumSummaries
        then mempty
        else $(widgetFile "widgets/grading-summary/grading-summary")
