module TD.Data.OptionValue
  (OptionValue(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T

-- | Represents the value of an option
data OptionValue
  = OptionValueBoolean -- ^ Represents a boolean option
    { OptionValue -> Maybe Bool
value :: Maybe Bool -- ^ The value of the option
    }
  | OptionValueEmpty -- ^ Represents an unknown option or an option which has a default value
  | OptionValueInteger -- ^ Represents an integer option
    { OptionValue -> Maybe Int
_value :: Maybe Int -- ^ The value of the option
    }
  | OptionValueString -- ^ Represents a string option
    { OptionValue -> Maybe Text
__value :: Maybe T.Text -- ^ The value of the option
    }
  deriving (OptionValue -> OptionValue -> Bool
(OptionValue -> OptionValue -> Bool)
-> (OptionValue -> OptionValue -> Bool) -> Eq OptionValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OptionValue -> OptionValue -> Bool
== :: OptionValue -> OptionValue -> Bool
$c/= :: OptionValue -> OptionValue -> Bool
/= :: OptionValue -> OptionValue -> Bool
Eq, Int -> OptionValue -> ShowS
[OptionValue] -> ShowS
OptionValue -> String
(Int -> OptionValue -> ShowS)
-> (OptionValue -> String)
-> ([OptionValue] -> ShowS)
-> Show OptionValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OptionValue -> ShowS
showsPrec :: Int -> OptionValue -> ShowS
$cshow :: OptionValue -> String
show :: OptionValue -> String
$cshowList :: [OptionValue] -> ShowS
showList :: [OptionValue] -> ShowS
Show)

instance I.ShortShow OptionValue where
  shortShow :: OptionValue -> String
shortShow OptionValueBoolean
    { value :: OptionValue -> Maybe Bool
value = Maybe Bool
value_
    }
      = String
"OptionValueBoolean"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"value" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
value_
        ]
  shortShow OptionValue
OptionValueEmpty
      = String
"OptionValueEmpty"
  shortShow OptionValueInteger
    { _value :: OptionValue -> Maybe Int
_value = Maybe Int
_value_
    }
      = String
"OptionValueInteger"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_value" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_value_
        ]
  shortShow OptionValueString
    { __value :: OptionValue -> Maybe Text
__value = Maybe Text
__value_
    }
      = String
"OptionValueString"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"__value" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
__value_
        ]

instance AT.FromJSON OptionValue where
  parseJSON :: Value -> Parser OptionValue
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"optionValueBoolean" -> Value -> Parser OptionValue
parseOptionValueBoolean Value
v
      String
"optionValueEmpty"   -> OptionValue -> Parser OptionValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OptionValue
OptionValueEmpty
      String
"optionValueInteger" -> Value -> Parser OptionValue
parseOptionValueInteger Value
v
      String
"optionValueString"  -> Value -> Parser OptionValue
parseOptionValueString Value
v
      String
_                    -> Parser OptionValue
forall a. Monoid a => a
mempty
    
    where
      parseOptionValueBoolean :: A.Value -> AT.Parser OptionValue
      parseOptionValueBoolean :: Value -> Parser OptionValue
parseOptionValueBoolean = String
-> (Object -> Parser OptionValue) -> Value -> Parser OptionValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"OptionValueBoolean" ((Object -> Parser OptionValue) -> Value -> Parser OptionValue)
-> (Object -> Parser OptionValue) -> Value -> Parser OptionValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
value_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        OptionValue -> Parser OptionValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OptionValue -> Parser OptionValue)
-> OptionValue -> Parser OptionValue
forall a b. (a -> b) -> a -> b
$ OptionValueBoolean
          { value :: Maybe Bool
value = Maybe Bool
value_
          }
      parseOptionValueInteger :: A.Value -> AT.Parser OptionValue
      parseOptionValueInteger :: Value -> Parser OptionValue
parseOptionValueInteger = String
-> (Object -> Parser OptionValue) -> Value -> Parser OptionValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"OptionValueInteger" ((Object -> Parser OptionValue) -> Value -> Parser OptionValue)
-> (Object -> Parser OptionValue) -> Value -> Parser OptionValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_value_ <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        OptionValue -> Parser OptionValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OptionValue -> Parser OptionValue)
-> OptionValue -> Parser OptionValue
forall a b. (a -> b) -> a -> b
$ OptionValueInteger
          { _value :: Maybe Int
_value = Maybe Int
_value_
          }
      parseOptionValueString :: A.Value -> AT.Parser OptionValue
      parseOptionValueString :: Value -> Parser OptionValue
parseOptionValueString = String
-> (Object -> Parser OptionValue) -> Value -> Parser OptionValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"OptionValueString" ((Object -> Parser OptionValue) -> Value -> Parser OptionValue)
-> (Object -> Parser OptionValue) -> Value -> Parser OptionValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
__value_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        OptionValue -> Parser OptionValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OptionValue -> Parser OptionValue)
-> OptionValue -> Parser OptionValue
forall a b. (a -> b) -> a -> b
$ OptionValueString
          { __value :: Maybe Text
__value = Maybe Text
__value_
          }
  parseJSON Value
_ = Parser OptionValue
forall a. Monoid a => a
mempty

instance AT.ToJSON OptionValue where
  toJSON :: OptionValue -> Value
toJSON OptionValueBoolean
    { value :: OptionValue -> Maybe Bool
value = Maybe Bool
value_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"optionValueBoolean"
        , Key
"value" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
value_
        ]
  toJSON OptionValue
OptionValueEmpty
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"optionValueEmpty"
        ]
  toJSON OptionValueInteger
    { _value :: OptionValue -> Maybe Int
_value = Maybe Int
_value_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"optionValueInteger"
        , Key
"value" Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (Int -> Value) -> Maybe Int -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Value
I.writeInt64  Maybe Int
_value_
        ]
  toJSON OptionValueString
    { __value :: OptionValue -> Maybe Text
__value = Maybe Text
__value_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"optionValueString"
        , Key
"value" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
__value_
        ]