ig          -- ^ The 'Config' to lookup page configuration data from.
         -> [Category]      -- ^ A list of 'Category's to pass on to the finished row.
         -> [Header]        -- ^ A list of 'Header's to select correct 'Text' from the given list.
         -> Bool            -- ^ If 'True' this will make the course available this year.
         -> [Text]          -- ^ Used to fill the columns of the row with values from the source 'Table'.
         -> Course          -- ^ The finished row.
toCourse Config{..} cats hs iscur xs =
    (cats, Map.adjust doLang (columnTitle columnHeaders "colLang") $
           Map.adjust doRepeats (columnTitle columnHeaders "colRepeats") $
           Map.insert "pidetään" (if iscur then "this-year" else "next-year") $
           Map.insert "lukukausi" lukukausi vals)
  where
    vals      = Map.fromList $ zip hs $ map normalize xs
    lukukausi = fromMaybe "syksy, kevät" $ Map.lookup (columnTitle columnHeaders "colPeriod") vals >>= toLukukausi
    toLukukausi x
        | "tammikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "helmikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "maaliskuu" `T.isInfixOf` x                                    = Just "kevät"
        | "huhtikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "toukokuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "kesäkuu"   `T.isInfixOf` x                                    = Just "kesä"
        | "heinäkuu"  `T.isInfixOf` x                                    = Just "kesä"
        | "elokuu"    `T.isInfixOf` x                                    = Just "kesä, syksy"
        | "syyskuu"   `T.isInfixOf` x                                    = Just "syksy"
        | "lokakuu"   `T.isInfixOf` x                                    = Just "syksy"
        | "marraskuu" `T.isInfixOf` x                                    = Just "syksy"
        | "joulukuu"  `T.isInfixOf` x                                    = Just "syksy"
        | x == "I"    || x == "II"     || x == "I-II"                    = Just "syksy"
        | x == "III"  || x == "IV"     || x == "III-IV"                  = Just "kevät"
        | x == "I-IV" || x == "II-III" || x == "I, III" || x == "II, IV" = Just "syksy, kevät"
        | x == "V"                                                       = Just "kesä"
        | "kevät" `T.isInfixOf` x                                        = Just "kevät"
        | "syksy" `T.isInfixOf` x                                        = Just "syksy"
        | "kesä"  `T.isInfixOf` x                                        = Just "kesä"
        | otherwise                                                      = Nothing


{- | Change the format of the @colLang@ column in the source 'Table' to be in
    the correct format.

        * Replace different ways of writing languages to the correct 'Lang'
          format.

        * Replace word separators to single spaces.
-}
doLang :: Text      -- ^ The line o
toCourse :: Config          -- ^ The 'Config' to lookup page configuration data from.
         -> [Category]      -- ^ A list of 'Category's to pass on to the finished row.
         -> [Header]        -- ^ A list of 'Header's to select correct 'Text' from the given list.
         -> Bool            -- ^ If 'True' this will make the course available this year.
         -> [Text]          -- ^ Used to fill the columns of the row with values from the source 'Table'.
         -> Course
toCourse Config{..} cats hs iscur xs =
    (cats, Map.adjust doLang (columnTitle columnHeaders "colLang") $
           Map.adjust doRepeats (columnTitle columnHeaders "colRepeats") $
           Map.insert "pidetään" (if iscur then "this-year" else "next-year") $
           Map.insert "lukukausi" lukukausi vals)
  where
    vals      = Map.fromList $ zip hs $ map normalize xs
    lukukausi = fromMaybe "syksy, kevät" $ Map.lookup (columnTitle columnHeaders "colPeriod") vals >>= toLukukausi
    toLukukausi x
        | "tammikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "helmikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "maaliskuu" `T.isInfixOf` x                                    = Just "kevät"
        | "huhtikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "toukokuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "kesäkuu"   `T.isInfixOf` x                                    = Just "kesä"
        | "heinäkuu"  `T.isInfixOf` x                                    = Just "kesä"
        | "elokuu"    `T.isInfixOf` x                                    = Just "kesä, syksy"
        | "syyskuu"   `T.isInfixOf` x                                    = Just "syksy"
        | "lokakuu"   `T.isInfixOf` x                                    = Just "syksy"
        | "marraskuu" `T.isInfixOf` x                                    = Just "syksy"
        | "joulukuu"  `T.isInfixOf` x                                    = Just "syksy"
        | x == "I"    || x == "II"     || x == "I-II"                    = Just "syksy"
        | x == "III"  || x == "IV"     || x == "III-IV"                  = Just "kevät"
        | x == "I-IV" || x == "II-III" || x == "I, III" || x == "II, IV" = Just "syksy, kevät"
        | x == "V"                                                       = Just "kesä"
        | "kevät" `T.isInfixOf` x                                        = Just "kevät"
        | "syksy" `T.isInfixOf` x                                        = Just "syksy"
        | "kesä"  `T.isInfixOf` x                                        = Just "kesä"
        | otherwise                                                      = Nothing


{- | Change the format of the @colLang@ column in the source 'Table' to be in
    the correct format.

        * Replace different ways of writing languages to the correct 'Lang'
          format.

        * Replace word separators to single spaces.
-}
toCourse Config{..} cats hs iscur xs =
    (cats, Map.adjust doLang (columnTitle columnHeaders "colLang") $
           Map.adjust doRepeats (columnTitle columnHeaders "colRepeats") $
           Map.insert "pidetään" (if iscur then "this-year" else "next-year") $
           Map.insert "lukukausi" lukukausi vals)
  where
    vals      = Map.fromList $ zip hs $ map normalize xs
    lukukausi = fromMaybe "syksy, kevät" $ Map.lookup (columnTitle columnHeaders "colPeriod") vals >>= toLukukausi
    toLukukausi x
        | "tammikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "helmikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "maaliskuu" `T.isInfixOf` x                                    = Just "kevät"
        | "huhtikuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "toukokuu"  `T.isInfixOf` x                                    = Just "kevät"
        | "kesäkuu"   `T.isInfixOf` x                                    = Just "kesä"
        | "heinäkuu"  `T.isInfixOf` x                                    = Just "kesä"
        | "elokuu"    `T.isInfixOf` x                                    = Just "kesä, syksy"
        | "syyskuu"   `T.isInfixOf` x                                    = Just "syksy"
        | "lokakuu"   `T.isInfixOf` x                                    = Just "syksy"
        | "marraskuu" `T.isInfixOf` x                                    = Just "syksy"
        | "joulukuu"  `T.isInfixOf` x                                    = Just "syksy"
        | x == "I"    || x == "II"     || x == "I-II"                    = Just "syksy"
        | x == "III"  || x == "IV"     || x == "III-IV"                  = Just "kevät"
        | x == "I-IV" || x == "II-III" || x == "I, III" || x == "II, IV" = Just "syksy, kevät"
        | x == "V"                                                       = Just "kesä"
        | "kevät" `T.isInfixOf` x                                        = Just "kevät"
        | "syksy" `T.isInfixOf` x                                        = Just "syksy"
        | "kesä"  `T.isInfixOf` x                                        = Just "kesä"
        | otherwise                                                      = Nothing


{- | Change the format of the @colLang@ column in the source 'Table' to be in
    the correct format.

        * Replace different ways of writing languages to the correct 'Lang'
          format.

        * Replace word separators to single spaces.
-}