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

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

module IHaskell.Display.Widgets.Selection.SelectionRangeSlider
  ( -- * The SelectionRangeSlider Widget
    SelectionRangeSlider
    -- * Constructor
  , mkSelectionRangeSlider
  ) 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(..), (<+>), rput)

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 'SelectionRangeSlider' represents a SelectionSlider widget from IPyhon.widgets
type SelectionRangeSlider = IPythonWidget 'SelectionRangeSliderType

-- | Create a new SelectionRangeSlider widget
mkSelectionRangeSlider :: IO SelectionRangeSlider
mkSelectionRangeSlider :: IO SelectionRangeSlider
mkSelectionRangeSlider = do
  UUID
wid <- IO UUID
U.random
  IPythonWidget 'LayoutType
layout <- IO (IPythonWidget 'LayoutType)
mkLayout
  DescriptionStyle
dstyle <- IO DescriptionStyle
mkDescriptionStyle

  let selectionAttrs :: Rec Attr MultipleSelectionClass
selectionAttrs = FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr MultipleSelectionClass
defaultMultipleSelectionWidget Text
"SelectionRangeSliderView" Text
"SelectionRangeSliderModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
      selectionRangeSliderAttrs :: Rec Attr '[ 'Orientation, 'ReadOut, 'ContinuousUpdate]
selectionRangeSliderAttrs = (forall {a :: Field}. (a ~ 'Orientation) => SField a
Orientation forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: OrientationValue
HorizontalOrientation)
                                  forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'ReadOut) => SField a
ReadOut 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 ~ 'ContinuousUpdate) => SField a
ContinuousUpdate 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 'SelectionRangeSliderType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ forall k (r :: k) (rs :: [k]) (record :: (k -> *) -> [k] -> *)
       (f :: k -> *).
(RecElem record r r rs rs (RIndex r rs), RecElemFCtx record f) =>
f r -> record f rs -> record f rs
rput (forall {a :: Field}. (a ~ 'Indices) => SField a
Indices forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> (FieldType f, FieldType f -> IO (FieldType f)) -> Attr f
=:. ([Integer
0,Integer
0], [Integer] -> IO [Integer]
rangeSliderVerification)) 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]
selectionAttrs forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec Attr '[ 'Orientation, 'ReadOut, 'ContinuousUpdate]
selectionRangeSliderAttrs

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

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

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

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

instance IHaskellWidget SelectionRangeSlider where
  getCommUUID :: SelectionRangeSlider -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: SelectionRangeSlider -> Value -> (Value -> IO ()) -> IO ()
comm SelectionRangeSlider
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' SelectionRangeSlider
widget forall {a :: Field}. (a ~ 'Indices) => SField a
Indices [Integer]
indicesList
        forall (w :: WidgetType).
('SelectionHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerSelection SelectionRangeSlider
widget
      Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()