{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module IHaskell.Display.Widgets.Link.DirectionalLink
  ( -- * The DirectionalLink Widget
    DirectionalLink
    -- * Constructor
  , mkDirectionalLink
    -- * Another constructor
  , jsdlink
  ) where

-- To keep `cabal repl` happy when running from the ihaskell repo
import           Prelude

import           Data.Aeson
import           Data.IORef (newIORef)

import           IHaskell.Display
import           IHaskell.Eval.Widgets
import           IHaskell.IPython.Message.UUID as U

import           IHaskell.Display.Widgets.Types
import           IHaskell.Display.Widgets.Common

-- | An 'DirectionalLink' represents a DirectionalLink widget from IPython.html.widgets.
type DirectionalLink = IPythonWidget 'DirectionalLinkType

-- | Create a new DirectionalLink widget
mkDirectionalLink :: IO DirectionalLink
mkDirectionalLink :: IO DirectionalLink
mkDirectionalLink = do
  -- Default properties, with a random uuid
  UUID
wid <- IO UUID
U.random

  let widgetState :: WidgetState 'DirectionalLinkType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ FieldType 'ModelName -> Rec Attr LinkClass
defaultLinkWidget Text
"DirectionalLinkModel"

  IORef (WidgetState 'DirectionalLinkType)
stateIO <- forall a. a -> IO (IORef a)
newIORef WidgetState 'DirectionalLinkType
widgetState

  let widget :: DirectionalLink
widget = forall (w :: WidgetType).
UUID -> IORef (WidgetState w) -> IPythonWidget w
IPythonWidget UUID
wid IORef (WidgetState 'DirectionalLinkType)
stateIO

  -- Open a comm for this widget, and store it in the kernel state
  forall a. IHaskellWidget a => a -> Value -> IO ()
widgetSendOpen DirectionalLink
widget forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Value
toJSON WidgetState 'DirectionalLinkType
widgetState

  -- Return the DirectionalLink widget
  forall (m :: * -> *) a. Monad m => a -> m a
return DirectionalLink
widget

-- | An easier constructor that links two widgets
jsdlink :: WidgetFieldPair -> WidgetFieldPair -> IO DirectionalLink
jsdlink :: WidgetFieldPair -> WidgetFieldPair -> IO DirectionalLink
jsdlink WidgetFieldPair
wfp1 WidgetFieldPair
wfp2 = do
  DirectionalLink
dlink <- IO DirectionalLink
mkDirectionalLink
  ()
_ <- forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w),
 ToPairs (Attr f)) =>
IPythonWidget w -> SField f -> FieldType f -> IO ()
setField DirectionalLink
dlink forall {a :: Field}. (a ~ 'Source) => SField a
Source WidgetFieldPair
wfp1
  ()
_ <- forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w),
 ToPairs (Attr f)) =>
IPythonWidget w -> SField f -> FieldType f -> IO ()
setField DirectionalLink
dlink forall {a :: Field}. (a ~ 'Target) => SField a
Target WidgetFieldPair
wfp2
  forall (m :: * -> *) a. Monad m => a -> m a
return DirectionalLink
dlink

instance IHaskellWidget DirectionalLink where
  getCommUUID :: DirectionalLink -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid