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

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

module IHaskell.Display.Widgets.Selection.Dropdown
  ( -- * The Dropdown Widget
    Dropdown
    -- * Constructor
  , mkDropdown
  ) 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           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
import           IHaskell.Display.Widgets.Style.DescriptionStyle

-- | A 'Dropdown' represents a Dropdown widget from IPython.html.widgets.
type Dropdown = IPythonWidget 'DropdownType

-- | Create a new Dropdown widget
mkDropdown :: IO Dropdown
mkDropdown :: IO Dropdown
mkDropdown = 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 widgetState :: WidgetState 'DropdownType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr SelectionClass
defaultSelectionWidget Text
"DropdownView" Text
"DropdownModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle

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

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

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

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

instance IHaskellWidget Dropdown where
  getCommUUID :: Dropdown -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: Dropdown -> Value -> (Value -> IO ()) -> IO ()
comm Dropdown
widget Value
val Value -> IO ()
_ =
    case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"state", Text
"index"] of
      Just (Number Scientific
index) -> 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' Dropdown
widget forall {a :: Field}. (a ~ 'OptionalIndex) => SField a
OptionalIndex (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Scientific -> Integer
Sci.coefficient Scientific
index)
        forall (w :: WidgetType).
('SelectionHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerSelection Dropdown
widget
      Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()