local assets =
{
    Asset("ANIM", "anim/trap_lunar.zip"),
	Asset("MINIMAP_IMAGE", "toothtrap"),

	    Asset("ATLAS", "inventoryimages/trap_lunar.xml"),
    Asset("IMAGE", "inventoryimages/trap_lunar.tex"),
}

local function onfinished_normal(inst)
    inst:RemoveComponent("inventoryitem")
    inst:RemoveComponent("mine")
    inst.persists = false
    inst.AnimState:PushAnimation("used", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
    inst:DoTaskInTime(3, inst.Remove)
end

local function onused_maxwell(inst)
    inst.AnimState:PlayAnimation("used", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
    inst:DoTaskInTime(3, inst.Remove)
end

local function onfinished_maxwell(inst)
    inst:RemoveComponent("mine")
    inst.persists = false
    inst:DoTaskInTime(1.25, onused_maxwell)
end

local function endanimation(inst)


    end

local function OnExplode(inst, target)
local trap = inst

  inst._fx = SpawnPrefab("lunartrapfx")
    inst._fx.entity:SetParent(inst.entity)
    inst._fx.Transform:SetPosition(0, 1, 0)

if target ~= nil then
    inst.AnimState:PlayAnimation("trap")
        inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_trigger")

target:DoTaskInTime(1, function(inst)
  inst._fx2 = SpawnPrefab("positronbeam_back")
    inst._fx2.entity:SetParent(target.entity)
    inst._fx2.Transform:SetPosition(0, 0, 0)
inst._fx2:DoTaskInTime(1, ErodeAway)
                inst.SoundEmitter:PlaySound("dontstarve/common/together/moonbase/beam_stop_fail")
				end)


    inst.AnimState:PlayAnimation("trap")
    if target then
        inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_trigger")
				target:DoTaskInTime(1, function(inst)
        target.components.combat:GetAttacked(trap, TUNING.TRAP_TEETH_DAMAGE)
		end)

		target:DoTaskInTime(1.3, function(inst)
		        inst.components.combat:GetAttacked(trap, 5)
		end)
		target:DoTaskInTime(1.5, function(inst)
		        inst.components.combat:GetAttacked(trap, 5)
		end)
		target:DoTaskInTime(1.7, function(inst)
		        inst.components.combat:GetAttacked(trap, 5)
		end)
		target:DoTaskInTime(1.9, function(inst)
		        inst.components.combat:GetAttacked(trap, 5)
		end)
    end
    if inst.components.finiteuses then
        inst.components.finiteuses:Use(1)
    end
	end
end

local function OnReset(inst)
    inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_reset")
    inst.AnimState:PlayAnimation("reset")
    inst.AnimState:PushAnimation("idle", false)
end

local function OnResetMax(inst)
    inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_reset")
    inst.AnimState:PlayAnimation("idle")
    --inst.AnimState:PushAnimation("idle", false)
end

local function SetSprung(inst)
    inst.AnimState:PlayAnimation("trap_idle")
end

local function SetInactive(inst)
    inst.AnimState:PlayAnimation("inactive")
end

local function OnDropped(inst)
    inst.components.mine:Deactivate()
end

local function ondeploy(inst, pt, deployer)
    inst.components.mine:Reset()
    inst.Physics:Teleport(pt:Get())
end

--legacy save support - mines used to start out activated
local function onload(inst, data)
    if not data or not data.mine then
        inst.components.mine:Reset()
    end
end

local function common_fn(bank, build, isinventoryitem)
    local inst = CreateEntity()

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

    MakeInventoryPhysics(inst)

    inst.MiniMapEntity:SetIcon("toothtrap.png")

    inst.AnimState:SetBank(bank)
    inst.AnimState:SetBuild(build)
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("trap")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    if isinventoryitem then
        inst:AddComponent("inventoryitem")
        inst.components.inventoryitem.nobounce = true
        inst.components.inventoryitem:SetOnDroppedFn(OnDropped)
    end

	inst.components.inventoryitem.imagename = "trap_lunar"
    inst.components.inventoryitem.atlasname = "inventoryimages/trap_lunar.xml"

    inst:AddComponent("mine")
    inst.components.mine:SetRadius(5)
    inst.components.mine:SetAlignment("player")
    inst.components.mine:SetOnExplodeFn(OnExplode)
    inst.components.mine:SetOnResetFn(OnReset)
    inst.components.mine:SetOnSprungFn(SetSprung)
    inst.components.mine:SetOnDeactivateFn(SetInactive)
    --inst.components.mine:StartTesting()

    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(TUNING.TRAP_TEETH_USES * 3)
    inst.components.finiteuses:SetUses(TUNING.TRAP_TEETH_USES * 3)
    inst.components.finiteuses:SetOnFinished(onfinished_normal)

    inst:AddComponent("deployable")
    inst.components.deployable.ondeploy = ondeploy
    inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.LESS)

    inst:AddComponent("hauntable")
    inst.components.hauntable:SetOnHauntFn(function(inst, haunter)
        if inst.components.mine ~= nil then
            if inst.components.mine.inactive then
                Launch(inst, haunter, TUNING.LAUNCH_SPEED_SMALL)
                inst.components.hauntable.hauntvalue = TUNING.HAUNT_TINY
                    return true
            elseif inst.components.mine.issprung then
                if math.random() <= TUNING.HAUNT_CHANCE_OFTEN then
                    inst.components.hauntable.hauntvalue = TUNING.HAUNT_SMALL
                    inst.components.mine:Reset()
                    return true
                end
            elseif math.random() <= TUNING.HAUNT_CHANCE_HALF then
                inst.components.hauntable.hauntvalue = TUNING.HAUNT_MEDIUM
                inst.components.mine:Explode(
                    FindEntity(
                        inst,
                        TUNING.TRAP_TEETH_RADIUS * 15,
                        function(dude, inst)
                            return not (dude.components.health ~= nil and
                                        dude.components.health:IsDead())
                                and dude.components.combat:CanBeAttacked(inst)
                        end,
                        { "_combat" }, -- see entityscript.lua
                        { "notraptrigger", "flying", "playerghost" },
                        { "monster", "character", "animal" }
                    )
                )
                return true
            end
        end
        return false
    end)

    inst.components.mine:Deactivate()
    inst.OnLoad = onload
    return inst
end

local function MakeTeethTrapNormal()
    return common_fn("trap_lunar", "trap_lunar", true)
end

return Prefab("trap_lunar", MakeTeethTrapNormal, assets),
    MakePlacer("trap_lunar_placer", "trap_lunar", "trap_lunar", "idle")
