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

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

module IHaskell.Display.Widgets.Controller.ControllerButton
  ( -- * The ControllerButton Widget
    ControllerButton
    -- * Constructor
  , mkControllerButton
  ) where

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

import           Data.Aeson
import           Data.IORef (newIORef)
import           Data.Vinyl (Rec(..), (<+>))

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

import           IHaskell.Display.Widgets.Types
import           IHaskell.Display.Widgets.Common
import           IHaskell.Display.Widgets.Layout.LayoutWidget

-- | 'ControllerButton' represents an ControllerButton widget from IPython.html.widgets.
type ControllerButton = IPythonWidget 'ControllerButtonType

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

  let domAttrs :: Rec Attr (CoreWidgetClass ++ DOMWidgetClass)
domAttrs = Rec Attr CoreWidgetClass
defaultCoreWidget forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> Rec Attr DOMWidgetClass
defaultDOMWidget Text
"ControllerButtonView" Text
"ControllerButtonModel" IPythonWidget 'LayoutType
layout
      btnAttrs :: Rec Attr '[ 'FloatValue, 'Pressed, 'ChangeHandler]
btnAttrs = (forall {a :: Field}. (a ~ 'FloatValue) => SField a
FloatValue forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:! Double
0.0)
                 forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Pressed) => SField a
Pressed 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 ~ 'ChangeHandler) => SField a
ChangeHandler forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
                 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 'ControllerButtonType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ Rec
  Attr
  ('ViewModule
     : 'ViewModuleVersion : 'ModelModule : 'ModelModuleVersion
     : DOMWidgetClass)
domAttrs forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec Attr '[ 'FloatValue, 'Pressed, 'ChangeHandler]
btnAttrs

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

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

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

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

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