local assets=
{
  Asset("ANIM", "anim/bambootree.zip"),
  Asset("ANIM", "anim/bambootree_build.zip"),
}

local prefabs =
{
    "bamboo",
    "dug_bambootree",
    "hacking_bamboo_fx",
}
--[[local respawndays = 2  --tempo para renascer em dias

local function makeanims(stage)
	return {
		idle="idle",
		blown1="blown_loop1",
		blown2="blown_loop2",
		blown_pre="blown_pre",
		blown_pst="blown_pst",
	}
end]]

local function onregenfn(inst)
    inst.AnimState:PlayAnimation("grow")
    inst.AnimState:PushAnimation("idle", true)
	  inst.Physics:SetCollides(true)
end
--[[local function OnTimerDone(inst, data)
    if data.name == "spawndelay" then
      inst.AnimState:PlayAnimation("grow")
      --inst.AnimState:PushAnimation("sway", true)
	    inst.AnimState:PushAnimation("idle",true)
	    inst:AddTag("machetecut")
      inst.components.workable:SetWorkLeft(6)
	    inst.components.workable:SetWorkAction(ACTIONS.HACK)
    end
end]]

local function dig_up(inst, worker)
	if inst.components.pickable and inst.components.pickable:CanBePicked() then
		inst.components.lootdropper:SpawnLootPrefab("bamboo")
	end
	inst.components.lootdropper:SpawnLootPrefab("dug_bambootree")
	inst:Remove()
end
--[[local function dig_up(inst, worker)
  if inst:HasTag("machetecut") then
    inst:RemoveTag("machetecut")
    inst.components.workable:SetWorkAction(ACTIONS.DIG)
    inst.components.workable:SetWorkLeft(1)
    inst.components.lootdropper:SpawnLootPrefab("bamboo")
    inst.components.timer:StartTimer("spawndelay", 60*8*respawndays)

    return
end
	if not inst:HasTag("machetecut") then
		if inst.components.lootdropper ~= nil then
        local withered = inst.components.witherable ~= nil and inst.components.witherable:IsWithered()
			  inst.components.lootdropper:SpawnLootPrefab(
            (withered) and
            "bamboo" or
            "dug_bambootree"
			   )
		end
		inst:Remove()
  end
end]]
local function onpickedfn(inst, picker)
	  local fx = SpawnPrefab("hacking_bamboo_fx")
    local x, y, z= inst.Transform:GetWorldPosition()
    fx.Transform:SetPosition(x,y + math.random()*2,z)

    inst.SoundEmitter:PlaySound("dontstarve/wilson/pickup_reeds")
    inst.AnimState:PlayAnimation("picking")

    if inst.components.pickable:IsBarren() then
        --inst.AnimState:PushAnimation("empty_to_dead")
        inst.AnimState:PushAnimation("idle_dead", false)
		    inst.Physics:SetCollides(true)
    else
        inst.AnimState:PushAnimation("picked", false)
		    inst.Physics:SetCollides(false)
    end
end
--[[local function onhackedfn(inst, hacker, hacksleft)
if not inst:HasTag("machetecut") then return end
	local fx = SpawnPrefab("green_leaves_chop")
    local x, y, z= inst.Transform:GetWorldPosition()
    fx.Transform:SetPosition(x,y,z)

	if(hacksleft <= 0) then

		inst.AnimState:PlayAnimation("picking")
		inst.SoundEmitter:PlaySound("dontstarve/forest/treefall")
	else

		inst.AnimState:PlayAnimation("chop")
		inst.AnimState:PushAnimation("idle")
	end

	inst.SoundEmitter:PlaySound("dontstarve/wilson/use_axe_tree")
end]]


--[[local function OnSave(inst, data)
if not inst:HasTag("machetecut") then
    data.tag = 1
end
end]]

--[[local function OnLoad(inst, data)
    if data and data.tag == 1 then
				inst.AnimState:PlayAnimation("picked")
				inst:RemoveTag("machetecut")
				inst.components.workable:SetWorkAction(ACTIONS.DIG)
				inst.components.workable:SetWorkLeft(1)
    end
end]]

local function makeemptyfn(inst)
    if inst.components.witherable ~= nil and inst.components.witherable:IsWithered() or inst.AnimState:IsCurrentAnimation("idle_dead") then
        inst.AnimState:PlayAnimation("dead_to_empty")
        inst.AnimState:PushAnimation("picked", false)
    else
        inst.AnimState:PlayAnimation("picked")
    end
	inst.Physics:SetCollides(true)
end

local function makebarrenfn(inst, wasempty)
    if inst.components.witherable ~= nil and inst.components.witherable:IsWithered() then
        inst.AnimState:PlayAnimation(wasempty and "empty_to_dead" or "full_to_dead")
        inst.AnimState:PushAnimation("idle_dead", false)
    else
        inst.AnimState:PlayAnimation("idle_dead")
    end
	inst.Physics:SetCollides(true)
end

local function ontransplantfn(inst)
	if inst.components.pickable then
		inst.components.pickable:MakeBarren()
	end
end

local function grass(name, stage)
local function fn()
    local inst = CreateEntity()

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

    inst.MiniMapEntity:SetIcon("bambootree.tex")

  	inst.AnimState:SetBank("bambootree")
  	inst.AnimState:SetBuild("bambootree_build")
  	inst.AnimState:PlayAnimation("idle",true)
  	MakeObstaclePhysics(inst, .35)
    inst:AddTag("renewable")

	  --[[inst:AddTag("machetecut")
	  inst:AddTag("bambootree")

    --witherable (from witherable component) added to pristine state for optimization
    inst:AddTag("witherable")]]

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.AnimState:SetTime(math.random() * 2)
    local color = 0.75 + math.random() * 0.25
    inst.AnimState:SetMultColour(color, color, color, 1)

	  --inst:AddComponent("interactions")
    --inst:AddComponent("witherable")
    inst:AddComponent("inspectable")
    inst:AddComponent("lootdropper")

    inst:AddComponent("pickable")
    inst.components.pickable.picksound = "dontstarve/wilson/pickup_reeds"

    inst.components.pickable:SetUp("bamboo", 1920)
    inst.components.pickable.onregenfn = onregenfn
    inst.components.pickable.onpickedfn = onpickedfn
    inst.components.pickable.makeemptyfn = makeemptyfn
    inst.components.pickable.makebarrenfn = makebarrenfn
    inst.components.pickable.max_cycles = 20
    inst.components.pickable.cycles_left = 20
    inst.components.pickable.ontransplantfn = ontransplantfn

	  --inst:AddComponent("timer")
    --inst:ListenForEvent("timerdone", OnTimerDone)

    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.DIG)
		inst.components.workable:SetOnFinishCallback(dig_up)
		inst.components.workable:SetWorkLeft(1)
    --inst.components.workable:SetWorkAction(ACTIONS.HACK)
    --inst.components.workable:SetOnFinishCallback(dig_up)
    --inst.components.workable:SetWorkLeft(6)
	  --inst.components.workable.onwork = onhackedfn

    if stage == 1 then
        inst.components.pickable:MakeBarren()
    end

    MakeMediumBurnable(inst)
    MakeSmallPropagator(inst)
    MakeHauntableIgnite(inst)
    MakeNoGrowInWinter(inst)

	  --inst.OnSave = OnSave
    --inst.OnLoad = OnLoad

    return inst
end
return Prefab(name, fn, assets, prefabs)
end

return grass("bambootree", 0),
      grass("depleted_bambootree", 1)
