{-# 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-python code blocks
-}

module Text.Pandoc.Filter.Plot.Renderers.PlotlyPython (
      plotlyPythonSupportedSaveFormats
    , plotlyPythonCommand
    , plotlyPythonCapture
    , plotlyPythonAvailable
) where

import           Text.Pandoc.Filter.Plot.Renderers.Prelude


plotlyPythonSupportedSaveFormats :: [SaveFormat]
plotlyPythonSupportedSaveFormats :: [SaveFormat]
plotlyPythonSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
JPG, SaveFormat
WEBP, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
EPS]


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


plotlyPythonAvailable :: PlotM Bool
plotlyPythonAvailable :: PlotM Bool
plotlyPythonAvailable = do
    FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
PlotlyPython
    Text -> PlotM Bool
commandSuccess [st|#{exe} -c "import plotly.graph_objects"|]


plotlyPythonCapture :: FigureSpec -> FilePath -> Script
plotlyPythonCapture :: FigureSpec -> FilePath -> Text
plotlyPythonCapture _ fname :: FilePath
fname = [st|
import plotly.graph_objects as go
__current_plotly_figure = next(obj for obj in globals().values() if type(obj) == go.Figure)
__current_plotly_figure.write_image(r"#{fname}")
|]