content: -- Copyright (c) 2016-present, Facebook, Inc.
  -- All rights reserved.
  --
  -- This source code is licensed under the BSD-style license found in the
  -- LICENSE file in the root directory of this source tree.
  
  
  {-# LANGUAGE GADTs #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  module Duckling.Duration.HU.Rules
    ( rules ) where
  
  import Control.Monad (join)
  import Data.String
  import Prelude
  import qualified Data.Text as Text
  
  import Duckling.Dimensions.Types
  import Duckling.Duration.Helpers
  import Duckling.Numeral.Helpers (parseInt, parseInteger)
  import Duckling.Numeral.Types (NumeralData(..))
  import Duckling.Regex.Types
  import Duckling.Types
  import qualified Duckling.Numeral.Types as TNumeral
  import qualified Duckling.TimeGrain.Types as TG
  
  ruleDurationQuarterOfAnHour :: Rule
  ruleDurationQuarterOfAnHour = Rule
    { name = "quarter of an hour"
    , pattern =
      [ regex "(negyed[\\s-]?\x00F3ra)"
      ]
    , prod = \_ -> Just . Token Duration $ duration TG.Minute 15
    }
  
  ruleDurationHalfAnHour :: Rule
  ruleDurationHalfAnHour = Rule
    { name = "half an hour"
    , pattern =
      [ regex "(f\x00E9l[\\s-]?\x00F3ra)"
      ]
    , prod = \_ -> Just . Token Duration $ duration TG.Minute 30
    }
  
  ruleDurationThreeQuartersOfAnHour :: Rule
  ruleDurationThreeQuartersOfAnHour = Rule
    { name = "three-quarters of an hour"
    , pattern =
      [ regex "(3/4[\\s-]?óra|h\x00E1romnegyed[\\s-]?\x00F3ra)"
      ]
    , prod = \_ -> Just . Token Duration $ duration TG.Minute 45
    }
  
  ruleDurationDotNumeralHours :: Rule
  ruleDurationDotNumeralHours = Rule
    { name = "number.number hours"
    , pattern =
      [ regex "(\\d+)\\.(\\d+) óra"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (h:m:_)):_) -> do
          hh <- parseInteger h
          mnum <- parseInteger m
          let mden = 10 ^ Text.length m
          Just . Token Duration $ minutesFromHourMixedFraction hh mnum mden
        _ -> Nothing
    }
  
  ruleDurationAndHalfHour :: Rule
  ruleDurationAndHalfHour = Rule
    { name = "<integer> and an half hour"
    , pattern =
      [ Predicate isNatural
      , regex "\x00E9s f\x00E9l[\\s-]?\x00F3ra"
      ]
    , prod = \tokens -> case tokens of
        (Token Numeral NumeralData{TNumeral.value = v}:_) ->
          Just . Token Duration . duration TG.Minute $ 30 + 60 * floor v
        _ -> Nothing
    }
  
  rules :: [Rule]
  rules =
    [ ruleDurationQuarterOfAnHour
    , ruleDurationHalfAnHour
    , ruleDurationThreeQuartersOfAnHour
    , ruleDurationDotNumeralHours
    , ruleDurationAndHalfHour
    ]
  