module TD.Data.Point
  (Point(..)) where

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

data Point
  = Point -- ^ A point on a Cartesian plane
    { Point -> Maybe Double
x :: Maybe Double -- ^ The point's first coordinate
    , Point -> Maybe Double
y :: Maybe Double -- ^ The point's second coordinate
    }
  deriving (Point -> Point -> Bool
(Point -> Point -> Bool) -> (Point -> Point -> Bool) -> Eq Point
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Point -> Point -> Bool
== :: Point -> Point -> Bool
$c/= :: Point -> Point -> Bool
/= :: Point -> Point -> Bool
Eq, Int -> Point -> ShowS
[Point] -> ShowS
Point -> String
(Int -> Point -> ShowS)
-> (Point -> String) -> ([Point] -> ShowS) -> Show Point
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Point -> ShowS
showsPrec :: Int -> Point -> ShowS
$cshow :: Point -> String
show :: Point -> String
$cshowList :: [Point] -> ShowS
showList :: [Point] -> ShowS
Show)

instance I.ShortShow Point where
  shortShow :: Point -> String
shortShow Point
    { x :: Point -> Maybe Double
x = Maybe Double
x_
    , y :: Point -> Maybe Double
y = Maybe Double
y_
    }
      = String
"Point"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"x" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
x_
        , String
"y" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
y_
        ]

instance AT.FromJSON Point where
  parseJSON :: Value -> Parser Point
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
"point" -> Value -> Parser Point
parsePoint Value
v
      String
_       -> Parser Point
forall a. Monoid a => a
mempty
    
    where
      parsePoint :: A.Value -> AT.Parser Point
      parsePoint :: Value -> Parser Point
parsePoint = String -> (Object -> Parser Point) -> Value -> Parser Point
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Point" ((Object -> Parser Point) -> Value -> Parser Point)
-> (Object -> Parser Point) -> Value -> Parser Point
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Double
x_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"x"
        Maybe Double
y_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"y"
        Point -> Parser Point
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Point -> Parser Point) -> Point -> Parser Point
forall a b. (a -> b) -> a -> b
$ Point
          { x :: Maybe Double
x = Maybe Double
x_
          , y :: Maybe Double
y = Maybe Double
y_
          }
  parseJSON Value
_ = Parser Point
forall a. Monoid a => a
mempty