module Test.Marionette.Window where

import Data.Aeson.Types
import Data.Hashable (Hashable)
import Data.Text (Text)
import GHC.Generics (Generic)
import Prelude

-------------------------------------------------------------------

newtype WindowHandle = WindowHandle Text
    deriving newtype
        ( WindowHandle -> WindowHandle -> Bool
(WindowHandle -> WindowHandle -> Bool)
-> (WindowHandle -> WindowHandle -> Bool) -> Eq WindowHandle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WindowHandle -> WindowHandle -> Bool
== :: WindowHandle -> WindowHandle -> Bool
$c/= :: WindowHandle -> WindowHandle -> Bool
/= :: WindowHandle -> WindowHandle -> Bool
Eq
        , Eq WindowHandle
Eq WindowHandle =>
(WindowHandle -> WindowHandle -> Ordering)
-> (WindowHandle -> WindowHandle -> Bool)
-> (WindowHandle -> WindowHandle -> Bool)
-> (WindowHandle -> WindowHandle -> Bool)
-> (WindowHandle -> WindowHandle -> Bool)
-> (WindowHandle -> WindowHandle -> WindowHandle)
-> (WindowHandle -> WindowHandle -> WindowHandle)
-> Ord WindowHandle
WindowHandle -> WindowHandle -> Bool
WindowHandle -> WindowHandle -> Ordering
WindowHandle -> WindowHandle -> WindowHandle
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: WindowHandle -> WindowHandle -> Ordering
compare :: WindowHandle -> WindowHandle -> Ordering
$c< :: WindowHandle -> WindowHandle -> Bool
< :: WindowHandle -> WindowHandle -> Bool
$c<= :: WindowHandle -> WindowHandle -> Bool
<= :: WindowHandle -> WindowHandle -> Bool
$c> :: WindowHandle -> WindowHandle -> Bool
> :: WindowHandle -> WindowHandle -> Bool
$c>= :: WindowHandle -> WindowHandle -> Bool
>= :: WindowHandle -> WindowHandle -> Bool
$cmax :: WindowHandle -> WindowHandle -> WindowHandle
max :: WindowHandle -> WindowHandle -> WindowHandle
$cmin :: WindowHandle -> WindowHandle -> WindowHandle
min :: WindowHandle -> WindowHandle -> WindowHandle
Ord
        , Int -> WindowHandle -> ShowS
[WindowHandle] -> ShowS
WindowHandle -> String
(Int -> WindowHandle -> ShowS)
-> (WindowHandle -> String)
-> ([WindowHandle] -> ShowS)
-> Show WindowHandle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WindowHandle -> ShowS
showsPrec :: Int -> WindowHandle -> ShowS
$cshow :: WindowHandle -> String
show :: WindowHandle -> String
$cshowList :: [WindowHandle] -> ShowS
showList :: [WindowHandle] -> ShowS
Show
        , ReadPrec [WindowHandle]
ReadPrec WindowHandle
Int -> ReadS WindowHandle
ReadS [WindowHandle]
(Int -> ReadS WindowHandle)
-> ReadS [WindowHandle]
-> ReadPrec WindowHandle
-> ReadPrec [WindowHandle]
-> Read WindowHandle
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS WindowHandle
readsPrec :: Int -> ReadS WindowHandle
$creadList :: ReadS [WindowHandle]
readList :: ReadS [WindowHandle]
$creadPrec :: ReadPrec WindowHandle
readPrec :: ReadPrec WindowHandle
$creadListPrec :: ReadPrec [WindowHandle]
readListPrec :: ReadPrec [WindowHandle]
Read
        , Eq WindowHandle
Eq WindowHandle =>
(Int -> WindowHandle -> Int)
-> (WindowHandle -> Int) -> Hashable WindowHandle
Int -> WindowHandle -> Int
WindowHandle -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> WindowHandle -> Int
hashWithSalt :: Int -> WindowHandle -> Int
$chash :: WindowHandle -> Int
hash :: WindowHandle -> Int
Hashable
        )

instance FromJSON WindowHandle where
    parseJSON :: Value -> Parser WindowHandle
parseJSON = String
-> (Text -> Parser WindowHandle) -> Value -> Parser WindowHandle
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"WindowHandle" ((Text -> Parser WindowHandle) -> Value -> Parser WindowHandle)
-> (Text -> Parser WindowHandle) -> Value -> Parser WindowHandle
forall a b. (a -> b) -> a -> b
$ WindowHandle -> Parser WindowHandle
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WindowHandle -> Parser WindowHandle)
-> (Text -> WindowHandle) -> Text -> Parser WindowHandle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> WindowHandle
WindowHandle

instance ToJSON WindowHandle where
    toJSON :: WindowHandle -> Value
