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

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

module IHaskell.Display.Widgets.Selection.SelectMultiple
  ( -- * The SelectMultiple Widget
    SelectMultiple
    -- * Constructor
  , mkSelectMultiple
  ) 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 qualified Data.Vector as V
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
import           IHaskell.Display.Widgets.Style.DescriptionStyle

-- | A 'SelectMultiple' represents a SelectMultiple widget from IPython.html.widgets.
type SelectMultiple = IPythonWidget 'SelectMultipleType

-- | Create a new SelectMultiple widget
mkSelectMultiple :: IO SelectMultiple
mkSelectMultiple :: IO SelectMultiple
mkSelectMultiple = 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 multipleSelectionAttrs :: Rec Attr MultipleSelectionClass
multipleSelectionAttrs = FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr MultipleSelectionClass
defaultMultipleSelectionWidget Text
"SelectMultipleView" Text
"SelectMultipleModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
      selectMultipleAttrs :: Rec Attr '[ 'Rows]
selectMultipleAttrs = (forall {a :: Field}. (a ~ 'Rows) => SField a
Rows forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: forall a. a -> Maybe a
Just Integer
5)
                            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 'SelectMultipleType
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, 'OptionsLabels,
     'Indices, 'Disabled, 'SelectionHandler]
multipleSelectionAttrs forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec Attr '[ 'Rows]
selectMultipleAttrs

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

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

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

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

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