{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}

{-
  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 dealing with stats and stereotypes
module Game.Antisplice.Stats where

import Control.Monad
import Control.Monad.Identity
import Data.Monoid
import Game.Antisplice.Monad.Dungeon
import Game.Antisplice.Rooms
import Game.Antisplice.Utils.AVL
import Game.Antisplice.Utils.Atoms
import Game.Antisplice.Utils.None
import Text.Chatty.Printer

-- | Typeclass for every pure data that saves stats
class HasStats s where
  -- | Set the given key to the given value
  setStat :: StatKey -> Int -> s -> s
  -- | Lookup the given key
  getStat :: StatKey -> s -> Int

-- | Typeclass for every monad that saves stats
class HasStatsM m where
  -- | Set the given key to the given value
  setStatM :: StatKey -> Int -> m ()
  -- | Lookup the given key
  getStatM :: StatKey -> m Int

instance HasStats PlayerState where
  setStat k v p = p{playerBaseStatsOf=avlInsert (k,v) $ playerBaseStatsOf p}
  getStat k p = case avlLookup k $ playerBaseStatsOf p of
    Nothing -> 0
    Just v -> v

instance Monad m => HasStatsM (PlayerT m) where
  setStatM k v = modifyPlayerState $ setStat k v
  getStatM k = (return . getStat k) =<< getPlayerState

-- | Calculates the stats of the objects the player carries
calcStat :: (MonadPlayer m,MonadAtoms m,MonadRoom m) => StatKey -> m Int
calcStat k = do
  p <- getPlayerState
  let ss k = getStat k p
  stes <- totalStereo
  return $ stereoCalcStatBonus stes ss k + ss k

-- | Calculates the total stereotype the player carries
totalStereo :: (MonadAtoms m,MonadPlayer m,MonadRoom m) => m PlayerStereo
totalStereo = do
  p <- getPlayerState
  r <- getRoomState
  let steseg r1 (Stereo r a)
        | r1 == r = [a]
        | otherwise = []
      steseg _ _ = []
      rsegs = concatMap (steseg Near) $ concatMap (avlInorder.objectFeaturesOf) $ avlInorder $ roomObjectsOf r
      isegs = concatMap (steseg Carried) $ concatMap (avlInorder.objectFeaturesOf) $ avlInorder $ playerInventoryOf p
      wsegs = concatMap (steseg Worn) $ concatMap (avlInorder.objectFeaturesOf) $ map snd $ avlInorder $ playerEquipOf p
  stes <- mapM getAtom (rsegs ++ isegs ++ wsegs ++ playerStereosOf p)
  return $ mconcat stes

instance Monoid PlayerStereo where
  mempty = PlayerStereo (\_ _ -> 0) (\_ -> Nothing) (\_ -> Nothing)
  a `mappend` b = PlayerStereo
    (\get k -> stereoCalcStatBonus a (\k -> stereoCalcStatBonus b get k + get k) k + stereoCalcStatBonus b get k)
    (\s -> case stereoSkillBonus a s of
          Nothing -> stereoSkillBonus b s
          Just b -> Just b)
    (\s -> case stereoRecipeBonus a s of
          Nothing -> stereoRecipeBonus b s
          Just b -> Just b)

instance None PlayerStereo where
  none = mempty