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

-- | Reason of vote restriction in the poll for the current user
data PollVoteRestrictionReason
  = PollVoteRestrictionReasonClosed -- ^ The poll is closed
  | PollVoteRestrictionReasonYetUnsent -- ^ The poll isn't sent yet
  | PollVoteRestrictionReasonScheduled -- ^ The poll is from a scheduled message
  | PollVoteRestrictionReasonCountryRestricted -- ^ The user is from a country, users from which aren't allowed to vote
    { PollVoteRestrictionReason -> Maybe Text
country_code :: Maybe T.Text -- ^ Two-letter ISO 3166-1 alpha-2 code of the current user's country
    }
  | PollVoteRestrictionReasonMembershipRequired -- ^ The user must be a member of the chat for at least a day to vote
    { PollVoteRestrictionReason -> Maybe Int
chat_id :: Maybe Int -- ^ Identifier of the chat which must be joined for at least a day before the user can vote
    }
  | PollVoteRestrictionReasonOther -- ^ The poll can't be voted by the user due to some other reason
  deriving (PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool
(PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool)
-> (PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool)
-> Eq PollVoteRestrictionReason
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool
== :: PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool
$c/= :: PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool
/= :: PollVoteRestrictionReason -> PollVoteRestrictionReason -> Bool
Eq, Int -> PollVoteRestrictionReason -> ShowS
[PollVoteRestrictionReason] -> ShowS
PollVoteRestrictionReason -> String
(Int -> PollVoteRestrictionReason -> ShowS)
-> (PollVoteRestrictionReason -> String)
-> ([PollVoteRestrictionReason] -> ShowS)
-> Show PollVoteRestrictionReason
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PollVoteRestrictionReason -> ShowS
showsPrec :: Int -> PollVoteRestrictionReason -> ShowS
$cshow :: PollVoteRestrictionReason -> String
show :: PollVoteRestrictionReason -> String
$cshowList :: [PollVoteRestrictionReason] -> ShowS
showList :: [PollVoteRestrictionReason] -> ShowS
Show)

instance I.ShortShow PollVoteRestrictionReason where
  shortShow :: PollVoteRestrictionReason -> String
shortShow PollVoteRestrictionReason
PollVoteRestrictionReasonClosed
      = String
"PollVoteRestrictionReasonClosed"
  shortShow PollVoteRestrictionReason
PollVoteRestrictionReasonYetUnsent
      = String
"PollVoteRestrictionReasonYetUnsent"
  shortShow PollVoteRestrictionReason
PollVoteRestrictionReasonScheduled
      = String
"PollVoteRestrictionReasonScheduled"
  shortShow PollVoteRestrictionReasonCountryRestricted
    { country_code :: PollVoteRestrictionReason -> Maybe Text
country_code = Maybe Text
country_code_
    }
      = String
"PollVoteRestrictionReasonCountryRestricted"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"country_code" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
country_code_
        ]
  shortShow PollVoteRestrictionReasonMembershipRequired
    { chat_id :: PollVoteRestrictionReason -> Maybe Int
chat_id = Maybe Int
chat_id_
    }
      = String
"PollVoteRestrictionReasonMembershipRequired"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        ]
  shortShow PollVoteRestrictionReason
PollVoteRestrictionReasonOther
      = String
"PollVoteRestrictionReasonOther"

instance AT.FromJSON PollVoteRestrictionReason where
  parseJSON :: Value -> Parser PollVoteRestrictionReason
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
"pollVoteRestrictionReasonClosed"             -> PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PollVoteRestrictionReason
PollVoteRestrictionReasonClosed
      String
"pollVoteRestrictionReasonYetUnsent"          -> PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PollVoteRestrictionReason
PollVoteRestrictionReasonYetUnsent
      String
"pollVoteRestrictionReasonScheduled"          -> PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PollVoteRestrictionReason
PollVoteRestrictionReasonScheduled
      String
"pollVoteRestrictionReasonCountryRestricted"  -> Value -> Parser PollVoteRestrictionReason
parsePollVoteRestrictionReasonCountryRestricted Value
v
      String
"pollVoteRestrictionReasonMembershipRequired" -> Value -> Parser PollVoteRestrictionReason
parsePollVoteRestrictionReasonMembershipRequired Value
v
      String
"pollVoteRestrictionReasonOther"              -> PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PollVoteRestrictionReason
PollVoteRestrictionReasonOther
      String
_                                             -> Parser PollVoteRestrictionReason
forall a. Monoid a => a
mempty
    
    where
      parsePollVoteRestrictionReasonCountryRestricted :: A.Value -> AT.Parser PollVoteRestrictionReason
      parsePollVoteRestrictionReasonCountryRestricted :: Value -> Parser PollVoteRestrictionReason
parsePollVoteRestrictionReasonCountryRestricted = String
-> (Object -> Parser PollVoteRestrictionReason)
-> Value
-> Parser PollVoteRestrictionReason
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollVoteRestrictionReasonCountryRestricted" ((Object -> Parser PollVoteRestrictionReason)
 -> Value -> Parser PollVoteRestrictionReason)
-> (Object -> Parser PollVoteRestrictionReason)
-> Value
-> Parser PollVoteRestrictionReason
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
country_code_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"country_code"
        PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PollVoteRestrictionReason -> Parser PollVoteRestrictionReason)
-> PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a b. (a -> b) -> a -> b
$ PollVoteRestrictionReasonCountryRestricted
          { country_code :: Maybe Text
country_code = Maybe Text
country_code_
          }
      parsePollVoteRestrictionReasonMembershipRequired :: A.Value -> AT.Parser PollVoteRestrictionReason
      parsePollVoteRestrictionReasonMembershipRequired :: Value -> Parser PollVoteRestrictionReason
parsePollVoteRestrictionReasonMembershipRequired = String
-> (Object -> Parser PollVoteRestrictionReason)
-> Value
-> Parser PollVoteRestrictionReason
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollVoteRestrictionReasonMembershipRequired" ((Object -> Parser PollVoteRestrictionReason)
 -> Value -> Parser PollVoteRestrictionReason)
-> (Object -> Parser PollVoteRestrictionReason)
-> Value
-> Parser PollVoteRestrictionReason
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PollVoteRestrictionReason -> Parser PollVoteRestrictionReason)
-> PollVoteRestrictionReason -> Parser PollVoteRestrictionReason
forall a b. (a -> b) -> a -> b
$ PollVoteRestrictionReasonMembershipRequired
          { chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
          }
  parseJSON Value
_ = Parser PollVoteRestrictionReason
forall a. Monoid a => a
mempty