module Dodge.Item.Draw where
import Dodge.Data
--import Dodge.Picture.Layer
--import Picture
--import Shape
import ShapePicture

import qualified Data.IntMap.Strict as IM
import Control.Lens
pictureWeaponOnAim
    :: Creature 
    -> Int  -- ^ Position of item in inventory
    -> SPic
pictureWeaponOnAim cr i = pictureWeaponOnAimItem pic cr i
  where
    it = _crInv cr IM.! i
    pic = _itFloorPict it it

pictureWeaponOnAim'
    :: (Item -> SPic)
    -> Creature 
    -> Int  -- ^ Position of item in inventory
    -> SPic
pictureWeaponOnAim' f cr i = pictureWeaponOnAimItem pic cr i
  where
    pic = f (_crInv cr IM.! i)

pictureWeaponAim
    :: (Item -> SPic)
    -> Creature 
    -> Int  -- ^ Position of item in inventory
    -> SPic
pictureWeaponAim f cr i = pictureWeaponOnAimItem pic cr i
  where
    pic = f (_crInv cr IM.! i)

{- | Places an item picture onto a creature when the item is selected. -}
pictureWeaponOnAimItem
    :: SPic 
    -> Creature 
    -> Int  -- ^ Position of item in inventory
    -> SPic
pictureWeaponOnAimItem p cr posInInv 
    | isSelected && _posture (_crStance cr) == Aiming && isTwisting
                = shoulderD twistWep
    | isSelected && _posture (_crStance cr) == Aiming && isOneHand
                = shoulderD oneHandWep
    | isSelected && _posture (_crStance cr) == Aiming && isTwoHandFlat
                = shoulderD drawnWep
    | isSelected && isTwoHandFlat
                = handD holsteredTwoFlat
    | isSelected && isOneHand
                = handD holsteredOneHandWep
    | isSelected 
                = handD holsteredWep
    | otherwise = emptyBlank
  where 
    shoulderD = translateSPz 18
    handD = translateSPz 15
    isSelected = _crInvSel cr == posInInv 
    drawnWep        = translateSPf (1.2 * _crRad cr) 0 p
    twistWep        = translateSPf (0.5 * _crRad cr) 0 p
    oneHandWep      = translateSPf (1.5 * _crRad cr) 0 p
    holsteredWep    = translateSPf (_crRad cr) 0 (rotateSP (strideRot cr + 1.2) p)
    holsteredTwoFlat    = translateSPf (_crRad cr) 0 (rotateSP (twoFlatHRot cr) p)
    holsteredOneHandWep = translateSPf (_crRad cr * 0.7 + handPos) (_crRad cr * negate 0.7) p
    handPos = case cr^? crStance . carriage of
        Just (Walking x LeftForward) -> f x * 50
        _ -> 0
    theIt = _crInv cr IM.! posInInv
    isTwisting = _itAimStance theIt == TwoHandTwist
    isOneHand = _itAimStance theIt == OneHand
    isTwoHandFlat = _itAimStance theIt == TwoHandFlat
    f i = 0.1 * fromIntegral (sLen - i) / fromIntegral sLen
    sLen = _strideLength $ _crStance cr

strideRot :: Creature -> Float
strideRot cr = case cr ^? crStance . carriage of
        Just (Walking x LeftForward)  -> f x
        Just (Walking x RightForward) -> - f x
        _ -> 0
  where
    f i = 0.1 * fromIntegral (sLen - i) / fromIntegral sLen
    sLen = _strideLength $ _crStance cr

twoFlatHRot :: Creature -> Float
twoFlatHRot cr = 2*strideRot cr

pictureItem
    :: SPic 
    -> Creature 
    -> Int  -- ^ Position of item in inventory
    -> SPic
pictureItem p cr posInInv 
    | _crInvSel cr == posInInv = drawnWep
    | otherwise                = emptyBlank
  where 
    drawnWep = translateSPf (_crRad cr) 0 p
