{-# LANGUAGE Safe #-}

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

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice 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 MonadSpawn overlay that may catch specific spawn calls and handle them itself.
module System.Chatty.Spawn.Overlay where

import System.Chatty.Spawn
import Control.Applicative
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
import System.IO

-- | MonadSpawn overlay. Carries a map of own command implementations that are called instead of the actual ones.
newtype SpawnOverlayT m a = SpawnOverlay { runSpawnOverlayT :: [(String,[String] -> String -> m (Int,String))] -> m (a,[(String,[String] -> String -> m (Int,String))]) }

instance Monad m => Monad (SpawnOverlayT m) where
  return a = SpawnOverlay $ \o -> return (a,o)
  (SpawnOverlay o) >>= f = SpawnOverlay $ \s -> do (a,s') <- o s; runSpawnOverlayT (f a) s'

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

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

instance Monad m => Functor (SpawnOverlayT m) where
  fmap f a = SpawnOverlay $ \s -> do (a',s') <- runSpawnOverlayT a s; return (f a',s')

instance Monad m => Applicative (SpawnOverlayT m) where
  (<*>) = ap
  pure = return

instance ChSpawn m => ChSpawn (SpawnOverlayT m) where
  mspw pn as (Right si) = SpawnOverlay $ \s ->
    case pn `elem` (map fst s) of
      True -> let c = snd $ head $ filter ((==pn).fst) s
              in do
                (r,so) <- c as si
                return ((r,so,[]),s)
      False -> do
                r <- mspw pn as (Right si)
                return (r,s)
  mspw pn as (Left h) = lift $ mspw pn as (Left h)
  mah pn = SpawnOverlay $ \s ->
    case pn `elem` (map fst s) of
      True -> return (False,s)
      False -> do
        ah <- mah pn
        return (ah,s)