module TD.Data.StoryArea
  (StoryArea(..)) where

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

data StoryArea
  = StoryArea -- ^ Describes a clickable rectangle area on a story media
    { StoryArea -> Maybe StoryAreaPosition
position :: Maybe StoryAreaPosition.StoryAreaPosition -- ^ Position of the area
    , StoryArea -> Maybe StoryAreaType
_type    :: Maybe StoryAreaType.StoryAreaType         -- ^ Type of the area
    }
  deriving (StoryArea -> StoryArea -> Bool
(StoryArea -> StoryArea -> Bool)
-> (StoryArea -> StoryArea -> Bool) -> Eq StoryArea
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoryArea -> StoryArea -> Bool
== :: StoryArea -> StoryArea -> Bool
$c/= :: StoryArea -> StoryArea -> Bool
/= :: StoryArea -> StoryArea -> Bool
Eq, Int -> StoryArea -> ShowS
[StoryArea] -> ShowS
StoryArea -> String
(Int -> StoryArea -> ShowS)
-> (StoryArea -> String)
-> ([StoryArea] -> ShowS)
-> Show StoryArea
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoryArea -> ShowS
showsPrec :: Int -> StoryArea -> ShowS
$cshow :: StoryArea -> String
show :: StoryArea -> String
$cshowList :: [StoryArea] -> ShowS
showList :: [StoryArea] -> ShowS
Show)

instance I.ShortShow StoryArea where
  shortShow :: StoryArea -> String
shortShow StoryArea
    { position :: StoryArea -> Maybe StoryAreaPosition
position = Maybe StoryAreaPosition
position_
    , _type :: StoryArea -> Maybe StoryAreaType
_type    = Maybe StoryAreaType
_type_
    }
      = String
"StoryArea"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"position" String -> Maybe StoryAreaPosition -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StoryAreaPosition
position_
        , String
"_type"    String -> Maybe StoryAreaType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StoryAreaType
_type_
        ]

instance AT.FromJSON StoryArea where
  parseJSON :: Value -> Parser StoryArea
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
"storyArea" -> Value -> Parser StoryArea
parseStoryArea Value
v
      String
_           -> Parser StoryArea
forall a. Monoid a => a
mempty
    
    where
      parseStoryArea :: A.Value -> AT.Parser StoryArea
      parseStoryArea :: Value -> Parser StoryArea
parseStoryArea = String -> (Object -> Parser StoryArea) -> Value -> Parser StoryArea
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryArea" ((Object -> Parser StoryArea) -> Value -> Parser StoryArea)
-> (Object -> Parser StoryArea) -> Value -> Parser StoryArea
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe StoryAreaPosition
position_ <- Object
o Object -> Key -> Parser (Maybe StoryAreaPosition)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"position"
        Maybe StoryAreaType
_type_    <- Object
o Object -> Key -> Parser (Maybe StoryAreaType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        StoryArea -> Parser StoryArea
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryArea -> Parser StoryArea) -> StoryArea -> Parser StoryArea
forall a b. (a -> b) -> a -> b
$ StoryArea
          { position :: Maybe StoryAreaPosition
position = Maybe StoryAreaPosition
position_
          , _type :: Maybe StoryAreaType
_type    = Maybe StoryAreaType
_type_
          }
  parseJSON Value
_ = Parser StoryArea
forall a. Monoid a => a
mempty