module TD.Data.Outline
  (Outline(..)) 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.ClosedVectorPath as ClosedVectorPath

data Outline
  = Outline -- ^ Represents outline of an image
    { Outline -> Maybe [ClosedVectorPath]
paths :: Maybe [ClosedVectorPath.ClosedVectorPath] -- ^ The list of closed vector paths
    }
  deriving (Outline -> Outline -> Bool
(Outline -> Outline -> Bool)
-> (Outline -> Outline -> Bool) -> Eq Outline
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Outline -> Outline -> Bool
== :: Outline -> Outline -> Bool
$c/= :: Outline -> Outline -> Bool
/= :: Outline -> Outline -> Bool
Eq, Int -> Outline -> ShowS
[Outline] -> ShowS
Outline -> String
(Int -> Outline -> ShowS)
-> (Outline -> String) -> ([Outline] -> ShowS) -> Show Outline
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Outline -> ShowS
showsPrec :: Int -> Outline -> ShowS
$cshow :: Outline -> String
show :: Outline -> String
$cshowList :: [Outline] -> ShowS
showList :: [Outline] -> ShowS
Show)

instance I.ShortShow Outline where
  shortShow :: Outline -> String
shortShow Outline
    { paths :: Outline -> Maybe [ClosedVectorPath]
paths = Maybe [ClosedVectorPath]
paths_
    }
      = String
"Outline"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"paths" String -> Maybe [ClosedVectorPath] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [ClosedVectorPath]
paths_
        ]

instance AT.FromJSON Outline where
  parseJSON :: Value -> Parser Outline
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
"outline" -> Value -> Parser Outline
parseOutline Value
v
      String
_         -> Parser Outline
forall a. Monoid a => a
mempty
    
    where
      parseOutline :: A.Value -> AT.Parser Outline
      parseOutline :: Value -> Parser Outline
parseOutline = String -> (Object -> Parser Outline) -> Value -> Parser Outline
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Outline" ((Object -> Parser Outline) -> Value -> Parser Outline)
-> (Object -> Parser Outline) -> Value -> Parser Outline
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [ClosedVectorPath]
paths_ <- Object
o Object -> Key -> Parser (Maybe [ClosedVectorPath])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"paths"
        Outline -> Parser Outline
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Outline -> Parser Outline) -> Outline -> Parser Outline
forall a b. (a -> b) -> a -> b
$ Outline
          { paths :: Maybe [ClosedVectorPath]
paths = Maybe [ClosedVectorPath]
paths_
          }
  parseJSON Value
_ = Parser Outline
forall a. Monoid a => a
mempty