local assets=
{
    Asset("ANIM", "anim/parrrley.zip"),
    Asset("ANIM", "anim/swap_parrrley.zip"),

    Asset("IMAGE", "images/inventoryimages/parrrley.tex"),   
    Asset("ATLAS", "images/inventoryimages/parrrley.xml"),

    Asset("ANIM", "anim/ui_sg_1x1.zip"), 
}
 
local prefabs =
{
    "impact",
}
 
local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_parrrley", "swap_speargun")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
    inst.components.container:Open(owner)
end
 
local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_object")
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    inst.components.container:Close(owner)
end
 
-- local function onhit(inst, attacker, target)
--     local impactfx = SpawnPrefab("impact")
--     if impactfx and attacker then
--         local follower = impactfx.entity:AddFollower()
--         follower:FollowSymbol(target.GUID, target.components.combat.hiteffectsymbol, 0, 0, 0 )
--         impactfx:FacePoint(attacker.Transform:GetWorldPosition())
--     end
--     inst:Remove()
-- end
 
--local function onthrown(inst, data)
   -- inst.AnimState:SetOrientation( ANIM_ORIENTATION.OnGround )
   -- inst.AnimState:PlayAnimation("parrrley")
   -- inst.SoundEmitter:PlaySound("dontstarve/common/blackpowder_explo")
--end
 
 local function onattack_gun(inst, owner, target)
    --if owner.components.sanity and owner.components.sanity:GetPercent() < 1.1 and not target:HasTag("wall") then
       -- owner.components.sanity:DoDelta(-1)        
   -- end
    owner.SoundEmitter:PlaySound("dontstarve/creatures/eyeballturret/shotexplo")
    SpawnPrefab("die_fx").Transform:SetPosition(target.Transform:GetWorldPosition())
    TheCamera:Shake("FULL", 0.2, 0.02, .5, 40)
    inst.components.container:ConsumeByName("bullet", 1)
    inst.components.container:Open(owner) 
end

 local function offattack_gun(inst, owner, target)

end

local slotpos = {}

	table.insert(slotpos, Vector3(-70, -110 ,0))


local function itemtest(inst, item, slot)
	return item.prefab == "bullet"
end


local function fn()
 
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.AnimState:SetBank("speargun")
    inst.AnimState:SetBuild("parrrley")

    inst.AnimState:PlayAnimation("idle")

    MakeInventoryFloatable(inst, "idle_water", "idle")

    inst:AddTag("speargun") 
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(500)
    inst.components.weapon:SetRange(30, 30)
    inst.components.weapon:SetOnAttack(onattack_gun)
    -------
    inst:AddComponent("finiteuses")
    inst:AddComponent("inspectable")
 
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/parrrley.xml"
 
    --inst:AddComponent("stackable")
 
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("container")
	inst.components.container.itemtestfn = itemtest
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.widgetslotpos = slotpos
    inst.components.container.widgetanimbank = "ui_backpack_2x4"
    inst.components.container.widgetanimbuild = "ui_sg_1x1"
    inst.components.container.widgetpos = Vector3(-150,-325,0)
	inst.components.container.widgetpos_controller = Vector3(-150,-325,0)
    inst.components.container.side_widget = true
    inst.components.container.type = "magazine"
    
    inst:DoPeriodicTask(1/100, function()
        if inst.components.equippable.isequipped then
          if not inst.components.container:Has("bullet", 1) then
                   
                   inst:RemoveTag("speargun")
                   inst:AddTag("spear") 
                   inst.components.weapon:SetDamage(10)
                   inst.components.weapon:SetRange(0, 0)
                   inst.components.weapon:SetOnAttack(offattack_gun)

            else
                     
            	   inst:AddTag("speargun")
            	   inst:RemoveTag("spear") 
                   inst.components.weapon:SetDamage(500)
                   inst.components.weapon:SetRange(30, 30)
                   inst.components.weapon:SetOnAttack(onattack_gun)
                
           end
        end
    end) 

    -- inst:DoPeriodicTask(1/100, function()
    --     if inst.components.equippable.isequipped then
    --        if not GetPlayer().components.inventory:Has("gunpowder", 1) then
    --             inst:DoTaskInTime(0.5, function() GetPlayer().components.inventory:DropItem(inst)
    --             end) 
    --         end
    --     end
    -- end)  
    
    inst.components.equippable.equipstack = true
 
    inst.AnimState:PlayAnimation("idle")

    return inst 
end
 
return Prefab( "common/inventory/parrrley", fn, assets)