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

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

  Chatty 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.

  Chatty 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 Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides a variable-storing monad and functions for access
module Data.Chatty.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 Data.Chatty.AVL
import Data.Chatty.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 ChCounter m => ChCounter (AtomStoreT m) where
  countOn = lift countOn

-- | Typeclass for all atom-storing monads.
class ChCounter m => ChAtoms 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,ChCounter m) => ChAtoms (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, ChAtoms 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)