module Dodge.Save
    ( saveWorldInEmptySlot
    , loadSaveSlot
    , doQuicksave
    , saveLevelStartSlot
    ) where
import Dodge.Data

import qualified Data.Map.Strict as M
--import Data.Maybe
import Control.Lens
--import qualified Data.Set as S

saveWorldInEmptySlot :: SaveSlot -> Universe -> Universe
saveWorldInEmptySlot slot w = case M.lookup slot $ _savedWorlds w of
    Nothing -> w & savedWorlds %~ M.insert slot (clearKeys (_uvWorld w))
    _       -> w

saveWorldInSlot :: SaveSlot -> Universe -> Universe
saveWorldInSlot slot u = u & savedWorlds %~ M.insert slot (clearKeys (_uvWorld u))

loadSaveSlot :: SaveSlot -> Universe -> Universe
loadSaveSlot slot u = maybe u 
    (\w -> u & menuLayers .~ [] & uvWorld .~ w) 
    $ M.lookup slot (_savedWorlds u) 

doQuicksave :: Universe -> Universe
doQuicksave = saveWorldInSlot QuicksaveSlot

clearKeys :: World -> World
clearKeys = (keys .~ mempty) . (mouseButtons .~ mempty)

saveLevelStartSlot :: World -> World
--saveLevelStartSlot = id
saveLevelStartSlot = sideEffects %~ (fmap  (saveWorldInSlot LevelStartSlot) . )
--saveLevelStartSlot = sideEffects %~ (fmap  (undefined) . )
