{-# LANGUAGE TemplateHaskell, TypeSynonymInstances, FlexibleInstances, MultiParamTypeClasses, FlexibleContexts, UndecidableInstances #-}

{-
  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 some class instances. Nothing really interesting here.
module Game.Antisplice.Monad where

import Game.Antisplice.Templates
import Data.Chatty.AVL
import Data.Chatty.None
import Data.Chatty.Fail
import Data.Chatty.Counter
import Game.Antisplice.Monad.Dungeon
import Game.Antisplice.Monad.Vocab
import Data.Chatty.Atoms
import Data.Chatty.BST
import Game.Antisplice.Errors
import Control.Monad
import Control.Monad.Trans.Class
import Text.Chatty.Expansion
import Text.Chatty.Expansion.Vars
import Text.Chatty.Templates
import Text.Chatty.Interactor
import Text.Chatty.Interactor.Templates
import Text.Chatty.Channel.Printer
import Text.Chatty.Channel.Broadcast
import Text.Chatty.Extended.HTML
import Text.Chatty.Extended.ANSI
import Text.Chatty.Printer
--import Language.Haskell.TH
import System.IO

mkInteractor ''SplErrT mkChatty mkRoom mkDungeon mkObject mkPlayer mkIO mkVocab (mkChannelPrinter ''PlayerId)
mkInteractor ''DungeonT mkPrinter mkDefCP mkExtendedPrinter mkScanner mkFinalizer mkClock mkExpanderEnv mkHistoryEnv mkRandom (mkFail ''SplErr) mkCounter mkAtoms mkIO  mkVocab (mkChannelPrinter ''PlayerId)
mkInteractor ''RoomT mkChatty (mkFail ''SplErr) mkIO mkVocab (mkChannelPrinter ''PlayerId)
mkInteractor ''ObjectT mkChatty (mkFail ''SplErr) mkIO mkVocab (mkChannelPrinter ''PlayerId)
mkInteractor ''CounterT (mkFail ''SplErr) mkRoom mkObject mkDungeon mkPlayer mkIO mkVocab (mkChannelPrinter ''PlayerId)
mkInteractor ''PlayerT mkChatty (mkFail ''SplErr) mkIO mkVocab (mkChannelPrinter ''PlayerId)
mkInteractor ''VocabT mkChatty (mkFail ''SplErr) mkDungeon mkRoom mkPlayer (mkChannelPrinter ''PlayerId)
mkInteractor ''AtomStoreT (mkFail ''SplErr) mkDungeon mkRoom mkVocab mkPlayer (mkChannelPrinter ''PlayerId)
mkInteractor ''PlayerFilterT mkArchiver mkVocab
liftM concat $ forM [''AnsiPrinterT,''HtmlPrinterT,''ExpanderT,''NullExpanderT,''RecorderT] $ \s ->
  mkInteractor s (mkChannelPrinter ''PlayerId) mkDungeon mkPlayer mkRoom (mkFail ''SplErr)

instance ChChannelPrinter PlayerId m => ChBroadcaster PlayerId (DungeonT m) where
  bprint c str = do
    s <- getDungeonState
    forM_ (anyBstInorder $ playersOf s) $ \p -> do
      b <- c $ indexOf p
      when b $ cprint (indexOf p) str