{-|
IO actions that apply config side effects and save configuration settings to disk.
-}
module Dodge.Config.Update
    where
--import Dodge.Data.SoundOrigin
import Dodge.Config.Data
import Sound
import Data.Preload
import Preload.Update

import Data.Aeson (encodeFile)
--import Control.Monad (when)
{- |
Write the current world configuration to disk as a json file.
 -}
saveConfig :: Configuration -> (a -> IO a) -> a -> IO a
saveConfig cfig f x = do
    putStrLn "Saving config to data/dodge.config.json"
    encodeFile "data/dodge.config.json" cfig
    f x

{- |
Apply the volume settings from the world configuration to the running game.
 -}
setVol :: Configuration -> IO ()
setVol cfig = do
    setSoundVolume ( _volume_master cfig * _volume_sound cfig)
    setMusicVolume ( _volume_master cfig * _volume_music cfig)

setVol' :: Configuration -> (a -> IO a) -> a -> IO a
setVol' cfig f a = do
    setVol cfig 
    f a


{- |
Apply /all/ of the values in the world configuration to the running game.
 -}
applyWorldConfig
    :: Configuration 
    -> PreloadData
    -> IO PreloadData
applyWorldConfig cfig pdata = do
    setVol cfig 
    pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y pdata
  where
    x = round $ _windowX cfig
    y = round $ _windowY cfig
    divRes = _resolution_factor cfig
