module TD.Data.StoryPrivacySettings
  (StoryPrivacySettings(..)) where

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

-- | Describes privacy settings of a story
data StoryPrivacySettings
  = StoryPrivacySettingsEveryone -- ^ The story can be viewed by everyone
    { StoryPrivacySettings -> Maybe [Int]
except_user_ids :: Maybe [Int] -- ^ Identifiers of the users that can't see the story; always unknown and empty for non-owned stories
    }
  | StoryPrivacySettingsContacts -- ^ The story can be viewed by all contacts except chosen users
    { except_user_ids :: Maybe [Int] -- ^ User identifiers of the contacts that can't see the story; always unknown and empty for non-owned stories
    }
  | StoryPrivacySettingsCloseFriends -- ^ The story can be viewed by all close friends
  | StoryPrivacySettingsSelectedUsers -- ^ The story can be viewed by certain specified users
    { StoryPrivacySettings -> Maybe [Int]
user_ids :: Maybe [Int] -- ^ Identifiers of the users; always unknown and empty for non-owned stories
    }
  deriving (StoryPrivacySettings -> StoryPrivacySettings -> Bool
(StoryPrivacySettings -> StoryPrivacySettings -> Bool)
-> (StoryPrivacySettings -> StoryPrivacySettings -> Bool)
-> Eq StoryPrivacySettings
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoryPrivacySettings -> StoryPrivacySettings -> Bool
== :: StoryPrivacySettings -> StoryPrivacySettings -> Bool
$c/= :: StoryPrivacySettings -> StoryPrivacySettings -> Bool
/= :: StoryPrivacySettings -> StoryPrivacySettings -> Bool
Eq, Int -> StoryPrivacySettings -> ShowS
[StoryPrivacySettings] -> ShowS
StoryPrivacySettings -> String
(Int -> StoryPrivacySettings -> ShowS)
-> (StoryPrivacySettings -> String)
-> ([StoryPrivacySettings] -> ShowS)
-> Show StoryPrivacySettings
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoryPrivacySettings -> ShowS
showsPrec :: Int -> StoryPrivacySettings -> ShowS
$cshow :: StoryPrivacySettings -> String
show :: StoryPrivacySettings -> String
$cshowList :: [StoryPrivacySettings] -> ShowS
showList :: [StoryPrivacySettings] -> ShowS
Show)

instance I.ShortShow StoryPrivacySettings where
  shortShow :: StoryPrivacySettings -> String
shortShow StoryPrivacySettingsEveryone
    { except_user_ids :: StoryPrivacySettings -> Maybe [Int]
except_user_ids = Maybe [Int]
except_user_ids_
    }
      = String
"StoryPrivacySettingsEveryone"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"except_user_ids" String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
except_user_ids_
        ]
  shortShow StoryPrivacySettingsContacts
    { except_user_ids :: StoryPrivacySettings -> Maybe [Int]
except_user_ids = Maybe [Int]
except_user_ids_
    }
      = String
"StoryPrivacySettingsContacts"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"except_user_ids" String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
except_user_ids_
        ]
  shortShow StoryPrivacySettings
StoryPrivacySettingsCloseFriends
      = String
"StoryPrivacySettingsCloseFriends"
  shortShow StoryPrivacySettingsSelectedUsers
    { user_ids :: StoryPrivacySettings -> Maybe [Int]
user_ids = Maybe [Int]
user_ids_
    }
      = String
"StoryPrivacySettingsSelectedUsers"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_ids" String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
user_ids_
        ]

instance AT.FromJSON StoryPrivacySettings where
  parseJSON :: Value -> Parser StoryPrivacySettings
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
"storyPrivacySettingsEveryone"      -> Value -> Parser StoryPrivacySettings
parseStoryPrivacySettingsEveryone Value
v
      String
"storyPrivacySettingsContacts"      -> Value -> Parser StoryPrivacySettings
parseStoryPrivacySettingsContacts Value
v
      String
"storyPrivacySettingsCloseFriends"  -> StoryPrivacySettings -> Parser StoryPrivacySettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure StoryPrivacySettings
StoryPrivacySettingsCloseFriends
      String
"storyPrivacySettingsSelectedUsers" -> Value -> Parser StoryPrivacySettings
parseStoryPrivacySettingsSelectedUsers Value
v
      String
_                                   -> Parser StoryPrivacySettings
forall a. Monoid a => a
mempty
    
    where
      parseStoryPrivacySettingsEveryone :: A.Value -> AT.Parser StoryPrivacySettings
      parseStoryPrivacySettingsEveryone :: Value -> Parser StoryPrivacySettings
