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

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

module IHaskell.Display.Widgets.String.Password
  ( -- * The Password Widget
    PasswordWidget
    -- * Constructor
  , mkPassword
  ) where

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

import           Control.Monad (when)
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
import           IHaskell.Display.Widgets.Common
import           IHaskell.Display.Widgets.Layout.LayoutWidget
import           IHaskell.Display.Widgets.Style.DescriptionStyle

-- | A 'PasswordWidget' represents a Password widget from IPython.html.widgets.
type PasswordWidget = IPythonWidget 'PasswordType

-- | Create a new Password widget
mkPassword :: IO PasswordWidget
mkPassword :: IO PasswordWidget
mkPassword = 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 widgetState :: WidgetState 'PasswordType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr TextClass
defaultTextWidget Text
"PasswordView" Text
"PasswordModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle

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

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

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

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

instance IHaskellWidget PasswordWidget where
  getCommUUID :: PasswordWidget -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: PasswordWidget -> Value -> (Value -> IO ()) -> IO ()
comm PasswordWidget
tw Value
val Value -> IO ()
_ = do
    case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"state", Text
"value"] of
      Just (String Text
value) -> forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w)) =>
IPythonWidget w -> SField f -> FieldType f -> IO (Attr f)
setField' PasswordWidget
tw forall {a :: Field}. (a ~ 'StringValue) => SField a
StringValue Text
value forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (w :: WidgetType).
('ChangeHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerChange PasswordWidget
tw
      Maybe Value
_                 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"content", Text
"event"] of
      Just (String Text
event) -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
event forall a. Eq a => a -> a -> Bool
== Text
"submit") forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
('SubmitHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerSubmit PasswordWidget
tw
      Maybe Value
_                   -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()