{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE RecordWildCards   #-}
{-|
Module      : $header$
Copyright   : (c) Laurent P René de Cotret, 2020
License     : GNU GPL, version 2 or above
Maintainer  : laurent.decotret@outlook.com
Stability   : internal
Portability : portable

Rendering Plotly/R plots code blocks
-}

module Text.Pandoc.Filter.Plot.Renderers.PlotlyR (
      plotlyRSupportedSaveFormats
    , plotlyRCommand
    , plotlyRCapture
    , plotlyRAvailable
) where

import qualified Data.Text as T
import           Text.Pandoc.Filter.Plot.Renderers.Prelude


plotlyRSupportedSaveFormats :: [SaveFormat]
plotlyRSupportedSaveFormats :: [SaveFormat]
plotlyRSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
JPG, SaveFormat
EPS, SaveFormat
HTML]


plotlyRCommand :: OutputSpec -> Text -> Text
plotlyRCommand :: OutputSpec -> Text -> Text
plotlyRCommand OutputSpec{FilePath
FigureSpec
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} Text
exe = [st|#{exe} "#{oScriptPath}"|]


plotlyRAvailable :: PlotM Bool
plotlyRAvailable :: PlotM Bool
plotlyRAvailable = do
    Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
PlotlyR
    case Maybe Executable
mexe of 
        Maybe Executable
Nothing -> Bool -> PlotM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        Just (Executable FilePath
dir Text
exe) -> 
            FilePath -> Text -> PlotM Bool
commandSuccess FilePath
dir [st|#{exe} -e 'if(!require("plotly")) {quit(status=1)}'|]


plotlyRCapture :: FigureSpec -> FilePath -> Script
plotlyRCapture :: FigureSpec -> FilePath -> Text
plotlyRCapture FigureSpec
fs FilePath
fp = 
    [Text] -> Text
T.unlines [ Text
"pdf(NULL)" -- Prevent the creation of empty Rplots.pdf

              , FigureSpec -> Text
script FigureSpec
fs
              , FigureSpec -> FilePath -> Text
plotlyRCaptureFragment FigureSpec
fs FilePath
fp
              ]


plotlyRCaptureFragment :: FigureSpec -> FilePath -> Script
plotlyRCaptureFragment :: FigureSpec -> FilePath -> Text
plotlyRCaptureFragment spec :: FigureSpec
spec@FigureSpec{Bool
Int
FilePath
[FilePath]
[(Text, Text)]
Attr
Text
SaveFormat
Toolkit
blockAttrs :: FigureSpec -> Attr
extraAttrs :: FigureSpec -> [(Text, Text)]
dependencies :: FigureSpec -> [FilePath]
dpi :: FigureSpec -> Int
directory :: FigureSpec -> FilePath
saveFormat :: FigureSpec -> SaveFormat
withSource :: FigureSpec -> Bool
caption :: FigureSpec -> Text
toolkit :: FigureSpec -> Toolkit
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [FilePath]
dpi :: Int
directory :: FilePath
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
toolkit :: Toolkit
script :: FigureSpec -> Text
..} FilePath
fname = case SaveFormat
saveFormat of
    SaveFormat
HTML -> FigureSpec -> FilePath -> Text
plotlyRCaptureHtml FigureSpec
spec FilePath
fname
    SaveFormat
_    -> FigureSpec -> FilePath -> Text
plotlyRCaptureStatic FigureSpec
spec FilePath
fname


-- Based on the following discussion:

--    https://stackoverflow.com/q/34580095

plotlyRCaptureHtml :: FigureSpec -> FilePath -> Script
plotlyRCaptureHtml :: FigureSpec -> FilePath -> Text
plotlyRCaptureHtml FigureSpec
_ FilePath
fname = [st|
library(plotly) # just in case
library(htmlwidgets)
p <- last_plot()
htmlwidgets::saveWidget(as_widget(p), "#{toRPath fname}")
|]


-- Based on the following documentation:

--    https://plotly.com/r/static-image-export/

plotlyRCaptureStatic :: FigureSpec -> FilePath -> Script
plotlyRCaptureStatic :: FigureSpec -> FilePath -> Text
plotlyRCaptureStatic FigureSpec
_ FilePath
fname = [st|
library(plotly) # just in case
if (!require("processx")) install.packages("processx")
pdf(NULL)
orca(last_plot(), file = "#{toRPath fname}")
|]