module Dodge.Default.Weapon
    where
import Dodge.Data
import Dodge.Reloading.Action
import Dodge.Item.Weapon.Bullet
import Dodge.Default.Item
import Dodge.Item.Draw
import Picture
import ShapePicture
import Shape
--import Geometry.Vector3D
import Geometry
--import Dodge.TweakBullet

import qualified Data.Map.Strict as M
import Control.Lens

defaultLeftLoadable :: ItemConsumption
defaultLeftLoadable = AutoRecharging 10 10 100 100

defaultLoadable :: ItemConsumption
defaultLoadable = LoadableAmmo 
    { _laAmmoType   = GenericAmmo
    , _laMax        = 15
    , _laLoaded     = 0
    , _laPrimed     = True
    , _laCycle      = [loadEject 10, loadInsert 10, loadPrime 10]
    , _laProgress   = Nothing
    }
defaultBulletLoadable :: ItemConsumption
defaultBulletLoadable = defaultLoadable & laAmmoType .~ basicBullet
defaultChargeable :: ItemConsumption
defaultChargeable = ChargeableAmmo 100 100

ruseRate :: Int 
    -> (Item -> Creature -> World -> World) 
    -> HammerType
    -> [(Item -> Creature -> World -> World) -> Item -> Creature -> World -> World]
    -> ItemUse
ruseRate i f ht usemods = RightUse
    { _rUse = f
    , _useDelay = FixedRate
        { _rateMax = i
        , _rateTime = 0
        }
    , _useMods = usemods
    , _useHammer = ht
    , _useAim = defaultAimParams
    , _heldScroll = \_ _ -> id
    }

ruseInstant 
    :: (Item -> Creature -> World -> World) 
    -> HammerType
    -> [(Item -> Creature -> World -> World) -> Item -> Creature -> World -> World]
    -> ItemUse
ruseInstant f ht usemods = RightUse
    { _rUse = f
    , _useDelay = NoDelay
    , _useMods  = usemods
    , _useHammer = ht
    , _useAim = defaultAimParams
    , _heldScroll = \_ _ -> id
    }

defaultrUse :: ItemUse
defaultrUse = RightUse
    { _rUse = \_ _ -> id
    , _useDelay = FixedRate {_rateMax = 8, _rateTime = 0}
    , _useMods = []
    , _useHammer  = HasHammer HammerUp
    , _useAim = defaultAimParams
    , _heldScroll = \_ _ -> id
    }
defaultlUse :: ItemUse
defaultlUse = LeftUse
    { _lUse = \_ _ -> id
    , _useDelay = FixedRate {_rateMax = 8, _rateTime = 0}
    , _useHammer  = NoHammer
    , _eqEq = defaultEquip
    }
defaultEquip :: Equipment
defaultEquip = Equipment
    { _eqSite = GoesOnSpecial
    , _eqUse = \_ _ -> id
    , _eqOnEquip = \_ _ -> id
    , _eqOnRemove = \_ _ -> id
    , _eqParams = NoEquipParams
    , _eqViewDist = Nothing
    }

luseInstantNoH :: (Item -> Creature -> World -> World) -> ItemUse
luseInstantNoH f = defaultlUse
    { _lUse = f
    , _useDelay = NoDelay
    , _useHammer  = NoHammer
    }
defaultAimParams :: AimParams
defaultAimParams = AimParams
    { _aimWeight = 0
    , _aimRange = 0
    , _aimZoom = ItZoom 20 0.2 1
    , _aimStance = OneHand
    }

-- TODO change this
defaultLeftItem :: Item
defaultLeftItem = defaultWeapon
    & itConsumption .~ defaultLeftLoadable

defaultWeapon :: Item
defaultWeapon = defaultItem 
    & itConsumption .~ defaultLoadable
    & itUse .~ defaultrUse
    & itDimension .~ defItDimCol white
    & itEquipPict .~ pictureWeaponOnAim
    & itInvColor .~ white
    & itType . iyModules .~ M.fromList 
        [(ModBullet, EMPTYMODULE)
        ,(ModTarget, EMPTYMODULE)
        ,(ModBulletTrajectory, EMPTYMODULE)
        ,(ModTeleport, EMPTYMODULE)
        ]

defaultBulletWeapon :: Item
defaultBulletWeapon = defaultWeapon & itConsumption .~ defaultBulletLoadable 

defaultItemValue :: ItemValue
defaultItemValue = ItemValue 10 MundaneItem
defaultCraftable :: Item
defaultCraftable = defaultItem 
    { _itEquipPict = pictureWeaponOnAim
    , _itInvColor = green
    , _itDimension = defItDimCol green
    }
defItDim :: ItemDimension
defItDim = ItemDimension
    { _dimRad = 2
    , _dimCenter = V3 0 0 0
    , _dimPortage = HeldItem
        { _handlePos = 0
        , _muzPos    = 0
        }
    , _dimSPic = const $ noPic $ colorSH green $ upperPrismPoly 3 $ square 4
    }
defItDimCol :: Color -> ItemDimension
defItDimCol col = ItemDimension
    { _dimRad = 2
    , _dimCenter = V3 0 0 0
    , _dimPortage = HeldItem
        { _handlePos = 0
        , _muzPos    = 0
        }
    , _dimSPic = const $ noPic $ colorSH col $ upperPrismPoly 3 $ square 4
    }
defBulletShooter :: ItemParams
defBulletShooter = BulletShooter
    { _muzVel = 1
    , _rifling = 0.8
    , _bore = 2
    , _gunBarrels = SingleBarrel 0
    , _recoil = 0
    , _torqueAfter = 0
    , _randomOffset = 0
    }
    
defaultAutoGun :: Item
defaultAutoGun = defaultBulletWeapon 
    & itUse . useAim . aimStance .~ TwoHandTwist
