module TD.Data.PremiumSource
  (PremiumSource(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.PremiumLimitType as PremiumLimitType
import qualified TD.Data.PremiumFeature as PremiumFeature
import qualified TD.Data.BusinessFeature as BusinessFeature
import qualified TD.Data.PremiumStoryFeature as PremiumStoryFeature
import qualified Data.Text as T

-- | Describes a source from which the Premium features screen is opened
data PremiumSource
  = PremiumSourceLimitExceeded -- ^ A limit was exceeded
    { PremiumSource -> Maybe PremiumLimitType
limit_type :: Maybe PremiumLimitType.PremiumLimitType -- ^ Type of the exceeded limit
    }
  | PremiumSourceFeature -- ^ A user tried to use a Premium feature
    { PremiumSource -> Maybe PremiumFeature
feature :: Maybe PremiumFeature.PremiumFeature -- ^ The used feature
    }
  | PremiumSourceBusinessFeature -- ^ A user tried to use a Business feature
    { PremiumSource -> Maybe BusinessFeature
_feature :: Maybe BusinessFeature.BusinessFeature -- ^ The used feature; pass null if none specific feature was used
    }
  | PremiumSourceStoryFeature -- ^ A user tried to use a Premium story feature
    { PremiumSource -> Maybe PremiumStoryFeature
__feature :: Maybe PremiumStoryFeature.PremiumStoryFeature -- ^ The used feature
    }
  | PremiumSourceLink -- ^ A user opened an internal link of the type internalLinkTypePremiumFeatures
    { PremiumSource -> Maybe Text
referrer :: Maybe T.Text -- ^ The referrer from the link
    }
  | PremiumSourceSettings -- ^ A user opened the Premium features screen from settings
  deriving (PremiumSource -> PremiumSource -> Bool
(PremiumSource -> PremiumSource -> Bool)
-> (PremiumSource -> PremiumSource -> Bool) -> Eq PremiumSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PremiumSource -> PremiumSource -> Bool
== :: PremiumSource -> PremiumSource -> Bool
$c/= :: PremiumSource -> PremiumSource -> Bool
/= :: PremiumSource -> PremiumSource -> Bool
Eq, Int -> PremiumSource -> ShowS
[PremiumSource] -> ShowS
PremiumSource -> String
(Int -> PremiumSource -> ShowS)
-> (PremiumSource -> String)
-> ([PremiumSource] -> ShowS)
-> Show PremiumSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PremiumSource -> ShowS
showsPrec :: Int -> PremiumSource -> ShowS
$cshow :: PremiumSource -> String
show :: PremiumSource -> String
$cshowList :: [PremiumSource] -> ShowS
showList :: [PremiumSource] -> ShowS
Show)

instance I.ShortShow PremiumSource where
  shortShow :: PremiumSource -> String
shortShow PremiumSourceLimitExceeded
    { limit_type :: PremiumSource -> Maybe PremiumLimitType
limit_type = Maybe PremiumLimitType
limit_type_
    }
      = String
"PremiumSourceLimitExceeded"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"limit_type" String -> Maybe PremiumLimitType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PremiumLimitType
limit_type_
        ]
  shortShow PremiumSourceFeature
    { feature :: PremiumSource -> Maybe PremiumFeature
feature = Maybe PremiumFeature
feature_
    }
      = String
"PremiumSourceFeature"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"feature" String -> Maybe PremiumFeature -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PremiumFeature
feature_
        ]
  shortShow PremiumSourceBusinessFeature
    { _feature :: PremiumSource -> Maybe BusinessFeature
_feature = Maybe BusinessFeature
_feature_
    }
      = String
"PremiumSourceBusinessFeature"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_feature" String -> Maybe BusinessFeature -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe BusinessFeature
_feature_
        ]
  shortShow PremiumSourceStoryFeature
    { __feature :: PremiumSource -> Maybe PremiumStoryFeature
__feature = Maybe PremiumStoryFeature
__feature_
    }
      = String
"PremiumSourceStoryFeature"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"__feature" String -> Maybe PremiumStoryFeature -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PremiumStoryFeature
__feature_
        ]
  shortShow PremiumSourceLink
    { referrer :: PremiumSource -> Maybe Text
referrer = Maybe Text
referrer_
    }
      = String
"PremiumSourceLink"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"referrer" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
referrer_
        ]
  shortShow PremiumSource
PremiumSourceSettings
      = String
"PremiumSourceSettings"

instance AT.FromJSON PremiumSource where
  parseJSON :: Value -> Parser PremiumSource
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
"premiumSourceLimitExceeded"   -> Value -> Parser PremiumSource
parsePremiumSourceLimitExceeded Value
v
      String
"premiumSourceFeature"         -> Value -> Parser PremiumSource
parsePremiumSourceFeature Value
v
      String
"premiumSourceBusinessFeature" -> Value -> Parser PremiumSource
parsePremiumSourceBusinessFeature Value
v
      String
"premiumSourceStoryFeature"    -> Value -> Parser PremiumSource
parsePremiumSourceStoryFeature Value
v
      String
"premiumSourceLink"            -> Value -> Parser PremiumSource
parsePremiumSourceLink Value
v
      String
"premiumSourceSettings"        -> PremiumSource -> Parser PremiumSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PremiumSource
PremiumSourceSettings
      String
_                              -> Parser PremiumSource
forall a. Monoid a => a
mempty
    
    where
      parsePremiumSourceLimitExceeded :: A.Value -> AT.Parser PremiumSource
      parsePremiumSourceLimitExceeded :: Value -> Parser PremiumSource
