-- |
--
-- 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.Types.TV
  ( TV (..),
    tvPosterURLs,
  )
where

import Data.Aeson
import Data.Time (Day (..))
import Network.API.TheMovieDB.Internal.Configuration
import Network.API.TheMovieDB.Internal.Date
import Network.API.TheMovieDB.Internal.Types
import Network.API.TheMovieDB.Types.Genre
import Network.API.TheMovieDB.Types.Season

-- | Metadata for a TV series.
--
--   * The 'tvPosterPath' field is an incomplete URL.  To construct a
--     complete URL you'll need to use the 'Configuration' type and the
--    'tvPosterURLs' helper function.
data TV = TV
  { -- | TheMovieDB unique ID.
    TV -> ItemID
tvID :: ItemID,
    -- | The name of the TV series.
    TV -> Text
tvName :: Text,
    -- | Short description of the TV series.
    TV -> Text
tvOverview :: Text,
    -- | List of 'Genre's.
    TV -> [Genre]
tvGenres :: [Genre],
    -- | Popularity ranking.
    TV -> Double
tvPopularity :: Double,
    -- | Incomplete URL for poster image.  See 'tvPosterURLs'.
    TV -> Text
tvPosterPath :: Text,
    -- | Air date for first episode.
    TV -> Maybe Day
tvFirstAirDate :: Maybe Day,
    -- | Air date for last episode.
    TV -> Maybe Day
tvLastAirDate :: Maybe Day,
    -- | Number of seasons for the TV series.
    TV -> ItemID
tvNumberOfSeasons :: Int,
    -- | Total number of episodes for all seasons.
    TV -> ItemID
tvNumberOfEpisodes :: Int,
    -- | Information about each season.
    --
    -- The number of elements in this list may not match
    -- 'tvNumberOfSeasons'.  Information about special episodes and
    -- unreleased episodes are usually kept in a 'Season' listed as
    -- season 0.  Therefore, the first element in this list might not
    -- be season 1.
    TV -> [Season]
tvSeasons :: [Season]
  }
  deriving (TV -> TV -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TV -> TV -> Bool
$c/= :: TV -> TV -> Bool
== :: TV -> TV -> Bool
$c== :: TV -> TV -> Bool
Eq, ItemID -> TV -> ShowS
[TV] -> ShowS
TV -> String
forall a.
(ItemID -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TV] -> ShowS
$cshowList :: [TV] -> ShowS
show :: TV -> String
$cshow :: TV -> String
showsPrec :: ItemID -> TV -> ShowS
$cshowsPrec :: ItemID -> TV -> ShowS
Show)

instance Ord TV where
  compare :: TV -> TV -> Ordering
compare TV
a TV
b = TV -> ItemID
tvID TV
a forall a. Ord a => a -> a -> Ordering
`compare` TV -> ItemID
tvID TV
b

instance FromJSON TV where
  parseJSON :: Value -> Parser TV
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"TV" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    ItemID
-> Text
-> Text
-> [Genre]
-> Double
-> Text
-> Maybe Day
-> Maybe Day
-> ItemID
-> ItemID
-> [Season]
-> TV
TV
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"overview" forall a. Parser (Maybe a) -> a -> Parser a
.!= Text
""
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"genres" forall a. Parser (Maybe a) -> a -> Parser a
.!= []
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"popularity" forall a. Parser (Maybe a) -> a -> Parser a
.!= Double
0.0
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"poster_path" forall a. Parser (Maybe a) -> a -> Parser a
.!= Text
""
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Day)
.:: Key
"first_air_date"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Day)
.:: Key
"last_air_date"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"number_of_seasons" forall a. Parser (Maybe a) -> a -> Parser a
.!= ItemID
0
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"number_of_episodes" forall a. Parser (Maybe a) -> a -> Parser a
.!= ItemID
0
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"seasons" forall a. Parser (Maybe a) -> a -> Parser a
.!= []

-- | Return a list of URLs for all possible TV posters.
tvPosterURLs :: Configuration -> TV -> [Text]
tvPosterURLs :: Configuration -> TV -> [Text]
tvPosterURLs Configuration
c TV
m = Configuration -> Text -> [Text]
posterURLs Configuration
c (TV -> Text
tvPosterPath TV
m)