
local assets =
{
	Asset("ANIM", "anim/venus_butterfy.zip"),
	Asset("ANIM", "anim/swap_venusbutterfly.zip"),
	Asset( "IMAGE", "images/inventoryimages/venus_butterfy.tex" ),
    Asset( "ATLAS", "images/inventoryimages/venus_butterfy.xml" ),
}
local prefabs =
{
    "venus_flywings",
	"venus_flylight",
	"butterfly",
	"venus_butterfy_fx",
}

local function onfinished(inst)
	inst:Remove()
end
local function fly_do_trail(inst)
    local owner = inst.components.inventoryitem:GetGrandOwner() or inst
    if not owner.entity:IsVisible() then
        return
    end

    local x, y, z = owner.Transform:GetWorldPosition()
    if owner.sg ~= nil and owner.sg:HasStateTag("moving") then
        local theta = -owner.Transform:GetRotation() * DEGREES
        local speed = owner.components.locomotor:GetRunSpeed() * .1
        x = x + speed * math.cos(theta)
        z = z + speed * math.sin(theta)
    end
	local map = GetWorld().Map
    local offset = FindValidPositionByFan(
        math.random() * 2 * PI,
         .5 + math.random() * .5,
        4,
        function(offset)
            local pt = Vector3(x + offset.x, 0, z + offset.z)
            return map:GetTileAtPoint(pt:Get())	
                and #TheSim:FindEntities(pt.x, 0, pt.z, .7, { "shadowtrail" }) <= 0 
        end
    )
    if offset ~= nil then
		SpawnPrefab("venus_butterfy_fx").Transform:SetPosition(x + offset.x, 0, z + offset.z)
    end
end

local function fly_equipped(inst)
    if inst._trailtask == nil then
        inst._trailtask = inst:DoPeriodicTask(6 * FRAMES, fly_do_trail, 2 * FRAMES)
    end
end

local function fly_unequipped(inst)
    if inst._trailtask ~= nil then
        inst._trailtask:Cancel()
        inst._trailtask = nil
    end
end
local function onequip(inst, owner)
	owner.AnimState:OverrideSymbol("swap_object", "swap_venusbutterfly", "swap_venusbutterfly")
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
        if not inst.fx then 
            inst.fx = SpawnPrefab( "venus_flylight" )
            local followerfx = inst.fx.entity:AddFollower()
            followerfx:FollowSymbol( owner.GUID, "swap_object", 15, -70, 0)
        end		                                                            
end

local function onunequip(inst, owner)
	if inst.fx then 
        inst.fx:Remove()
        inst.fx = nil
    end 
	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")
end
local function spawnbutterfly(inst,owner)
	local clock = GetWorld().components.clock
	local owner = inst.components.inventoryitem.owner 
	if owner ~= nil  and inst.components.equippable:IsEquipped() then
		if clock:IsDay() then  
        local x,y,z = owner.Transform:GetWorldPosition()
		local fly = SpawnPrefab("butterfly") 
			if fly then
            fly.Transform:SetPosition(x,y,z)
			end
		end
	end
end
local function fn(Sim)
    local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	MakeInventoryPhysics(inst)
	
	inst.AnimState:SetBuild("venus_butterfy")
	inst.AnimState:SetBank("venus_butterfy")
	inst.AnimState:PlayAnimation("idle")

	inst:AddTag("sharp")
    inst:AddTag("pointy")

	inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(17)
    inst.components.weapon:SetRange(8, 10)
	
	inst:AddComponent("inspectable")
	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/venus_butterfy.xml"

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip( onequip )
	inst.components.equippable:SetOnUnequip( onunequip )
    if IsDLCEnabled(CAPY_DLC) then
	inst.components.equippable.walkspeedmult = 0.25
	else
	inst.components.equippable.walkspeedmult = 1.25
	end
	
	inst:ListenForEvent( "daytime",  function() spawnbutterfly(inst) end, GetWorld())
	inst:ListenForEvent("equipped", fly_equipped)
	inst:ListenForEvent("unequipped", fly_unequipped)
	return inst
end

return Prefab( "common/inventory/venus_butterfy", fn, assets, prefabs)