parsePremiumSourceLimitExceeded = String
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PremiumSourceLimitExceeded" ((Object -> Parser PremiumSource) -> Value -> Parser PremiumSource)
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe PremiumLimitType
limit_type_ <- Object
o Object -> Key -> Parser (Maybe PremiumLimitType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"limit_type"
        PremiumSource -> Parser PremiumSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PremiumSource -> Parser PremiumSource)
-> PremiumSource -> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ PremiumSourceLimitExceeded
          { limit_type :: Maybe PremiumLimitType
limit_type = Maybe PremiumLimitType
limit_type_
          }
      parsePremiumSourceFeature :: A.Value -> AT.Parser PremiumSource
      parsePremiumSourceFeature :: Value -> Parser PremiumSource
parsePremiumSourceFeature = String
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PremiumSourceFeature" ((Object -> Parser PremiumSource) -> Value -> Parser PremiumSource)
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe PremiumFeature
feature_ <- Object
o Object -> Key -> Parser (Maybe PremiumFeature)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"feature"
        PremiumSource -> Parser PremiumSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PremiumSource -> Parser PremiumSource)
-> PremiumSource -> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ PremiumSourceFeature
          { feature :: Maybe PremiumFeature
feature = Maybe PremiumFeature
feature_
          }
      parsePremiumSourceBusinessFeature :: A.Value -> AT.Parser PremiumSource
      parsePremiumSourceBusinessFeature :: Value -> Parser PremiumSource
parsePremiumSourceBusinessFeature = String
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PremiumSourceBusinessFeature" ((Object -> Parser PremiumSource) -> Value -> Parser PremiumSource)
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe BusinessFeature
_feature_ <- Object
o Object -> Key -> Parser (Maybe BusinessFeature)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"feature"
        PremiumSource -> Parser PremiumSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PremiumSource -> Parser PremiumSource)
-> PremiumSource -> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ PremiumSourceBusinessFeature
          { _feature :: Maybe BusinessFeature
_feature = Maybe BusinessFeature
_feature_
          }
      parsePremiumSourceStoryFeature :: A.Value -> AT.Parser PremiumSource
      parsePremiumSourceStoryFeature :: Value -> Parser PremiumSource
parsePremiumSourceStoryFeature = String
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PremiumSourceStoryFeature" ((Object -> Parser PremiumSource) -> Value -> Parser PremiumSource)
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe PremiumStoryFeature
__feature_ <- Object
o Object -> Key -> Parser (Maybe PremiumStoryFeature)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"feature"
        PremiumSource -> Parser PremiumSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PremiumSource -> Parser PremiumSource)
-> PremiumSource -> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ PremiumSourceStoryFeature
          { __feature :: Maybe PremiumStoryFeature
__feature = Maybe PremiumStoryFeature
__feature_
          }
      parsePremiumSourceLink :: A.Value -> AT.Parser PremiumSource
      parsePremiumSourceLink :: Value -> Parser PremiumSource
parsePremiumSourceLink = String
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PremiumSourceLink" ((Object -> Parser PremiumSource) -> Value -> Parser PremiumSource)
-> (Object -> Parser PremiumSource)
-> Value
-> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
referrer_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"referrer"
        PremiumSource -> Parser PremiumSource
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PremiumSource -> Parser PremiumSource)
-> PremiumSource -> Parser PremiumSource
forall a b. (a -> b) -> a -> b
$ PremiumSourceLink
          { referrer :: Maybe Text
referrer = Maybe Text
referrer_
          }
  parseJSON Value
_ = Parser PremiumSource
forall a. Monoid a => a
mempty

instance AT.ToJSON PremiumSource where
  toJSON :: PremiumSource -> Value
toJSON PremiumSourceLimitExceeded
    { limit_type :: PremiumSource -> Maybe PremiumLimitType
limit_type = Maybe PremiumLimitType
limit_type_
    }
      = [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
"premiumSourceLimitExceeded"
        , Key
"limit_type" Key -> Maybe PremiumLimitType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe PremiumLimitType
limit_type_
        ]
  toJSON PremiumSourceFeature
    { feature :: PremiumSource -> Maybe PremiumFeature
feature = Maybe PremiumFeature
feature_
    }
      = [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
"premiumSourceFeature"
        , Key
"feature" Key -> Maybe PremiumFeature -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe PremiumFeature
feature_
        ]
  toJSON PremiumSourceBusinessFeature
    { _feature :: PremiumSource -> Maybe BusinessFeature
_feature = Maybe BusinessFeature
_feature_
    }
      = [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
"premiumSourceBusinessFeature"
        , Key
"feature" Key -> Maybe BusinessFeature -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe BusinessFeature
_feature_
        ]
  toJSON PremiumSourceStoryFeature
    { __feature :: PremiumSource -> Maybe PremiumStoryFeature
__feature = Maybe PremiumStoryFeature
__feature_
    }
      = [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
"premiumSourceStoryFeature"
        , Key
"feature" Key -> Maybe PremiumStoryFeature -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe PremiumStoryFeature
__feature_
        ]
  toJSON PremiumSourceLink
    { referrer :: PremiumSource -> Maybe Text
referrer = Maybe Text
referrer_
    }
      = [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
"premiumSourceLink"
        , Key
"referrer" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
referrer_
        ]
  toJSON PremiumSource
PremiumSourceSettings
      = [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
"premiumSourceSettings"
        ]