{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
{-
  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 ChannelPrinter c m => Broadcaster 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 Broadcaster c m => BroadcasterBracket 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 f m = bstart f >> m >>= \a -> bfin f >> return 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 ()