module TD.Data.BackgroundFill
  (BackgroundFill(..)) where

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

-- | Describes a fill of a background
data BackgroundFill
  = BackgroundFillSolid -- ^ Describes a solid fill of a background
    { BackgroundFill -> Maybe Int
color :: Maybe Int -- ^ A color of the background in the RGB format
    }
  | BackgroundFillGradient -- ^ Describes a gradient fill of a background
    { BackgroundFill -> Maybe Int
top_color      :: Maybe Int -- ^ A top color of the background in the RGB format
    , BackgroundFill -> Maybe Int
bottom_color   :: Maybe Int -- ^ A bottom color of the background in the RGB format
    , BackgroundFill -> Maybe Int
rotation_angle :: Maybe Int -- ^ Clockwise rotation angle of the gradient, in degrees; 0-359. Must always be divisible by 45
    }
  | BackgroundFillFreeformGradient -- ^ Describes a freeform gradient fill of a background
    { BackgroundFill -> Maybe [Int]
colors :: Maybe [Int] -- ^ A list of 3 or 4 colors of the freeform gradient in the RGB format
    }
  deriving (BackgroundFill -> BackgroundFill -> Bool
(BackgroundFill -> BackgroundFill -> Bool)
-> (BackgroundFill -> BackgroundFill -> Bool) -> Eq BackgroundFill
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BackgroundFill -> BackgroundFill -> Bool
== :: BackgroundFill -> BackgroundFill -> Bool
$c/= :: BackgroundFill -> BackgroundFill -> Bool
/= :: BackgroundFill -> BackgroundFill -> Bool
Eq, Int -> BackgroundFill -> ShowS
[BackgroundFill] -> ShowS
BackgroundFill -> String
(Int -> BackgroundFill -> ShowS)
-> (BackgroundFill -> String)
-> ([BackgroundFill] -> ShowS)
-> Show BackgroundFill
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BackgroundFill -> ShowS
showsPrec :: Int -> BackgroundFill -> ShowS
$cshow :: BackgroundFill -> String
show :: BackgroundFill -> String
$cshowList :: [BackgroundFill] -> ShowS
showList :: [BackgroundFill] -> ShowS
Show)

instance I.ShortShow BackgroundFill where
  shortShow :: BackgroundFill -> String
shortShow BackgroundFillSolid
    { color :: BackgroundFill -> Maybe Int
color = Maybe Int
color_
    }
      = String
"BackgroundFillSolid"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"color" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
color_
        ]
  shortShow BackgroundFillGradient
    { top_color :: BackgroundFill -> Maybe Int
top_color      = Maybe Int
top_color_
    , bottom_color :: BackgroundFill -> Maybe Int
bottom_color   = Maybe Int
bottom_color_
    , rotation_angle :: BackgroundFill -> Maybe Int
rotation_angle = Maybe Int
rotation_angle_
    }
      = String
"BackgroundFillGradient"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"top_color"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
top_color_
        , String
"bottom_color"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
bottom_color_
        , String
"rotation_angle" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
rotation_angle_
        ]
  shortShow BackgroundFillFreeformGradient
    { colors :: BackgroundFill -> Maybe [Int]
colors = Maybe [Int]
colors_
    }
      = String
"BackgroundFillFreeformGradient"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"colors" String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
colors_
        ]

instance AT.FromJSON BackgroundFill where
  parseJSON :: Value -> Parser BackgroundFill
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
"backgroundFillSolid"            -> Value -> Parser BackgroundFill
parseBackgroundFillSolid Value
v
      String
"backgroundFillGradient"         -> Value -> Parser BackgroundFill
parseBackgroundFillGradient Value
v
      String
"backgroundFillFreeformGradient" -> Value -> Parser BackgroundFill
parseBackgroundFillFreeformGradient Value
v
      String
_                                -> Parser BackgroundFill
forall a. Monoid a => a
mempty
    
    where
      parseBackgroundFillSolid :: A.Value -> AT.Parser BackgroundFill
      parseBackgroundFillSolid :: Value -> Parser BackgroundFill
