{-
  This module is part of Antisplice.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Antisplice is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Antisplice is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Antisplice. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides a variable-storing monad and functions for access (they almost form an arrow)
module Game.Antisplice.Utils.Atoms where

import Control.Arrow
import qualified Control.Category as C
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
import Data.Dynamic
import Data.Typeable
import Game.Antisplice.Utils.AVL
import Game.Antisplice.Utils.Counter

-- | Phantom type for atom IDs
newtype Atom a = Atom Int deriving (Ord,Eq)

-- | The storage monad
newtype AtomStoreT m a = AtomStore { runAtomStoreT :: AVL (Int,Dynamic) -> m (a,AVL (Int,Dynamic)) }

instance Functor m => Functor (AtomStoreT m) where
  fmap f a = AtomStore $ \s -> fmap (first f) $ runAtomStoreT a s

instance Monad m => Monad (AtomStoreT m) where
  return a = AtomStore $ \s -> return (a,s)
  m >>= f = AtomStore $ \s -> do (a,s') <- runAtomStoreT m s; runAtomStoreT (f a) s'

instance MonadTrans AtomStoreT where
  lift m = AtomStore $ \s -> do a <- m; return (a,s)

instance MonadIO m => MonadIO (AtomStoreT m) where
  liftIO = lift . liftIO

instance MonadCounter m => MonadCounter (AtomStoreT m) where
  countOn = lift countOn

-- | Typeclass for all atom-storing monads.
class MonadCounter m => MonadAtoms m where
  -- | Reserve a new atom.
  newAtom :: Typeable v => m (Atom v)
  -- | Save a value for the given atom.
  putAtom :: Typeable v => Atom v -> v -> m ()
  -- | Get the value from a given atom.
  getAtom :: Typeable v => Atom v -> m v
  -- | Dispose the given atom.
  dispAtom :: Atom v -> m ()
  -- | Clone the given atom.
  cloneAtom :: Typeable v => Atom v -> m (Atom v)
  cloneAtom a = do
    b <- newAtom
    v <- getAtom a
    putAtom b v
    return b

instance (Functor m,MonadCounter m) => MonadAtoms (AtomStoreT m) where
  newAtom = fmap Atom $ lift countOn
  putAtom (Atom a) v = AtomStore $ \s -> return ((),avlInsert (a,toDyn v) s)
  getAtom (Atom a) = AtomStore $ \s -> let Just v = avlLookup a s in return (fromDyn v undefined,s)
  dispAtom (Atom a) = AtomStore $ \s -> return ((),avlRemove a s)

-- | Atomar operation (almost arrow)
newtype (Typeable a,Typeable b) => Atomar m a b = Atomar { runAtomar :: Atom a -> m (Atom b) }

{-instance MonadAtoms m => C.Category (Atomar m) where
  id = Atomar return
  a . b = Atomar (runAtomar a <=< runAtomar b)

instance MonadAtoms m => Arrow (Atomar m) where
  arr = Atomar . mapAtom
  first f = Atomar $ \a -> do
    b <- cloneAtom a
    b' <- mapAtom fst b
    c <- runAtomar f b'
    v <- getAtom c
    mapAtom (first $ const v) a
    dispAtom c-}

-- | Run a pure function on atoms.
mapAtom :: (Typeable a, Typeable b, MonadAtoms m) => (a -> b) -> Atom a -> m (Atom b)
mapAtom f (Atom a) = do
  v <- getAtom (Atom a)
  putAtom (Atom a) $ f v
  return (Atom a)