{-# 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           Text.Pandoc.Filter.Plot.Renderers.Prelude


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


plotlyRCommand :: OutputSpec -> PlotM Text
plotlyRCommand :: OutputSpec -> PlotM Text
plotlyRCommand OutputSpec{..} = do
    FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
PlotlyR
    Text -> PlotM Text
forall (m :: * -> *) a. Monad m => a -> m a
return [st|#{exe} "#{oScriptPath}"|]


plotlyRAvailable :: PlotM Bool
plotlyRAvailable :: PlotM Bool
plotlyRAvailable = do
    FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
GGPlot2
    Text -> PlotM Bool
commandSuccess [st|#{exe} -e 'library("plotly")'|]


-- Based on the following documentation:

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

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