module Rattletrap.Type.Attribute.FlaggedByte where

import qualified Rattletrap.BitGet as BitGet
import qualified Rattletrap.BitPut as BitPut
import qualified Rattletrap.Schema as Schema
import qualified Rattletrap.Type.U8 as U8
import qualified Rattletrap.Utility.Json as Json

data FlaggedByte = FlaggedByte
  { FlaggedByte -> Bool
flag :: Bool
  , FlaggedByte -> U8
byte :: U8.U8
  }
  deriving (FlaggedByte -> FlaggedByte -> Bool
(FlaggedByte -> FlaggedByte -> Bool)
-> (FlaggedByte -> FlaggedByte -> Bool) -> Eq FlaggedByte
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FlaggedByte -> FlaggedByte -> Bool
$c/= :: FlaggedByte -> FlaggedByte -> Bool
== :: FlaggedByte -> FlaggedByte -> Bool
$c== :: FlaggedByte -> FlaggedByte -> Bool
Eq, Int -> FlaggedByte -> ShowS
[FlaggedByte] -> ShowS
FlaggedByte -> String
(Int -> FlaggedByte -> ShowS)
-> (FlaggedByte -> String)
-> ([FlaggedByte] -> ShowS)
-> Show FlaggedByte
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FlaggedByte] -> ShowS
$cshowList :: [FlaggedByte] -> ShowS
show :: FlaggedByte -> String
$cshow :: FlaggedByte -> String
showsPrec :: Int -> FlaggedByte -> ShowS
$cshowsPrec :: Int -> FlaggedByte -> ShowS
Show)

instance Json.FromJSON FlaggedByte where
  parseJSON :: Value -> Parser FlaggedByte
parseJSON = String
-> (Object -> Parser FlaggedByte) -> Value -> Parser FlaggedByte
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Json.withObject String
"FlaggedByte" ((Object -> Parser FlaggedByte) -> Value -> Parser FlaggedByte)
-> (Object -> Parser FlaggedByte) -> Value -> Parser FlaggedByte
forall a b. (a -> b) -> a -> b
$ \Object
object -> do
    Bool
flag <- Object -> String -> Parser Bool
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"flag"
    U8
byte <- Object -> String -> Parser U8
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"byte"
    FlaggedByte -> Parser FlaggedByte
forall (f :: * -> *) a. Applicative f => a -> f a
pure FlaggedByte :: Bool -> U8 -> FlaggedByte
FlaggedByte { Bool
flag :: Bool
flag :: Bool
flag, U8
byte :: U8
byte :: U8
byte }

instance Json.ToJSON FlaggedByte where
  toJSON :: FlaggedByte -> Value
toJSON FlaggedByte
x =
    [Pair] -> Value
Json.object [String -> Bool -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"flag" (Bool -> Pair) -> Bool -> Pair
forall a b. (a -> b) -> a -> b
$ FlaggedByte -> Bool
flag FlaggedByte
x, String -> U8 -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"byte" (U8 -> Pair) -> U8 -> Pair
forall a b. (a -> b) -> a -> b
$ FlaggedByte -> U8
byte FlaggedByte
x]

schema :: Schema.Schema
schema :: Schema
schema = String -> Value -> Schema
Schema.named String
"attribute-flagged-byte" (Value -> Schema) -> Value -> Schema
forall a b. (a -> b) -> a -> b
$ [(Pair, Bool)] -> Value
Schema.object
  [ (String -> Value -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"flag" (Value -> Pair) -> Value -> Pair
forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
Schema.boolean, Bool
True)
  , (String -> Value -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"byte" (Value -> Pair) -> Value -> Pair
forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
U8.schema, Bool
True)
  ]

bitPut :: FlaggedByte -> BitPut.BitPut
bitPut :: FlaggedByte -> BitPut
bitPut FlaggedByte
flaggedByteAttribute = Bool -> BitPut
BitPut.bool (FlaggedByte -> Bool
flag FlaggedByte
flaggedByteAttribute)
  BitPut -> BitPut -> BitPut
forall a. Semigroup a => a -> a -> a
<> U8 -> BitPut
U8.bitPut (FlaggedByte -> U8
byte FlaggedByte
flaggedByteAttribute)

bitGet :: BitGet.BitGet FlaggedByte
bitGet :: BitGet FlaggedByte
bitGet = String -> BitGet FlaggedByte -> BitGet FlaggedByte
forall a. String -> BitGet a -> BitGet a
BitGet.label String
"FlaggedByte" (BitGet FlaggedByte -> BitGet FlaggedByte)
-> BitGet FlaggedByte -> BitGet FlaggedByte
forall a b. (a -> b) -> a -> b
$ do
  Bool
flag <- String -> BitGet Bool -> BitGet Bool
forall a. String -> BitGet a -> BitGet a
BitGet.label String
"flag" BitGet Bool
BitGet.bool
  U8
byte <- String -> BitGet U8 -> BitGet U8
forall a. String -> BitGet a -> BitGet a
BitGet.label String
"byte" BitGet U8
U8.bitGet
  FlaggedByte -> BitGet FlaggedByte
forall (f :: * -> *) a. Applicative f => a -> f a
pure FlaggedByte :: Bool -> U8 -> FlaggedByte
FlaggedByte { Bool
flag :: Bool
flag :: Bool
flag, U8
byte :: U8
byte :: U8
byte }