module Brillo.Interface.Environment where

import Data.IORef (newIORef)
import Data.Text qualified as T

import Brillo.Data.FileDialog (FileDialog)
import Brillo.Internals.Interface.Backend (defaultBackendState)
import Brillo.Internals.Interface.Backend.Types qualified as Backend.Types
import Brillo.Internals.TinyFileDialogs qualified as TinyFileDialogs


{-| Get the size of the screen, in pixels.

  This will be the size of the rendered brillo image when
  fullscreen mode is enabled.
-}
getScreenSize :: IO (Int, Int)
getScreenSize :: IO (Int, Int)
getScreenSize = do
  IORef GLFWState
backendStateRef <- GLFWState -> IO (IORef GLFWState)
forall a. a -> IO (IORef a)
newIORef GLFWState
defaultBackendState
  IORef GLFWState -> Bool -> IO ()
forall a. Backend a => IORef a -> Bool -> IO ()
Backend.Types.initializeBackend IORef GLFWState
backendStateRef Bool
False
  IORef GLFWState -> IO (Int, Int)
forall a. Backend a => IORef a -> IO (Int, Int)
Backend.Types.getScreenSize IORef GLFWState
backendStateRef


{-| Open a file dialog to select files/directories.

  Returns a list of paths, or `Nothing` if the dialog was cancelled.
-}
openFileDialog :: FileDialog -> IO (Maybe [FilePath])
openFileDialog :: FileDialog -> IO (Maybe [FilePath])
openFileDialog FileDialog
fileDialog = do
  IORef GLFWState
backendStateRef <- GLFWState -> IO (IORef GLFWState)
forall a. a -> IO (IORef a)
newIORef GLFWState
defaultBackendState
  IORef GLFWState -> Bool -> IO ()
forall a. Backend a => IORef a -> Bool -> IO ()
Backend.Types.initializeBackend IORef GLFWState
backendStateRef Bool
False
  IORef GLFWState -> FileDialog -> IO (Maybe [FilePath])
forall a.
Backend a =>
IORef a -> FileDialog -> IO (Maybe [FilePath])
Backend.Types.openFileDialog IORef GLFWState
backendStateRef FileDialog
fileDialog


{-| Open a save file dialog.

  Returns the selected file path, or `Nothing` if the dialog was cancelled.
-}
saveFileDialog ::
  -- | Dialog title
  T.Text ->
  -- | Default file path
  T.Text ->
  -- | Filter patterns like @[\"*.svg\", \"*.png\"]@
  [T.Text] ->
  -- | Filter description like @\"Image files\"@
  T.Text ->
  IO (Maybe FilePath)
saveFileDialog :: Text -> Text -> [Text] -> Text -> IO (Maybe FilePath)
saveFileDialog Text
title Text
defaultPath [Text]
filterPatterns Text
filterDescription = do
  Maybe Text
result <-
    Text -> Text -> [Text] -> Text -> IO (Maybe Text)
TinyFileDialogs.saveFileDialog
      Text
title
      Text
defaultPath
      [Text]
filterPatterns
      Text
filterDescription
  Maybe FilePath -> IO (Maybe FilePath)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe FilePath -> IO (Maybe FilePath))
-> Maybe FilePath -> IO (Maybe FilePath)
forall a b. (a -> b) -> a -> b
$ (Text -> FilePath) -> Maybe Text -> Maybe FilePath
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> FilePath
T.unpack Maybe Text
result