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

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

module IHaskell.Display.Widgets.DatePicker
  ( -- * The DatePicker Widget
    DatePicker
    -- * Create a new DatePicker
  , mkDatePicker
  ) 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 'DatePicker' represents a DatePicker from IPython.html.widgets.
type DatePicker = IPythonWidget 'DatePickerType

-- | Create a new DatePicker
mkDatePicker :: IO DatePicker
mkDatePicker :: IO DatePicker
mkDatePicker = 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
"DatePickerView" Text
"DatePickerModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
      date :: Rec Attr '[ 'DateValue, 'Disabled, 'ChangeHandler]
date = (forall {a :: Field}. (a ~ 'DateValue) => SField a
DateValue forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Date
defaultDate)
              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
      datePickerState :: WidgetState 'DatePickerType
datePickerState = 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 '[ 'DateValue, 'Disabled, 'ChangeHandler]
date)

  IORef (WidgetState 'DatePickerType)
stateIO <- forall a. a -> IO (IORef a)
newIORef WidgetState 'DatePickerType
datePickerState

  let datePicker :: DatePicker
datePicker = forall (w :: WidgetType).
UUID -> IORef (WidgetState w) -> IPythonWidget w
IPythonWidget UUID
wid IORef (WidgetState 'DatePickerType)
stateIO

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

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

instance IHaskellWidget DatePicker where
  getCommUUID :: DatePicker -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: DatePicker -> Value -> (Value -> IO ()) -> IO ()
comm DatePicker
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 Date
date -> forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w)) =>
IPythonWidget w -> SField f -> FieldType f -> IO (Attr f)
setField' DatePicker
widget forall {a :: Field}. (a ~ 'DateValue) => SField a
DateValue Date
date forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (w :: WidgetType).
('ChangeHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerChange DatePicker
widget
        Result Date
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()