content: module WebDriverPreCore.BiDi.Session where
  
  import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), object, withObject, (.:), (.=))
  import Data.Aeson.Types (Parser)
  import Data.Text (Text)
  import GHC.Generics (Generic)
  import WebDriverPreCore.BiDi.Capabilities (CapabilitiesResult)
  import Prelude (Applicative ((<*>)), Bool (..), Eq (..), Maybe (..), Show (..), ($), (<$>))
  
  -- ######### Remote #########
  
  -- | Subscription
  newtype Subscription = MkSubscription {subscriptionId :: Text}
    deriving (Show, Eq, Generic, FromJSON, ToJSON)
  
  
  -- | Subscription Request
  data SessionSubscriptionRequest = MkSessionSubscriptionRequest
    { events :: [Text],
      contexts :: Maybe [Text],
      userContexts :: Maybe [Text]
    }
    deriving (Show, Eq, Generic)
  
  instance ToJSON SessionSubscriptionRequest
  
  -- | Unsubscribe Parameters
  data SessionUnsubscribeParameters
    = UnsubscribeByID SessionUnsubscribeByIDRequest
    | UnsubscribeByAttributes SessionUnsubscribeByAttributesRequest
    deriving (Show, Eq, Generic)
  
  instance ToJSON SessionUnsubscribeParameters
  
  -- | Unsubscribe By ID Request
  newtype SessionUnsubscribeByIDRequest = MkSessionUnsubscribeByIDRequest
    { subscriptions :: [Subscription]
    }
    deriving (Show, Eq, Generic)
  
  instance ToJSON SessionUnsubscribeByIDRequest
  
  -- | Unsubscribe By Attributes Request
  data SessionUnsubscribeByAttributesRequest = MkSessionUnsubscribeByAttributesRequest
    { unsubEvents :: [Text],
      unsubContexts :: Maybe [Text]
    }
    deriving (Show, Eq, Generic)
  
  instance ToJSON SessionUnsubscribeByAttributesRequest
  
  -- ######### Local #########
  
  -- | Session New Result
  data SessionNewResult = MkSessionNewResult
    { sessionId :: Text,
      capabilities :: CapabilitiesResult
    }
    deriving (Show, Eq, Generic)
  
  instance ToJSON SessionNewResult where
    toJSON :: SessionNewResult -> Value
    toJSON (MkSessionNewResult sessionId capabilities) =
      object
        [ "sessionId" .= sessionId,
          "capabilities" .= capabilities
        ]
  
  instance FromJSON SessionNewResult where
    parseJSON :: Value -> Parser SessionNewResult
    parseJSON = withObject "SessionNewResult" $ \v ->
      MkSessionNewResult
        <$> v .: "sessionId"
        <*> v .: "capabilities"
  
  -- | Session Status Result
  data SessionStatusResult = MkSessionStatusResult
    { ready :: Bool,
      message :: Text
    }
    deriving (Show, Eq, Generic)
  
  instance FromJSON SessionStatusResult
  
  -- | Session Subscribe Result
  newtype SessionSubscribeResult = MkSessionSubscribeResult
    { subscription :: Subscription
    }
    deriving (Show, Eq, Generic)
  
  instance FromJSON SessionSubscribeResult
  