local assets =
{
	Asset("ANIM", "anim/marsh_tile.zip"),
	Asset("ANIM", "anim/splash.zip"),
}

local function OnSnowCoverChange(inst, thresh)
	thresh = thresh or .02
	local snow_cover = GetSeasonManager() and GetSeasonManager():GetSnowPercent() or 0
	if snow_cover > thresh and not inst.frozen then
		inst.frozen = true
		inst.AnimState:PlayAnimation("frozen")
		inst.SoundEmitter:PlaySound("dontstarve/winter/pondfreeze")
	    inst.components.childspawner:StopSpawning()
		inst.components.childspawner:StopRegen()
		inst.components.periodicspawner:Stop()
		inst.components.fishable:Freeze()
		inst.components.trader:Disable()
        inst.Physics:SetCollisionGroup(COLLISION.OBSTACLES)
        inst.Physics:ClearCollisionMask()
        inst.Physics:CollidesWith(COLLISION.WORLD)
        inst.Physics:CollidesWith(COLLISION.ITEMS)
		for i,item in ipairs(inst.decor) do
			item:Remove()
		end
		inst.decor = {}
	elseif snow_cover < thresh and inst.frozen then
		inst.frozen = false
		inst.AnimState:PlayAnimation("idle_mos")
	    inst.components.childspawner:StartSpawning()
		inst.components.fishable:Unfreeze()
		inst.components.trader:Enable()	
		if inst.fishes and inst.fishes < 10 then
			inst.components.periodicspawner:Stop()
			inst.components.childspawner:StopRegen()
			inst.components.childspawner:StopSpawning()
			inst.components.inspectable.nameoverride = "infestpondclean"
		elseif not inst.fishes then
			inst.components.periodicspawner:Stop()
			inst.components.childspawner:StopRegen()
			inst.components.childspawner:StopSpawning()
			inst.components.inspectable.nameoverride = "infestpondclean"
		end
		inst.Physics:SetCollisionGroup(COLLISION.OBSTACLES)
        inst.Physics:ClearCollisionMask()
        inst.Physics:CollidesWith(COLLISION.WORLD)
        inst.Physics:CollidesWith(COLLISION.ITEMS)
        inst.Physics:CollidesWith(COLLISION.CHARACTERS)
		SpawnPlants(inst, inst.planttype)
	end
end

local function OnGetItemFromPlayer(inst, giver, item)
	if item.prefab == "fish" then
		if not inst.fishes then
			inst.fishes = 1
		else 
			inst.fishes = inst.fishes + 1 
		end
	elseif item.prefab == "eel" then
		if not inst.fishes then
			inst.fishes = 3
		else 
			inst.fishes = inst.fishes + 3
		end
    end
	if inst.fishes and inst.fishes >= 10 then
		GetPlayer().components.talker:Say("This should be enought to kill mosquito eggs.")
	end
end

local function ShouldAcceptItem(inst, item)
	if (item.prefab == "fish" or item.prefab == "eel") then   
		return true
    end
end

local function OnRefuseItem(inst, item)
    local playerprefab = GetPlayer()
    if playerprefab then
		playerprefab.components.talker:Say("I don't think this will be useful.")
    end
end

local function commonfn()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    MakeObstaclePhysics(inst, 4.25)--1.95)
	inst.Transform:SetScale(1.8, 1.8, 1.8)

    anim:SetBuild("marsh_tile")
    anim:SetBank("marsh_tile")
    anim:PlayAnimation("idle_mos", true)
	anim:SetOrientation(ANIM_ORIENTATION.OnGround)
	anim:SetLayer(LAYER_BACKGROUND)
	anim:SetSortOrder(3)

	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon( "pond_mos.png" )

	inst:AddComponent("childspawner")
	inst.components.childspawner:SetRegenPeriod(.1)
	inst.components.childspawner:SetSpawnPeriod(.1)
	inst.components.childspawner:SetMaxChildren(40)
	inst.components.childspawner:StartRegen()
	inst.components.childspawner:StartSpawning()
	inst.components.childspawner.childname = "infestmosquito"
	
	inst:AddComponent("periodicspawner")
    inst.components.periodicspawner:SetPrefab("infestmosquito")
    inst.components.periodicspawner:SetRandomTimes(1,1)
    inst.components.periodicspawner:SetDensityInRange(45, 25)
    inst.components.periodicspawner:SetMinimumSpacing(.1)
    inst.components.periodicspawner:Start()
	
	inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader.onrefuse = OnRefuseItem

	inst:AddComponent("inspectable")
		
	inst:DoPeriodicTask(1, function()
		if inst.fishes and inst.fishes >= 10 then
			inst.components.periodicspawner:Stop()
			inst.components.childspawner:StopRegen()
			inst.components.childspawner:StopSpawning()
			inst.components.inspectable.nameoverride = "infestpondclean"
		end
		if inst:GetTimeAlive() > 1920 then
			local dust = SpawnPrefab("small_puff")
			dust.Transform:SetPosition(inst:GetPosition():Get())
			dust.Transform:SetScale(7,7,7)
			local dust = SpawnPrefab("small_puff")
			dust.Transform:SetPosition(inst:GetPosition():Get())
			dust.Transform:SetScale(5,5,5)
			local dust = SpawnPrefab("collapse_small")
			dust.Transform:SetPosition(inst:GetPosition():Get())
			dust.Transform:SetScale(4,4,4)
			inst:Remove()
		end
	end)

	--inst:AddComponent("fishable")
	--inst.components.fishable:SetRespawnTime(480)
	--inst.components.fishable:AddFish("fish")
	
	inst.frozen = false
    inst.no_wet_prefix = true
	
	inst:ListenForEvent("snowcoverchange", function() OnSnowCoverChange(inst) end, GetWorld())
	
	inst.OnSave = function(inst, data)
		if inst.fishes then 
			data.fishes = inst.fishes
		end
    end     
    
    inst.OnLoad = function(inst, data) 
		OnSnowCoverChange(inst)
		if data and data.fishes then
			inst.fishes = data.fishes
		end
    end
	return inst
end

STRINGS.NAMES.INFESTPOND = "Infest Pond"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.INFESTPOND = "Many larva inside!"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.INFESTPONDCLEAN = "The fishes made a rampage."

return Prefab("marsh/objects/infestpond", commonfn, assets, prefabs)