-- 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 #-}

module Duckling.Temperature.Helpers
  ( isLatent
  , isValueOnly
  , isSimpleTemperature
  , withUnit
  , withInterval
  , withValue
  , valueOnly
  , unitOnly
  , withMin
  , withMax
  ) where

import Data.Maybe
import Prelude

import Duckling.Dimensions.Types
import qualified Duckling.Temperature.Types as TTemperature
import Duckling.Temperature.Types (TemperatureData(..))
import Duckling.Types hiding (isLatent)

-- -----------------------------------------------------------------
-- Patterns

isLatent :: Predicate
isLatent :: Predicate
isLatent (Token Dimension a
Temperature TemperatureData{TTemperature.unit = Nothing}) =
  Bool
True
isLatent Token
_ = Bool
False

isValueOnly :: Bool -> Predicate
isValueOnly :: Bool -> Predicate
isValueOnly Bool
allowDegree (Token Dimension a
Temperature TemperatureData
                         { TTemperature.unit = u, TTemperature.value = Just _
                         , TTemperature.minValue = Nothing
                         , TTemperature.maxValue = Nothing})
      = Maybe TemperatureUnit -> Bool
forall a. Maybe a -> Bool
isNothing Maybe TemperatureUnit
u Bool -> Bool -> Bool
|| (Bool
allowDegree Bool -> Bool -> Bool
&& Maybe TemperatureUnit
u Maybe TemperatureUnit -> Maybe TemperatureUnit -> Bool
forall a. Eq a => a -> a -> Bool
== TemperatureUnit -> Maybe TemperatureUnit
forall a. a -> Maybe a
Just TemperatureUnit
TTemperature.Degree)
isValueOnly Bool
_ Token
_ = Bool
False

isSimpleTemperature :: Predicate
isSimpleTemperature :: Predicate
isSimpleTemperature (Token Dimension a
Temperature TemperatureData
                      {TTemperature.value = Just _}) = Bool
True
isSimpleTemperature Token
_ = Bool
False

-- -----------------------------------------------------------------
-- Production

withUnit :: TTemperature.TemperatureUnit -> TemperatureData -> TemperatureData
withUnit :: TemperatureUnit -> TemperatureData -> TemperatureData
withUnit TemperatureUnit
u TemperatureData
td = TemperatureData
td {unit :: Maybe TemperatureUnit
TTemperature.unit = TemperatureUnit -> Maybe TemperatureUnit
forall a. a -> Maybe a
Just TemperatureUnit
u}

withInterval :: (Double, Double) -> TemperatureData -> TemperatureData
withInterval :: (Double, Double) -> TemperatureData -> TemperatureData
withInterval (Double
from, Double
to) TemperatureData
td = TemperatureData
td
  { value :: Maybe Double
TTemperature.value = Maybe Double
forall a. Maybe a
Nothing
  , minValue :: Maybe Double
TTemperature.minValue = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
from
  , maxValue :: Maybe Double
TTemperature.maxValue = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
to
  }

withValue :: Double -> TemperatureData -> TemperatureData
withValue :: Double -> TemperatureData -> TemperatureData
withValue Double
v TemperatureData
td = TemperatureData
td
  { value :: Maybe Double
TTemperature.value = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
v
  , minValue :: Maybe Double
TTemperature.minValue = Maybe Double
forall a. Maybe a
Nothing
  , maxValue :: Maybe Double
TTemperature.maxValue = Maybe Double
forall a. Maybe a
Nothing
  }

valueOnly :: Double -> TemperatureData
valueOnly :: Double -> TemperatureData
valueOnly Double
v = TemperatureData :: Maybe TemperatureUnit
-> Maybe Double -> Maybe Double -> Maybe Double -> TemperatureData
TemperatureData
  { unit :: Maybe TemperatureUnit
TTemperature.unit = Maybe TemperatureUnit
forall a. Maybe a
Nothing
  , value :: Maybe Double
TTemperature.value = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
v
  , minValue :: Maybe Double
TTemperature.minValue = Maybe Double
forall a. Maybe a
Nothing
  , maxValue :: Maybe Double
TTemperature.maxValue = Maybe Double
forall a. Maybe a
Nothing
  }

unitOnly :: TTemperature.TemperatureUnit -> TemperatureData
unitOnly :: TemperatureUnit -> TemperatureData
unitOnly TemperatureUnit
u = TemperatureData :: Maybe TemperatureUnit
-> Maybe Double -> Maybe Double -> Maybe Double -> TemperatureData
TemperatureData
  { unit :: Maybe TemperatureUnit
TTemperature.unit = TemperatureUnit -> Maybe TemperatureUnit
forall a. a -> Maybe a
Just TemperatureUnit
u
  , value :: Maybe Double
TTemperature.value = Maybe Double
forall a. Maybe a
Nothing
  , minValue :: Maybe Double
TTemperature.minValue = Maybe Double
forall a. Maybe a
Nothing
  , maxValue :: Maybe Double
TTemperature.maxValue = Maybe Double
forall a. Maybe a
Nothing
  }

withMax :: Double -> TemperatureData -> TemperatureData
withMax :: Double -> TemperatureData -> TemperatureData
withMax Double
v TemperatureData
td = TemperatureData
td
  { value :: Maybe Double
TTemperature.value = Maybe Double
forall a. Maybe a
Nothing
  , minValue :: Maybe Double
TTemperature.minValue = Maybe Double
forall a. Maybe a
Nothing
  , maxValue :: Maybe Double
TTemperature.maxValue = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
v
  }

withMin :: Double -> TemperatureData -> TemperatureData
withMin :: Double -> TemperatureData -> TemperatureData
withMin Double
v TemperatureData
td = TemperatureData
td
  { value :: Maybe Double
TTemperature.value = Maybe Double
forall a. Maybe a
Nothing
  , minValue :: Maybe Double
TTemperature.minValue = Double -> Maybe Double
forall a. a -> Maybe a
Just Double
v
  , maxValue :: Maybe Double
TTemperature.maxValue = Maybe Double
forall a. Maybe a
Nothing
  }