parseStoryPrivacySettingsEveryone = String
-> (Object -> Parser StoryPrivacySettings)
-> Value
-> Parser StoryPrivacySettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryPrivacySettingsEveryone" ((Object -> Parser StoryPrivacySettings)
 -> Value -> Parser StoryPrivacySettings)
-> (Object -> Parser StoryPrivacySettings)
-> Value
-> Parser StoryPrivacySettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Int]
except_user_ids_ <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"except_user_ids"
        StoryPrivacySettings -> Parser StoryPrivacySettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryPrivacySettings -> Parser StoryPrivacySettings)
-> StoryPrivacySettings -> Parser StoryPrivacySettings
forall a b. (a -> b) -> a -> b
$ StoryPrivacySettingsEveryone
          { except_user_ids :: Maybe [Int]
except_user_ids = Maybe [Int]
except_user_ids_
          }
      parseStoryPrivacySettingsContacts :: A.Value -> AT.Parser StoryPrivacySettings
      parseStoryPrivacySettingsContacts :: Value -> Parser StoryPrivacySettings
parseStoryPrivacySettingsContacts = String
-> (Object -> Parser StoryPrivacySettings)
-> Value
-> Parser StoryPrivacySettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryPrivacySettingsContacts" ((Object -> Parser StoryPrivacySettings)
 -> Value -> Parser StoryPrivacySettings)
-> (Object -> Parser StoryPrivacySettings)
-> Value
-> Parser StoryPrivacySettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Int]
except_user_ids_ <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"except_user_ids"
        StoryPrivacySettings -> Parser StoryPrivacySettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryPrivacySettings -> Parser StoryPrivacySettings)
-> StoryPrivacySettings -> Parser StoryPrivacySettings
forall a b. (a -> b) -> a -> b
$ StoryPrivacySettingsContacts
          { except_user_ids :: Maybe [Int]
except_user_ids = Maybe [Int]
except_user_ids_
          }
      parseStoryPrivacySettingsSelectedUsers :: A.Value -> AT.Parser StoryPrivacySettings
      parseStoryPrivacySettingsSelectedUsers :: Value -> Parser StoryPrivacySettings
parseStoryPrivacySettingsSelectedUsers = String
-> (Object -> Parser StoryPrivacySettings)
-> Value
-> Parser StoryPrivacySettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryPrivacySettingsSelectedUsers" ((Object -> Parser StoryPrivacySettings)
 -> Value -> Parser StoryPrivacySettings)
-> (Object -> Parser StoryPrivacySettings)
-> Value
-> Parser StoryPrivacySettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Int]
user_ids_ <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_ids"
        StoryPrivacySettings -> Parser StoryPrivacySettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryPrivacySettings -> Parser StoryPrivacySettings)
-> StoryPrivacySettings -> Parser StoryPrivacySettings
forall a b. (a -> b) -> a -> b
$ StoryPrivacySettingsSelectedUsers
          { user_ids :: Maybe [Int]
user_ids = Maybe [Int]
user_ids_
          }
  parseJSON Value
_ = Parser StoryPrivacySettings
forall a. Monoid a => a
mempty

instance AT.ToJSON StoryPrivacySettings where
  toJSON :: StoryPrivacySettings -> Value
toJSON StoryPrivacySettingsEveryone
    { except_user_ids :: StoryPrivacySettings -> Maybe [Int]
except_user_ids = Maybe [Int]
except_user_ids_
    }
      = [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
"storyPrivacySettingsEveryone"
        , Key
"except_user_ids" Key -> Maybe [Int] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Int]
except_user_ids_
        ]
  toJSON StoryPrivacySettingsContacts
    { except_user_ids :: StoryPrivacySettings -> Maybe [Int]
except_user_ids = Maybe [Int]
except_user_ids_
    }
      = [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
"storyPrivacySettingsContacts"
        , Key
"except_user_ids" Key -> Maybe [Int] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Int]
except_user_ids_
        ]
  toJSON StoryPrivacySettings
StoryPrivacySettingsCloseFriends
      = [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
"storyPrivacySettingsCloseFriends"
        ]
  toJSON StoryPrivacySettingsSelectedUsers
    { user_ids :: StoryPrivacySettings -> Maybe [Int]
user_ids = Maybe [Int]
user_ids_
    }
      = [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
"storyPrivacySettingsSelectedUsers"
        , Key
"user_ids" Key -> Maybe [Int] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Int]
user_ids_
        ]