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

data CountryInfo
  = CountryInfo -- ^ Contains information about a country
    { CountryInfo -> Maybe Text
country_code  :: Maybe T.Text   -- ^ A two-letter ISO 3166-1 alpha-2 country code
    , CountryInfo -> Maybe Text
name          :: Maybe T.Text   -- ^ Native name of the country
    , CountryInfo -> Maybe Text
english_name  :: Maybe T.Text   -- ^ English name of the country
    , CountryInfo -> Maybe Bool
is_hidden     :: Maybe Bool     -- ^ True, if the country must be hidden from the list of all countries
    , CountryInfo -> Maybe [Text]
calling_codes :: Maybe [T.Text] -- ^ List of country calling codes
    }
  deriving (CountryInfo -> CountryInfo -> Bool
(CountryInfo -> CountryInfo -> Bool)
-> (CountryInfo -> CountryInfo -> Bool) -> Eq CountryInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CountryInfo -> CountryInfo -> Bool
== :: CountryInfo -> CountryInfo -> Bool
$c/= :: CountryInfo -> CountryInfo -> Bool
/= :: CountryInfo -> CountryInfo -> Bool
Eq, Int -> CountryInfo -> ShowS
[CountryInfo] -> ShowS
CountryInfo -> String
(Int -> CountryInfo -> ShowS)
-> (CountryInfo -> String)
-> ([CountryInfo] -> ShowS)
-> Show CountryInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CountryInfo -> ShowS
showsPrec :: Int -> CountryInfo -> ShowS
$cshow :: CountryInfo -> String
show :: CountryInfo -> String
$cshowList :: [CountryInfo] -> ShowS
showList :: [CountryInfo] -> ShowS
Show)

instance I.ShortShow CountryInfo where
  shortShow :: CountryInfo -> String
shortShow CountryInfo
    { country_code :: CountryInfo -> Maybe Text
country_code  = Maybe Text
country_code_
    , name :: CountryInfo -> Maybe Text
name          = Maybe Text
name_
    , english_name :: CountryInfo -> Maybe Text
english_name  = Maybe Text
english_name_
    , is_hidden :: CountryInfo -> Maybe Bool
is_hidden     = Maybe Bool
is_hidden_
    , calling_codes :: CountryInfo -> Maybe [Text]
calling_codes = Maybe [Text]
calling_codes_
    }
      = String
"CountryInfo"
        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_
        , String
"name"          String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
name_
        , String
"english_name"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
english_name_
        , String
"is_hidden"     String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_hidden_
        , String
"calling_codes" String -> Maybe [Text] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Text]
calling_codes_
        ]

instance AT.FromJSON CountryInfo where
  parseJSON :: Value -> Parser CountryInfo
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
"countryInfo" -> Value -> Parser CountryInfo
parseCountryInfo Value
v
      String
_             -> Parser CountryInfo
forall a. Monoid a => a
mempty
    
    where
      parseCountryInfo :: A.Value -> AT.Parser CountryInfo
      parseCountryInfo :: Value -> Parser CountryInfo
parseCountryInfo = String
-> (Object -> Parser CountryInfo) -> Value -> Parser CountryInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CountryInfo" ((Object -> Parser CountryInfo) -> Value -> Parser CountryInfo)
-> (Object -> Parser CountryInfo) -> Value -> Parser CountryInfo
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"
        Maybe Text
name_          <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"name"
        Maybe Text
english_name_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"english_name"
        Maybe Bool
is_hidden_     <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_hidden"
        Maybe [Text]
calling_codes_ <- Object
o Object -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"calling_codes"
        CountryInfo -> Parser CountryInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CountryInfo -> Parser CountryInfo)
-> CountryInfo -> Parser CountryInfo
forall a b. (a -> b) -> a -> b
$ CountryInfo
          { country_code :: Maybe Text
country_code  = Maybe Text
country_code_
          , name :: Maybe Text
name          = Maybe Text
name_
          , english_name :: Maybe Text
english_name  = Maybe Text
english_name_
          , is_hidden :: Maybe Bool
is_hidden     = Maybe Bool
is_hidden_
          , calling_codes :: Maybe [Text]
calling_codes = Maybe [Text]
calling_codes_
          }
  parseJSON Value
_ = Parser CountryInfo
forall a. Monoid a => a
mempty