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

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

module IHaskell.Display.Widgets.Style.DescriptionStyle
  ( -- * Description style
    DescriptionStyle
    -- * Create a new description style
  , mkDescriptionStyle
  ) where

-- To keep `cabal repl` happy when running from the ihaskell repo
import           Prelude

import           Data.Aeson
import           Data.IORef (newIORef)

import           IHaskell.Display
import           IHaskell.Eval.Widgets
import           IHaskell.IPython.Message.UUID as U

import           IHaskell.Display.Widgets.Types

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

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

  let stl :: Rec Attr DescriptionStyleClass
stl = FieldType 'ModelName -> Rec Attr DescriptionStyleClass
defaultDescriptionStyleWidget Text
"DescriptionStyleModel"
      btnStlState :: WidgetState 'DescriptionStyleType
btnStlState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState Rec
  Attr
  '[ 'ModelName, 'ViewName, 'ViewModule, 'ViewModuleVersion,
     'ModelModule, 'ModelModuleVersion, 'DescriptionWidth]
stl

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

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

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

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

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