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

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

module IHaskell.Display.Widgets.Style.SliderStyle
  ( -- * Slider style
    SliderStyle
    -- * Create a new slider style
  , mkSliderStyle
  ) 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

-- | A 'SliderStyle' represents a Button Style from IPython.html.widgets.
type SliderStyle = IPythonWidget 'SliderStyleType

-- | Create a new button style
mkSliderStyle :: IO SliderStyle
mkSliderStyle :: IO SliderStyle
mkSliderStyle = do
  UUID
wid <- IO UUID
U.random

  let stl :: Rec Attr DescriptionStyleClass
stl = FieldType 'ModelName -> Rec Attr DescriptionStyleClass
defaultDescriptionStyleWidget Text
"SliderStyleModel"
      but :: Rec Attr '[ 'HandleColor]
but = (forall {a :: Field}. (a ~ 'HandleColor) => SField a
HandleColor forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: forall a. Maybe a
Nothing)
            forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& forall {u} (a :: u -> *). Rec a '[]
RNil
      btnStlState :: WidgetState 'SliderStyleType
btnStlState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState (Rec
  Attr
  '[ 'ModelName, 'ViewName, 'ViewModule, 'ViewModuleVersion,
     'ModelModule, 'ModelModuleVersion, 'DescriptionWidth]
stl forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec Attr '[ 'HandleColor]
but)

  IORef (WidgetState 'SliderStyleType)
stateIO <- forall a. a -> IO (IORef a)
newIORef WidgetState 'SliderStyleType
btnStlState

  let style :: SliderStyle
style = forall (w :: WidgetType).
UUID -> IORef (WidgetState w) -> IPythonWidget w
IPythonWidget UUID
wid IORef (WidgetState 'SliderStyleType)
stateIO

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

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

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