-- |
--
-- Copyright:
--   This file is part of the package themoviedb.  It is subject to
--   the license terms in the LICENSE file found in the top-level
--   directory of this distribution and at:
--
--     https://github.com/pjones/themoviedb
--
--   No part of this package, including this file, may be copied,
--   modified, propagated, or distributed except according to the terms
--   contained in the LICENSE file.
--
-- License: MIT
module Network.API.TheMovieDB.Internal.Settings
  ( Settings (..),
    defaultSettings,
  )
where

import qualified Data.Aeson as Aeson
import Network.API.TheMovieDB.Internal.Types

-- | Settings used by this library.
data Settings = Settings
  { -- | The API key to use.
    Settings -> Key
tmdbKey :: Key,
    -- | Optional ISO 639-1 language code to send with every request.
    Settings -> Maybe Key
tmdbLanguage :: Maybe LanguageCode
  }

instance Aeson.FromJSON Settings where
  parseJSON :: Value -> Parser Settings
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Settings" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Key -> Maybe Key -> Settings
Settings
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
Aeson..: Key
"key"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
Aeson..: Key
"lang"

instance Aeson.ToJSON Settings where
  toJSON :: Settings -> Value
toJSON Settings {Maybe Key
Key
tmdbLanguage :: Maybe Key
tmdbKey :: Key
tmdbLanguage :: Settings -> Maybe Key
tmdbKey :: Settings -> Key
..} =
    [Pair] -> Value
Aeson.object
      [ Key
"key" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Aeson..= Key
tmdbKey,
        Key
"lang" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Aeson..= Maybe Key
tmdbLanguage
      ]

-- | Default settings.
defaultSettings :: Key -> Settings
defaultSettings :: Key -> Settings
defaultSettings Key
key =
  Settings
    { tmdbKey :: Key
tmdbKey = Key
key,
      tmdbLanguage :: Maybe Key
tmdbLanguage = forall a. Maybe a
Nothing
    }