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

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

module IHaskell.Display.Widgets.ColorPicker
  ( -- * The ColorPicker Widget
    ColorPicker
    -- * Create a new ColorPicker
  , mkColorPicker
  ) 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
import           IHaskell.Display.Widgets.Style.DescriptionStyle

-- | A 'ColorPicker' represents a ColorPicker from IPython.html.widgets.
type ColorPicker = IPythonWidget 'ColorPickerType

-- | Create a new ColorPicker
mkColorPicker :: IO ColorPicker
mkColorPicker :: IO ColorPicker
mkColorPicker = 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 ddw :: Rec Attr DescriptionWidgetClass
ddw = FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr DescriptionWidgetClass
defaultDescriptionWidget Text
"ColorPickerView" Text
"ColorPickerModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
      color :: Rec Attr '[ 'StringValue, 'Concise, 'Disabled, 'ChangeHandler]
color = (forall {a :: Field}. (a ~ 'StringValue) => SField a
StringValue forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Text
"black")
              forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Concise) => SField a
Concise 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 ~ '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 ~ 'ChangeHandler) => SField a
ChangeHandler forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: forall (m :: * -> *) a. Monad m => a -> m a
return ())
              forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& forall {u} (a :: u -> *). Rec a '[]
RNil
      colorPickerState :: WidgetState 'ColorPickerType
colorPickerState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState (Rec
  Attr
  '[ 'ViewModule, 'ViewModuleVersion, 'ModelModule,
     'ModelModuleVersion, 'ModelName, 'ViewName, 'DOMClasses, 'Tooltip,
     'Layout, 'DisplayHandler, 'Description, 'Style]
ddw forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec Attr '[ 'StringValue, 'Concise, 'Disabled, 'ChangeHandler]
color)

  IORef (WidgetState 'ColorPickerType)
stateIO <- forall a. a -> IO (IORef a)
newIORef WidgetState 'ColorPickerType
colorPickerState

  let colorPicker :: ColorPicker
colorPicker = forall (w :: WidgetType).
UUID -> IORef (WidgetState w) -> IPythonWidget w
IPythonWidget UUID
wid IORef (WidgetState 'ColorPickerType)
stateIO

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

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

instance IHaskellWidget ColorPicker where
  getCommUUID :: ColorPicker -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: ColorPicker -> Value -> (Value -> IO ()) -> IO ()
comm ColorPicker
widget Value
val Value -> IO ()
_ =
    case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"state", Text
"value"] of
      Just Value
o -> case forall a. FromJSON a => Value -> Result a
fromJSON Value
o of
        Success (String Text
color) -> forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w)) =>
IPythonWidget w -> SField f -> FieldType f -> IO (Attr f)
setField' ColorPicker
widget forall {a :: Field}. (a ~ 'StringValue) => SField a
StringValue Text
color forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (w :: WidgetType).
('ChangeHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerChange ColorPicker
widget
        Result Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()