module Dodge.Creature.HandPos where
import Dodge.Data
import Dodge.Creature.Test
--import Shape
import ShapePicture
import Geometry

import Control.Lens
import Data.Maybe

-- the position of a weapon handle
aimingWeaponHandlePos :: Creature -> Item -> Float
aimingWeaponHandlePos cr it = case it ^? itUse. useAim . aimStance of
    Just TwoHandTwist   -> -5
    Just OneHand        -> 14
    Just TwoHandFlat    -> 1.2 * _crRad cr
    Just LeaveHolstered -> 0
    Nothing             -> 0
aimingWeaponZeroPos :: Creature -> Item -> Float
aimingWeaponZeroPos cr it = aimingWeaponHandlePos cr it 
    - fromMaybe 0 (it ^? itDimension . dimPortage . handlePos)
aimingMuzzlePos :: Creature -> Item -> Float
aimingMuzzlePos cr it = aimingWeaponZeroPos cr it 
    + fromMaybe 0 (it ^? itDimension . dimPortage . muzPos)

translatePointToRightHand :: Creature -> Point3 -> Point3
translatePointToRightHand cr = translatePointToRightHand' cr . mirrorV3xz

mirrorV3xz :: Point3 -> Point3
mirrorV3xz (V3 x y z) = V3 x (-y) z

translatePointToRightHand' :: Creature -> Point3 -> Point3
translatePointToRightHand' cr
    | oneH cr = (+.+.+ V3 11 (-3) 20) . rotate3 0.4 -- . rotate3 (-0.5)-- . scaleSH (V3 1 1.5 1)
    | twists cr = (+.+.+ V3 0 5 20) . rotate3 (-1) . (+.+.+ V3 4 (-10) 0)
    | twoFlat cr = (+.+.+ V3 4 (-8) 10)
    | otherwise = case cr ^? crStance . carriage of
        Just (Walking sa LeftForward)  -> (+.+.+ V3 (-f sa) (-off) 10)
        _ -> (+.+.+ V3 0 (-off) 10)
  where
    off = 8
    sLen = _strideLength $ _crStance cr
    f i = negate 2 + negate 6 * fromIntegral (sLen - i) / fromIntegral sLen

translateToRightHand :: Creature -> SPic -> SPic
translateToRightHand cr = translateToRightHand' cr . mirrorSPxz

translateToRightHand' :: Creature -> SPic -> SPic
translateToRightHand' cr
    | oneH cr = shoulderSP . translateSPf 11 (-3) . rotateSP (-0.5)-- . scaleSH (V3 1 1.5 1)
    | twists cr = shoulderSP . translateSPf 0 5 . rotateSP (-1) . translateSPf 4 (-10)
    | twoFlat cr = waistSP . translateSPf 4 (-8)
    | otherwise = case cr ^? crStance . carriage of
        Just (Walking sa LeftForward)  -> waistSP . translateSPf (-f sa) (-off)
        _ -> waistSP . translateSPf 0 (-off)
  where
    off = 8
    sLen = _strideLength $ _crStance cr
    f i = negate 2 + negate 6 * fromIntegral (sLen - i) / fromIntegral sLen

translatePointToLeftHand :: Creature -> Point3 -> Point3
translatePointToLeftHand cr
    | oneH cr = (+.+.+ V3 0 0 10) . rotate3 0.4 . (+.+.+ V3 0 off 0)
    | twists cr = (+.+.+ V3 0 5 20) . rotate3 (-1) . (+.+.+ V3 12 4 0) . rotate3 0.4
    | twoFlat cr = (+.+.+ V3 4 8 10)
    | otherwise = case cr ^? crStance . carriage of
        Just (Walking sa RightForward)  -> (+.+.+ V3 (-f sa) off 10)
        _ -> (+.+.+ V3 0 off 10)
  where
    off = 8
    sLen = _strideLength $ _crStance cr
    f i = negate 2 + negate 6 * fromIntegral (sLen - i) / fromIntegral sLen

translateToLeftHand :: Creature -> SPic -> SPic
translateToLeftHand cr
    | oneH cr = waistSP . rotateSP 0.4 . translateSPf 0 off
    | twists cr = shoulderSP . translateSPf 0 5 . rotateSP (-1) . translateSPf 12 4
    | twoFlat cr = waistSP . translateSPf 4 8
    | otherwise = case cr ^? crStance . carriage of
        Just (Walking sa RightForward)  -> waistSP . translateSPf (-f sa) off
        _ -> waistSP . translateSPf 0 off
  where
    off = 8
    sLen = _strideLength $ _crStance cr
    f i = negate 2 + negate 6 * fromIntegral (sLen - i) / fromIntegral sLen

translateToLeftLeg :: Creature -> SPic -> SPic
translateToLeftLeg cr = rotateSP (_crMvDir cr - _crDir cr) . case cr ^? crStance . carriage of
    Just (Walking sa LeftForward)  -> translateSPf ( f sa)   off
    Just (Walking sa RightForward) -> translateSPf (-f sa)   off
    _ -> translateSPf 0   off
  where
    off = 5
    sLen = _strideLength $ _crStance cr
    f i = 6 * fromIntegral (sLen - i) / fromIntegral sLen

translateToRightLeg :: Creature -> SPic -> SPic
translateToRightLeg cr = rotateSP (_crMvDir cr - _crDir cr) . case cr ^? crStance . carriage of
    Just (Walking sa LeftForward)  -> translateSPf (-f sa) (-off)
    Just (Walking sa RightForward) -> translateSPf ( f sa) (-off)
    _ -> translateSPf 0 (-off)
  where
    off = 5
    sLen = _strideLength $ _crStance cr
    f i = 6 * fromIntegral (sLen - i) / fromIntegral sLen

translateToHead :: Creature -> SPic -> SPic
translateToHead cr
    | twists cr = translateSPz 20 . translateSPf 0 5 . rotateSP (-1) . translateSPf (negate 2.5) 0.25 
            . rotateSP 1
    | oneH   cr = translateSPz 20 . rotateSP 0.5 . translateSPf 2.5 0 
            . rotateSP (negate 0.5)
    | otherwise = translateSPz 20 . translateSPf 2.5 0 

translatePointToHead :: Creature -> Point3 -> Point3
translatePointToHead cr
    | twists cr = (+.+.+ V3 0 5 20) . rotate3 (-1) . (+.+.+ V3 (negate 2.5) 0.25 0)
            . rotate3 1
    | oneH   cr = (+.+.+ V3 0 0 20) . rotate3 0.5 . (+.+.+ V3 2.5 0 0)
            . rotate3 (negate 0.5)
    | otherwise = (+.+.+ V3 2.5 0 20)

translateToChest :: Creature -> SPic -> SPic
translateToChest cr
    | oneH cr   = rotateSP 0.5
--    | twists cr = translateSPf 0 5 . rotateSP (-1)
    | twists cr = rotateSP (-1)
    | otherwise = id

shoulderSP :: SPic -> SPic
shoulderSP = translateSPz 20
waistSP :: SPic -> SPic
waistSP = translateSPz 10
