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

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

module IHaskell.Display.Widgets.Int.BoundedInt.Play
  ( -- * The Play Widget
    Play
    -- * Constructor
  , mkPlay
  ) where

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

import           Control.Monad (void)
import           Data.Aeson
import           Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import           Data.Vinyl (Rec(..), (<+>))

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

import           IHaskell.Display (IHaskellWidget(..))
import           IHaskell.Display.Widgets.Types
import           IHaskell.Display.Widgets.Common
import           IHaskell.Display.Widgets.Layout.LayoutWidget
import           IHaskell.Display.Widgets.Style.DescriptionStyle

-- | 'Play' represents an Play widget from IPython.html.widgets.
type Play = IPythonWidget 'PlayType

-- | Create a new widget
mkPlay :: IO Play
mkPlay :: IO Play
mkPlay = do
  -- Default properties, with a random uuid
  UUID
wid <- IO UUID
U.random
  IPythonWidget 'LayoutType
layout <- IO (IPythonWidget 'LayoutType)
mkLayout
  DescriptionStyle
dstyle <- IO DescriptionStyle
mkDescriptionStyle

  let boundedIntAttrs :: Rec Attr BoundedIntClass
boundedIntAttrs = FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr BoundedIntClass
defaultBoundedIntWidget Text
"PlayView" Text
"PlayModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
      playAttrs :: Rec
  Attr
  '[ 'Playing, 'Repeat, 'Interval, 'StepInt, 'Disabled, 'ShowRepeat]
playAttrs = (forall {a :: Field}. (a ~ 'Playing) => SField a
Playing forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
True)
                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Repeat) => SField a
Repeat forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
True)
                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Interval) => SField a
Interval forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Integer
100)
                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'StepInt) => SField a
StepInt forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: forall a. a -> Maybe a
Just Integer
1)
                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Disabled) => SField a
Disabled forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
False)
                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'ShowRepeat) => SField a
ShowRepeat forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
True)
                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& forall {u} (a :: u -> *). Rec a '[]
RNil
      widgetState :: WidgetState 'PlayType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ Rec
  Attr
  '[ 'ViewModule, 'ViewModuleVersion, 'ModelModule,
     'ModelModuleVersion, 'ModelName, 'ViewName, 'DOMClasses, 'Tooltip,
     'Layout, 'DisplayHandler, 'Description, 'Style, 'IntValue,
     'ChangeHandler, 'MaxInt, 'MinInt]
boundedIntAttrs forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec
  Attr
  '[ 'Playing, 'Repeat, 'Interval, 'StepInt, 'Disabled, 'ShowRepeat]
playAttrs

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

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

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

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

instance IHaskellWidget Play where
  getCommUUID :: Play -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: Play -> Value -> (Value -> IO ()) -> IO ()
comm Play
widget Value
val Value -> IO ()
_ =
    case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"state", Text
"value"] of
      Just (Number Scientific
value) -> do
        forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w)) =>
IPythonWidget w -> SField f -> FieldType f -> IO (Attr f)
setField' Play
widget forall {a :: Field}. (a ~ 'IntValue) => SField a
IntValue (Scientific -> Integer
Sci.coefficient Scientific
value)
        forall (w :: WidgetType).
('ChangeHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerChange Play
widget
      Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()