module TD.Data.StatisticalGraph
  (StatisticalGraph(..)) 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

-- | Describes a statistical graph
data StatisticalGraph
  = StatisticalGraphData -- ^ A graph data
    { StatisticalGraph -> Maybe Text
json_data  :: Maybe T.Text -- ^ Graph data in JSON format
    , StatisticalGraph -> Maybe Text
zoom_token :: Maybe T.Text -- ^ If non-empty, a token which can be used to receive a zoomed in graph
    }
  | StatisticalGraphAsync -- ^ The graph data to be asynchronously loaded through getStatisticalGraph
    { StatisticalGraph -> Maybe Text
token :: Maybe T.Text -- ^ The token to use for data loading
    }
  | StatisticalGraphError -- ^ An error message to be shown to the user instead of the graph
    { StatisticalGraph -> Maybe Text
error_message :: Maybe T.Text -- ^ The error message
    }
  deriving (StatisticalGraph -> StatisticalGraph -> Bool
(StatisticalGraph -> StatisticalGraph -> Bool)
-> (StatisticalGraph -> StatisticalGraph -> Bool)
-> Eq StatisticalGraph
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StatisticalGraph -> StatisticalGraph -> Bool
== :: StatisticalGraph -> StatisticalGraph -> Bool
$c/= :: StatisticalGraph -> StatisticalGraph -> Bool
/= :: StatisticalGraph -> StatisticalGraph -> Bool
Eq, Int -> StatisticalGraph -> ShowS
[StatisticalGraph] -> ShowS
StatisticalGraph -> String
(Int -> StatisticalGraph -> ShowS)
-> (StatisticalGraph -> String)
-> ([StatisticalGraph] -> ShowS)
-> Show StatisticalGraph
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StatisticalGraph -> ShowS
showsPrec :: Int -> StatisticalGraph -> ShowS
$cshow :: StatisticalGraph -> String
show :: StatisticalGraph -> String
$cshowList :: [StatisticalGraph] -> ShowS
showList :: [StatisticalGraph] -> ShowS
Show)

instance I.ShortShow StatisticalGraph where
  shortShow :: StatisticalGraph -> String
shortShow StatisticalGraphData
    { json_data :: StatisticalGraph -> Maybe Text
json_data  = Maybe Text
json_data_
    , zoom_token :: StatisticalGraph -> Maybe Text
zoom_token = Maybe Text
zoom_token_
    }
      = String
"StatisticalGraphData"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"json_data"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
json_data_
        , String
"zoom_token" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
zoom_token_
        ]
  shortShow StatisticalGraphAsync
    { token :: StatisticalGraph -> Maybe Text
token = Maybe Text
token_
    }
      = String
"StatisticalGraphAsync"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"token" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
token_
        ]
  shortShow StatisticalGraphError
    { error_message :: StatisticalGraph -> Maybe Text
error_message = Maybe Text
error_message_
    }
      = String
"StatisticalGraphError"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"error_message" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
error_message_
        ]

instance AT.FromJSON StatisticalGraph where
  parseJSON :: Value -> Parser StatisticalGraph
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
"statisticalGraphData"  -> Value -> Parser StatisticalGraph
parseStatisticalGraphData Value
v
      String
"statisticalGraphAsync" -> Value -> Parser StatisticalGraph
parseStatisticalGraphAsync Value
v
      String
"statisticalGraphError" -> Value -> Parser StatisticalGraph
parseStatisticalGraphError Value
v
      String
_                       -> Parser StatisticalGraph
forall a. Monoid a => a
mempty
    
    where
      parseStatisticalGraphData :: A.Value -> AT.Parser StatisticalGraph
      parseStatisticalGraphData :: Value -> Parser StatisticalGraph
parseStatisticalGraphData = String
-> (Object -> Parser StatisticalGraph)
-> Value
-> Parser StatisticalGraph
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StatisticalGraphData" ((Object -> Parser StatisticalGraph)
 -> Value -> Parser StatisticalGraph)
-> (Object -> Parser StatisticalGraph)
-> Value
-> Parser StatisticalGraph
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
json_data_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"json_data"
        Maybe Text
zoom_token_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"zoom_token"
        StatisticalGraph -> Parser StatisticalGraph
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StatisticalGraph -> Parser StatisticalGraph)
-> StatisticalGraph -> Parser StatisticalGraph
forall a b. (a -> b) -> a -> b
$ StatisticalGraphData
          { json_data :: Maybe Text
json_data  = Maybe Text
json_data_
          , zoom_token :: Maybe Text
zoom_token = Maybe Text
zoom_token_
          }
      parseStatisticalGraphAsync :: A.Value -> AT.Parser StatisticalGraph
      parseStatisticalGraphAsync :: Value -> Parser StatisticalGraph
parseStatisticalGraphAsync = String
-> (Object -> Parser StatisticalGraph)
-> Value
-> Parser StatisticalGraph
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StatisticalGraphAsync" ((Object -> Parser StatisticalGraph)
 -> Value -> Parser StatisticalGraph)
-> (Object -> Parser StatisticalGraph)
-> Value
-> Parser StatisticalGraph
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
token_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"token"
        StatisticalGraph -> Parser StatisticalGraph
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StatisticalGraph -> Parser StatisticalGraph)
-> StatisticalGraph -> Parser StatisticalGraph
forall a b. (a -> b) -> a -> b
$ StatisticalGraphAsync
          { token :: Maybe Text
token = Maybe Text
token_
          }
      parseStatisticalGraphError :: A.Value -> AT.Parser StatisticalGraph
      parseStatisticalGraphError :: Value -> Parser StatisticalGraph
parseStatisticalGraphError = String
-> (Object -> Parser StatisticalGraph)
-> Value
-> Parser StatisticalGraph
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StatisticalGraphError" ((Object -> Parser StatisticalGraph)
 -> Value -> Parser StatisticalGraph)
-> (Object -> Parser StatisticalGraph)
-> Value
-> Parser StatisticalGraph
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
error_message_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"error_message"
        StatisticalGraph -> Parser StatisticalGraph
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StatisticalGraph -> Parser StatisticalGraph)
-> StatisticalGraph -> Parser StatisticalGraph
forall a b. (a -> b) -> a -> b
$ StatisticalGraphError
          { error_message :: Maybe Text
error_message = Maybe Text
error_message_
          }
  parseJSON Value
_ = Parser StatisticalGraph
forall a. Monoid a => a
mempty