{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
 - Module      : Haskengl.Internal.IO.Window
 - Description : Create, resize, and destroy window
 - Copyright   : Andromeda 2025
 - License     : WTFPL
 - Maintainer  : Matrix @Andromeda:tchncs.de
 - Stability   : Experimental
 -}
module Haskengl.Internal.IO.Window where

import Relude

import qualified Haskengl.Internal.Bindings.OpenGL as GL
import           Haskengl.Internal.Bindings.OpenGL         (($=))
import qualified Haskengl.Internal.Bindings.GLFW   as GLFW
import           Haskengl.Internal.Types                   (DisplayableObjects)
import           Haskengl.Internal.IO.Interrupts           (keyPressed, shutdownWindow, resizeWindow)
import           Haskengl.Internal.IO.Shaders.Init         (initResources)

-- | open a window with sensible presets
openWindow :: String -> IO (GLFW.Window, DisplayableObjects)
openWindow
  title
  = do
    GLFW.init
    GLFW.defaultWindowHints
    GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 4)
    GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 5)
    GLFW.windowHint (GLFW.WindowHint'Samples (Just 16))
    GLFW.windowHint (GLFW.WindowHint'OpenGLProfile GLFW.OpenGLProfile'Core)
    monitor <- GLFW.getPrimaryMonitor
    Just window <- GLFW.createWindow 256 256 title monitor Nothing
    GLFW.makeContextCurrent (Just window)
    GLFW.setWindowCloseCallback window (Just shutdownWindow)
    GLFW.setWindowSizeCallback window (Just resizeWindow)
    GLFW.setKeyCallback window (Just keyPressed)
    objects <- initResources window
    return (window, objects)
