{-# LANGUAGE NoImplicitPrelude, OverloadedStrings, FlexibleInstances #-}

-- | If you are interested in the IHaskell library for the purpose of augmenting the IHaskell
-- notebook or writing your own display mechanisms and widgets, this module contains all functions
-- you need.
--
-- In order to create a display mechanism for a particular data type, write a module named (for
-- example) @IHaskell.Display.YourThing@ in a package named @ihaskell-yourThing@. (Note the
-- capitalization - it's important!) Then, in that module, add an instance of @IHaskellDisplay@ for
-- your data type. Similarly, to create a widget, add an instance of @IHaskellWidget@.
--
-- An example of creating a display is provided in the
-- <http://gibiansky.github.io/IHaskell/demo.html demo notebook>.
--
module IHaskell.Display (
    -- * Rich display and interactive display typeclasses and types
    IHaskellDisplay(..),
    Display(..),
    DisplayData(..),
    IHaskellWidget(..),

    -- ** Interactive use functions
    printDisplay,

    -- * Constructors for displays
    plain,
    html,
    bmp,
    png,
    jpg,
    gif,
    svg,
    latex,
    markdown,
    javascript,
    json,
    vega,
    vegalite,
    vdom,
    widgetdisplay,
    custom,
    many,

    -- ** Image and data encoding functions
    Width,
    Height,
    Base64,
    encode64,
    base64,

    -- * Internal only use
    displayFromChanEncoded,
    serializeDisplay,
    Widget(..),
    ) where

import           IHaskellPrelude
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as CBS
import qualified Data.ByteString.Lazy as LBS

import           Data.Binary as Binary
import qualified Data.ByteString.Base64 as Base64

import           Control.Concurrent.STM (atomically)
import           Control.Concurrent.STM.TChan
import           System.IO.Unsafe (unsafePerformIO)

import qualified Data.Text.Encoding as E

import           IHaskell.Types
import           IHaskell.Eval.Util (unfoldM)
import           StringUtils (rstrip)

type Base64 = Text

-- | Encode many displays into a single one. All will be output.
many :: [Display] -> Display
many :: [Display] -> Display
many = [Display] -> Display
ManyDisplay

-- | Generate a plain text display.
plain :: String -> DisplayData
plain :: String -> DisplayData
plain = MimeType -> Text -> DisplayData
DisplayData MimeType
PlainText forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
rstrip

-- | Generate an HTML display.
html :: String -> DisplayData
html :: String -> DisplayData
html = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeHtml forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate an SVG display.
svg :: String -> DisplayData
svg :: String -> DisplayData
svg = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeSvg forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a LaTeX display.
latex :: String -> DisplayData
latex :: String -> DisplayData
latex = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeLatex forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a Javascript display.
javascript :: String -> DisplayData
javascript :: String -> DisplayData
javascript = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeJavascript forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a Json display.
json :: String -> DisplayData
json :: String -> DisplayData
json = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeJson forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a Vega display.
vega :: String -> DisplayData
vega :: String -> DisplayData
vega = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeVega forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a Vegalite display.
vegalite :: String -> DisplayData
vegalite :: String -> DisplayData
vegalite = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeVegalite forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a Vdom display.
vdom :: String -> DisplayData
vdom :: String -> DisplayData
vdom = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeVdom forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a custom display. The first argument is the mimetype and the second argument is the
-- payload.
custom :: T.Text -> String -> DisplayData
custom :: Text -> String -> DisplayData
custom Text
mimetype = MimeType -> Text -> DisplayData
DisplayData (Text -> MimeType
MimeCustom Text
mimetype) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a Markdown display.
markdown :: String -> DisplayData
markdown :: String -> DisplayData
markdown = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeMarkdown forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Generate a GIF display of the given width and height. Data must be provided in a Base64 encoded
-- manner, suitable for embedding into HTML. The @base64@ function may be used to encode data into
-- this format.
gif :: Width -> Height -> Base64 -> DisplayData
gif :: Width -> Width -> Text -> DisplayData
gif Width
width Width
height = MimeType -> Text -> DisplayData
DisplayData (Width -> Width -> MimeType
MimeGif Width
width Width
height)

-- | Generate a BMP display of the given width and height. Data must be provided in a Base64 encoded
-- manner, suitable for embedding into HTML. The @base64@ function may be used to encode data into
-- this format.
bmp :: Width -> Height -> Base64 -> DisplayData
bmp :: Width -> Width -> Text -> DisplayData
bmp Width
width Width
height = MimeType -> Text -> DisplayData
DisplayData (Width -> Width -> MimeType
MimeBmp Width
width Width
height)

-- | Generate a PNG display of the given width and height. Data must be provided in a Base64 encoded
-- manner, suitable for embedding into HTML. The @base64@ function may be used to encode data into
-- this format.
png :: Width -> Height -> Base64 -> DisplayData
png :: Width -> Width -> Text -> DisplayData
png Width
width Width
height = MimeType -> Text -> DisplayData
DisplayData (Width -> Width -> MimeType
MimePng Width
width Width
height)

-- | Generate a JPG display of the given width and height. Data must be provided in a Base64 encoded
-- manner, suitable for embedding into HTML. The @base64@ function may be used to encode data into
-- this format.
jpg :: Width -> Height -> Base64 -> DisplayData
jpg :: Width -> Width -> Text -> DisplayData
jpg Width
width Width
height = MimeType -> Text -> DisplayData
DisplayData (Width -> Width -> MimeType
MimeJpg Width
width Width
height)

-- | Generate a Widget display given the uuid and the view version
widgetdisplay :: String -> DisplayData
widgetdisplay :: String -> DisplayData
widgetdisplay = MimeType -> Text -> DisplayData
DisplayData MimeType
MimeWidget forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> Text
T.pack

-- | Convert from a string into base 64 encoded data.
encode64 :: String -> Base64
encode64 :: String -> Text
encode64 String
str = ByteString -> Text
base64 forall a b. (a -> b) -> a -> b
$ String -> ByteString
CBS.pack String
str

-- | Convert from a ByteString into base 64 encoded data.
base64 :: ByteString -> Base64
base64 :: ByteString -> Text
base64 = ByteString -> Text
E.decodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
Base64.encode

-- | For internal use within IHaskell. Serialize displays to a ByteString.
serializeDisplay :: Display -> LBS.ByteString
serializeDisplay :: Display -> ByteString
serializeDisplay = forall a. Binary a => a -> ByteString
Binary.encode

-- | Items written to this chan will be included in the output sent to the frontend (ultimately the
-- browser), the next time IHaskell has an item to display.
{-# NOINLINE displayChan #-}
displayChan :: TChan Display
displayChan :: TChan Display
displayChan = forall a. IO a -> a
unsafePerformIO forall a. IO (TChan a)
newTChanIO

-- | Take everything that was put into the 'displayChan' at that point out, and make a 'Display' out
-- of it.
displayFromChanEncoded :: IO LBS.ByteString
displayFromChanEncoded :: IO ByteString
displayFromChanEncoded =
  forall a. Binary a => a -> ByteString
Binary.encode forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Display] -> Display
many forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IO (Maybe a) -> IO [a]
unfoldM (forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TChan a -> STM (Maybe a)
tryReadTChan TChan Display
displayChan)

-- | Write to the display channel. The contents will be displayed in the notebook once the current
-- execution call ends.
printDisplay :: IHaskellDisplay a => a -> IO ()
printDisplay :: forall a. IHaskellDisplay a => a -> IO ()
printDisplay a
disp = forall a. IHaskellDisplay a => a -> IO Display
display a
disp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. STM a -> IO a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. TChan a -> a -> STM ()
writeTChan TChan Display
displayChan