{-# LANGUAGE RankNTypes, FlexibleContexts, FlexibleInstances #-}

{-
  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 utility functions for composing skills
module Game.Antisplice.Skills (
  -- * Utility types
  Condition,
  Consumer,
  Skill (..),
  ToConsumer (toConsumer),
  -- * Compositors
  (!+), (>!+), (!+>), (>!+>),
  -- * Skill builders
  skill,
  bareAction,
  bareCondition,
  -- * Sample consumers
  focusDirectC,
  optionallyFocusDirectC,
  -- * Final wrappers
  runSkill,
  wrapSkills
  ) where

import Control.Arrow
import Control.Monad
import Control.Monad.Error.Class
import Data.Monoid
import Game.Antisplice.Action
import Game.Antisplice.Errors
import Game.Antisplice.Monad.Dungeon
import Game.Antisplice.Rooms
import Game.Antisplice.Stats
import Game.Antisplice.Utils.None
import Game.Antisplice.Utils.Counter
import Game.Antisplice.Utils.Atoms

-- | A wrapper type for skill execution preconditions.
newtype Condition = Condition { runCondition :: SkillParam -> Prerequisite }

instance Monoid Condition where
  mempty = Condition $ \_ -> return True
  a `mappend` b = Condition $ \p -> do
    a' <- runCondition a p
    b' <- runCondition b p
    return (a' && b')

instance IsAction Condition where
  p #&& q = Condition $ \x -> do
    a <- runCondition p x
    if a then runCondition q x
         else return False
  p #|| q = Condition $ \x -> do
    a <- runCondition p x
    if a then return True
         else runCondition q x
  p !&& q = p <> q
  p !|| q = Condition $ \x -> do
    a <- runCondition p x
    b <- runCondition q x
    return (a || b)
              
instance None Condition where
  none = mempty

-- | A skill. Build it using the '!+' compositor and the 'skill' function.
data Skill = Skill {
    skillConditionOf :: !Condition,
    skillActionOf :: !(SkillParam -> Handler),
    skillNameOf :: !String
  }

-- | A single consumer. Build it using 'bareAction', 'bareCondition' and monoid concatenation.
data Consumer = Consumer !Condition !(SkillParam -> Handler)

instance Monoid Consumer where
  mempty = Consumer none (const noneM)
  (Consumer c1 a1) `mappend` (Consumer c2 a2) = Consumer (c1 <> c2) (\p -> a1 p >> a2 p)

instance IsAction Consumer where
  (Consumer c1 a1) #&& (Consumer c2 a2) = Consumer (c1 #&& c2) (\p -> a1 p >> a2 p)
  (Consumer c1 a1) #|| (Consumer c2 a2) = Consumer (c1 #|| c2) (\p -> runCondition c1 p >>= \q -> if q then a1 p else a2 p)
  p !&& q = p <> q
  (Consumer c1 a1) !|| (Consumer c2 a2) = Consumer (c1 !|| c2) (\p -> runCondition c1 p >>= \q -> runCondition c2 p >> if q then a1 p else a2 p)

instance None Consumer where
  none = mempty

-- | Focus direct object
focusDirectC :: Consumer
focusDirectC = bareAction (\p -> case p of
                                   SkillParam (Just o) _ _ -> focusOpponent $ objectIdOf o
                                   _ -> throwError UnintellegibleError)

-- | Optionally ocus direct object (obligatory if none is focused yet)
optionallyFocusDirectC :: Consumer
optionallyFocusDirectC = bareAction (\p -> case p of
                                        SkillParam (Just o) _ _ -> focusOpponent $ objectIdOf o
                                        SkillParam Nothing _ _ -> do
                                          o <- liftM playerOpponentOf getPlayerState
                                          case o of
                                            FalseObject -> throwError UnintellegibleError
                                            _ -> noneM)

-- | Typeclass for everything that may act as a consumer.
class ToConsumer c where
  toConsumer :: c -> Consumer

instance ToConsumer Consumer where
  toConsumer = id

instance ToConsumer Skill where
  toConsumer (Skill c a n) = Consumer c a

instance ToConsumer HandlerBox where
  toConsumer (Handler t) = Consumer none (const t)

instance ToConsumer (SkillParam -> HandlerBox) where
  toConsumer t = Consumer none (runHandler . t)

instance ToConsumer Condition where
  toConsumer c = Consumer c (const noneM)

instance ToConsumer PrerequisiteBox where
  toConsumer p = Consumer (Condition $ \_ -> runPrerequisite p) (const noneM)

instance ToConsumer Action where
  toConsumer a = Consumer (Condition $ \_ -> askAction a) (\_ -> runAction a)

infixl 5 !+
-- | Add a consumer to the skill.
(!+) :: ToConsumer c => Skill -> c -> Skill
s !+ c = s !+! toConsumer c
  where (Skill c a n) !+! (Consumer c1 a1) = Skill (c <> c1) (\p -> a p >> a1 p) n

infixl 5 >!+
-- | Add a consumer to the monadic skill
(>!+) :: (ToConsumer c,Monad m) => m Skill -> c -> m Skill
m >!+ c = do
  s <- m
  return (s !+ c)

infixl 5 !+>
-- | Add a monadic consumer to the skill
(!+>) :: (ToConsumer c,Monad m) => Skill -> m c -> m Skill
s !+> m = do
  c <- m
  return (s !+ c)

infixl 5 >!+>
-- | Add a monadic consumer to the monadic skill
(>!+>) :: (ToConsumer c,Monad m) => m Skill -> m c -> m Skill
ms >!+> mc = do
  s <- ms
  c <- mc
  return (s !+ c)

-- | Build a consumer from an action.
bareAction :: (SkillParam -> Handler) -> Consumer
bareAction t = toConsumer $ \p -> Handler (t p)

-- | Build a consumer from a condition.
bareCondition :: (SkillParam -> ChattyDungeonM Bool) -> Consumer
bareCondition = toConsumer.Condition

-- | Build a bogus skill that does nothing but has a name. Use this with '!+' to build powerful skills.
skill :: String -> Skill
skill = Skill none (const noneM)

-- | Wrap the skills into a form that is accepted by stereotypes.
wrapSkills :: [Skill] -> String -> Maybe (SkillParam -> HandlerBox)
wrapSkills sk n =
  case filter ((==n).skillNameOf) sk of
    [] -> Nothing
    sk:_ -> Just $ \p -> Handler (runSkill sk p)
    
-- | Run the given skill
runSkill :: Skill -> SkillParam -> Handler
runSkill (Skill c a _) p = do
  b <- runCondition c p
  unless b $ throwError CantCastThatNowError
  a p