local assets =
{
    Asset("ANIM", "anim/lunarang.zip"),
    Asset("ANIM", "anim/swap_lunarang.zip"),
	  Asset("ATLAS", "inventoryimages/lunarang.xml"),
		Asset("IMAGE", "inventoryimages/lunarang.tex"),
}

local function OnFinished(inst)
    inst.AnimState:PlayAnimation("used")
    inst:ListenForEvent("animover", inst.Remove)
end

local function OnEquip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_lunarang", "swap_lunarang")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
end

local function OnDropped(inst)
    inst.AnimState:PlayAnimation("idle")
end

local function OnUnequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
end

local function DeleteMe(inst)
inst:Remove()
end

local function OnThrown(inst, owner, target)

if inst:HasTag("split") then
    inst.components.projectile:SetSpeed(12)
   inst:DoTaskInTime(0.5, DeleteMe)
	end
    if target ~= owner then
        inst.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_throw")
    end
    inst.AnimState:PlayAnimation("spin_loop", true)
end

local function OnCaught(inst, catcher)
    inst.components.projectile:SetSpeed(12)
if inst:HasTag("split") then
    inst:Remove()
	end
	local pt = catcher:GetPosition()
local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, 200)
   for i, v in ipairs(ents) do
   		v:RemoveTag("marked")
end

    if catcher ~= nil and catcher.components.inventory ~= nil and catcher.components.inventory.isopen then
        if inst.components.equippable ~= nil and not catcher.components.inventory:GetEquippedItem(inst.components.equippable.equipslot) then
            catcher.components.inventory:Equip(inst)
        else
            catcher.components.inventory:GiveItem(inst)
        end
        catcher:PushEvent("catch")
    end
end

local function ReturnToOwner(inst, owner)
if inst:HasTag("split") then
    inst:Remove()
	end


    if owner ~= nil and not (inst.components.finiteuses ~= nil and inst.components.finiteuses:GetUses() < 1) then
        inst.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_return")
        inst.components.projectile:Throw(owner, owner)
    end
end


local SMASHABLE_TAGS = { "_combat"}
local NON_SMASHABLE_TAGS = { "INLIMBO", "playerghost", "NOCLICK", "player"}
	local bombchance = 0.40
local function OnHit(inst, owner, target)
    inst.components.projectile:SetSpeed(9)
if inst:HasTag("split") then
    inst:Remove()
	end

    if owner == target or owner:HasTag("playerghost") then
	local pt = owner:GetPosition()

if owner ~= nil and owner.components.inventory ~= nil and owner.components.inventory.isopen then
        if inst.components.equippable ~= nil and not owner.components.inventory:GetEquippedItem(inst.components.equippable.equipslot) then
            owner.components.inventory:Equip(inst)
                  owner.components.health:DoDelta(-10)
				     inst.components.projectile:SetSpeed(12)


        else
            owner.components.inventory:GiveItem(inst)
							   owner.components.health:DoDelta(-5)
							     inst.components.projectile:SetSpeed(12)

			end
	else
        owner:PushEvent("catch")
						 owner.components.health:DoDelta(-5)
						     inst.components.projectile:SetSpeed(12)

		end


    else











    if target ~= nil and target:IsValid() then
	 if owner == target then
	 else
	 if inst:HasTag("split") then
	 target.components.combat:GetAttacked(owner, 10, nil, "shadow")
	 else
target.components.combat:GetAttacked(owner, 20, nil, "shadow")
end
				end
	 else
 end

  if owner == target then
  else
  if inst:HasTag("split")then
  else
          ReturnToOwner(inst, owner)
        local impactfx = SpawnPrefab("impact")
        if impactfx ~= nil then
            local follower = impactfx.entity:AddFollower()
            follower:FollowSymbol(target.GUID, target.components.combat.hiteffectsymbol, 0, 0, 0)
            impactfx:FacePoint(inst.Transform:GetWorldPosition())
        end
    end
end

  if inst:HasTag("split")then
  else
	local pt = inst:GetPosition()
    local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, 7, SMASHABLE_TAGS, NON_SMASHABLE_TAGS)
	local boomerang = inst
    for i, v in ipairs(ents) do
  if v ~= nil and v:IsValid() and v.components.health and v.components.combat ~= nil and not v:HasTag("NOCLICK") and not v:HasTag("player") then
  	 v:DoTaskInTime(0, function(inst)
	    if math.random() < bombchance then
	  local test = SpawnPrefab("minirang")
 test.Transform:SetPosition(boomerang.Transform:GetWorldPosition())
  test:AddTag("split")
        test.components.projectile:Throw(owner, inst)

else end






end)
end
end
end
end
end


local function OnMiss(inst, owner, target)
if inst:HasTag("split") then
    inst:Remove()
	end
    if owner == target then
        OnDropped(inst)
    else
        ReturnToOwner(inst, owner)
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()
	    inst.entity:AddSoundEmitter()

    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)

    inst.AnimState:SetBank("lunarang")
    inst.AnimState:SetBuild("lunarang")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetRayTestOnBB(true)

    inst:AddTag("projectile")
    inst:AddTag("thrown")
	inst:AddTag("scarytoprey")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)
    inst.components.weapon:SetRange(TUNING.BOOMERANG_DISTANCE, TUNING.BOOMERANG_DISTANCE)
    -------

    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(150)
    inst.components.finiteuses:SetUses(150)

    inst.components.finiteuses:SetOnFinished(OnFinished)

    inst:AddComponent("inspectable")

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(12)
    inst.components.projectile:SetCanCatch(true)
    inst.components.projectile:SetOnThrownFn(OnThrown)
    inst.components.projectile:SetOnHitFn(OnHit)
    inst.components.projectile:SetOnMissFn(OnMiss)
    inst.components.projectile:SetOnCaughtFn(OnCaught)


    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:SetOnDroppedFn(OnDropped)
			inst.components.inventoryitem.imagename = "lunarang"
    inst.components.inventoryitem.atlasname = "inventoryimages/lunarang.xml"

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(OnEquip)
    inst.components.equippable:SetOnUnequip(OnUnequip)

    MakeHauntableLaunch(inst)

    return inst
end

return Prefab("lunarang", fn, assets)
