{-# LANGUAGE MultiParamTypeClasses #-}

module StateMachine where

import Affection

import Types

import Load
import MainGame.WorldMap
import MainGame.MindMap

instance StateMachine State UserData where
  smLoad (Main _) = loadMap

  smLoad Load = loadLoad

  smUpdate (Main WorldMap) = updateMap
  smUpdate (Main MindMap) = updateMind

  smUpdate Load = updateLoad

  smDraw (Main WorldMap) = drawMap
  smDraw (Main MindMap) = drawMind

  smDraw Load = drawLoad

  smEvent _ evs = do
    Subsystems w m k <- subsystems <$> getAffection
    _ <- consumeSDLEvents k =<< consumeSDLEvents w =<< consumeSDLEvents m evs
    return ()

  smClean _ = do
    ud <- getAffection
    let Subsystems w m k = subsystems ud
        toClean          = uuid ud
    mapM_ (\u -> do
      partUnSubscribe w u
      partUnSubscribe m u
      partUnSubscribe k u
      ) toClean
    putAffection ud
      { uuid = []
      }