parseBackgroundFillSolid = String
-> (Object -> Parser BackgroundFill)
-> Value
-> Parser BackgroundFill
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundFillSolid" ((Object -> Parser BackgroundFill)
 -> Value -> Parser BackgroundFill)
-> (Object -> Parser BackgroundFill)
-> Value
-> Parser BackgroundFill
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
color_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"color"
        BackgroundFill -> Parser BackgroundFill
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundFill -> Parser BackgroundFill)
-> BackgroundFill -> Parser BackgroundFill
forall a b. (a -> b) -> a -> b
$ BackgroundFillSolid
          { color :: Maybe Int
color = Maybe Int
color_
          }
      parseBackgroundFillGradient :: A.Value -> AT.Parser BackgroundFill
      parseBackgroundFillGradient :: Value -> Parser BackgroundFill
parseBackgroundFillGradient = String
-> (Object -> Parser BackgroundFill)
-> Value
-> Parser BackgroundFill
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundFillGradient" ((Object -> Parser BackgroundFill)
 -> Value -> Parser BackgroundFill)
-> (Object -> Parser BackgroundFill)
-> Value
-> Parser BackgroundFill
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
top_color_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"top_color"
        Maybe Int
bottom_color_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"bottom_color"
        Maybe Int
rotation_angle_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"rotation_angle"
        BackgroundFill -> Parser BackgroundFill
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundFill -> Parser BackgroundFill)
-> BackgroundFill -> Parser BackgroundFill
forall a b. (a -> b) -> a -> b
$ BackgroundFillGradient
          { top_color :: Maybe Int
top_color      = Maybe Int
top_color_
          , bottom_color :: Maybe Int
bottom_color   = Maybe Int
bottom_color_
          , rotation_angle :: Maybe Int
rotation_angle = Maybe Int
rotation_angle_
          }
      parseBackgroundFillFreeformGradient :: A.Value -> AT.Parser BackgroundFill
      parseBackgroundFillFreeformGradient :: Value -> Parser BackgroundFill
parseBackgroundFillFreeformGradient = String
-> (Object -> Parser BackgroundFill)
-> Value
-> Parser BackgroundFill
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundFillFreeformGradient" ((Object -> Parser BackgroundFill)
 -> Value -> Parser BackgroundFill)
-> (Object -> Parser BackgroundFill)
-> Value
-> Parser BackgroundFill
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Int]
colors_ <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"colors"
        BackgroundFill -> Parser BackgroundFill
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundFill -> Parser BackgroundFill)
-> BackgroundFill -> Parser BackgroundFill
forall a b. (a -> b) -> a -> b
$ BackgroundFillFreeformGradient
          { colors :: Maybe [Int]
colors = Maybe [Int]
colors_
          }
  parseJSON Value
_ = Parser BackgroundFill
forall a. Monoid a => a
mempty

instance AT.ToJSON BackgroundFill where
  toJSON :: BackgroundFill -> Value
toJSON BackgroundFillSolid
    { color :: BackgroundFill -> Maybe Int
color = Maybe Int
color_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"backgroundFillSolid"
        , Key
"color" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
color_
        ]
  toJSON BackgroundFillGradient
    { top_color :: BackgroundFill -> Maybe Int
top_color      = Maybe Int
top_color_
    , bottom_color :: BackgroundFill -> Maybe Int
bottom_color   = Maybe Int
bottom_color_
    , rotation_angle :: BackgroundFill -> Maybe Int
rotation_angle = Maybe Int
rotation_angle_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"          Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"backgroundFillGradient"
        , Key
"top_color"      Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
top_color_
        , Key
"bottom_color"   Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
bottom_color_
        , Key
"rotation_angle" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
rotation_angle_
        ]
  toJSON BackgroundFillFreeformGradient
    { colors :: BackgroundFill -> Maybe [Int]
colors = Maybe [Int]
colors_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"  Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"backgroundFillFreeformGradient"
        , Key
"colors" Key -> Maybe [Int] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Int]
colors_
        ]