toJSON (WindowHandle Text
handle) = [Pair] -> Value
object [Key
"handle" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
handle]

-------------------------------------------------------------------

data WindowType = Tab | Window
    deriving stock (WindowType -> WindowType -> Bool
(WindowType -> WindowType -> Bool)
-> (WindowType -> WindowType -> Bool) -> Eq WindowType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WindowType -> WindowType -> Bool
== :: WindowType -> WindowType -> Bool
$c/= :: WindowType -> WindowType -> Bool
/= :: WindowType -> WindowType -> Bool
Eq, Int -> WindowType -> ShowS
[WindowType] -> ShowS
WindowType -> String
(Int -> WindowType -> ShowS)
-> (WindowType -> String)
-> ([WindowType] -> ShowS)
-> Show WindowType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WindowType -> ShowS
showsPrec :: Int -> WindowType -> ShowS
$cshow :: WindowType -> String
show :: WindowType -> String
$cshowList :: [WindowType] -> ShowS
showList :: [WindowType] -> ShowS
Show, (forall x. WindowType -> Rep WindowType x)
-> (forall x. Rep WindowType x -> WindowType) -> Generic WindowType
forall x. Rep WindowType x -> WindowType
forall x. WindowType -> Rep WindowType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. WindowType -> Rep WindowType x
from :: forall x. WindowType -> Rep WindowType x
$cto :: forall x. Rep WindowType x -> WindowType
to :: forall x. Rep WindowType x -> WindowType
Generic)

instance ToJSON WindowType where
    toJSON :: WindowType -> Value
toJSON WindowType
Tab = Text -> Value
String Text
"tab"
    toJSON WindowType
Window = Text -> Value
String Text
"window"

instance FromJSON WindowType where
    parseJSON :: Value -> Parser WindowType
parseJSON = String -> (Text -> Parser WindowType) -> Value -> Parser WindowType
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"WindowType" \case
        Text
"tab" -> WindowType -> Parser WindowType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure WindowType
Tab
        Text
"window" -> WindowType -> Parser WindowType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure WindowType
Window
        Text
other -> String -> Parser WindowType
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser WindowType) -> String -> Parser WindowType
forall a b. (a -> b) -> a -> b
$ String
"Unknown window type: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
other

-------------------------------------------------------------------

data NewWindowResult = NewWindowResult
    { NewWindowResult -> WindowHandle
newWindowHandle :: WindowHandle
    , NewWindowResult -> WindowType
newWindowType :: WindowType
    }
    deriving stock (Int -> NewWindowResult -> ShowS
[NewWindowResult] -> ShowS
NewWindowResult -> String
(Int -> NewWindowResult -> ShowS)
-> (NewWindowResult -> String)
-> ([NewWindowResult] -> ShowS)
-> Show NewWindowResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NewWindowResult -> ShowS
showsPrec :: Int -> NewWindowResult -> ShowS
$cshow :: NewWindowResult -> String
show :: NewWindowResult -> String
$cshowList :: [NewWindowResult] -> ShowS
showList :: [NewWindowResult] -> ShowS
Show, NewWindowResult -> NewWindowResult -> Bool
(NewWindowResult -> NewWindowResult -> Bool)
-> (NewWindowResult -> NewWindowResult -> Bool)
-> Eq NewWindowResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NewWindowResult -> NewWindowResult -> Bool
== :: NewWindowResult -> NewWindowResult -> Bool
$c/= :: NewWindowResult -> NewWindowResult -> Bool
/= :: NewWindowResult -> NewWindowResult -> Bool
Eq, (forall x. NewWindowResult -> Rep NewWindowResult x)
-> (forall x. Rep NewWindowResult x -> NewWindowResult)
-> Generic NewWindowResult
forall x. Rep NewWindowResult x -> NewWindowResult
forall x. NewWindowResult -> Rep NewWindowResult x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. NewWindowResult -> Rep NewWindowResult x
from :: forall x. NewWindowResult -> Rep NewWindowResult x
$cto :: forall x. Rep NewWindowResult x -> NewWindowResult
to :: forall x. Rep NewWindowResult x -> NewWindowResult
Generic)

instance FromJSON NewWindowResult where
    parseJSON :: Value -> Parser NewWindowResult
parseJSON = String
-> (Object -> Parser NewWindowResult)
-> Value
-> Parser NewWindowResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"NewWindowResult" \Object
o -> do
        WindowHandle
newWindowHandle <- Object
o Object -> Key -> Parser WindowHandle
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"handle"
        WindowType
newWindowType <- Object
o Object -> Key -> Parser WindowType
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
        pure NewWindowResult{WindowType
WindowHandle
newWindowHandle :: WindowHandle
newWindowType :: WindowType
newWindowHandle :: WindowHandle
newWindowType :: WindowType
..}