{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, 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 printer class that may broadcast to all channels that fulfill a condition.
module Text.Chatty.Channel.Broadcast where

import Control.Arrow
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Text.Chatty.Printer
import Text.Chatty.Channel.Printer

-- | Typeclass for all channel printers that may broadcast to all channels that fulfill a condition.
class ChChannelPrinter c m => ChBroadcaster c m where
  -- | Print the string to all channels that fulfill the current condition.
  bprint :: (c -> m Bool) -> String -> m ()

-- | Typeclass for all broadcaster that may embrace sections
class ChBroadcaster c m => ChBroadcasterBracket c m where
  -- | Run the function and print to all channels that fulfill the given condition.
  bbracket :: (c -> m Bool) -> m a -> m a
  bbracket c -> m Bool
f m a
m = (c -> m Bool) -> m ()
forall c (m :: * -> *).
ChBroadcasterBracket c m =>
(c -> m Bool) -> m ()
bstart c -> m Bool
f m () -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m a
m m a -> (a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> (c -> m Bool) -> m ()
forall c (m :: * -> *).
ChBroadcasterBracket c m =>
(c -> m Bool) -> m ()
bfin c -> m Bool
f m () -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
  -- | From now on use the given condition to determine the channels to print to.
  bstart :: (c -> m Bool) -> m ()
  -- | Return to the previous condition. The argument is bogus (just for type inference).
  bfin :: (c -> m Bool) -> m ()