require "prefabutil"

local assets=
{
	Asset("ANIM", "anim/asiahouse2.zip"),
	Asset("ATLAS", "images/inventoryimages/asiahouse2.xml")
}


local function onhammered(inst, worker)
	inst.components.lootdropper:DropLoot()
	SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition())
	inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
	inst:Remove()
end

local function onhit(inst, worker)
	inst.AnimState:PlayAnimation("hit")
	inst.AnimState:PushAnimation("idle", true)
end

local function onfinished(inst)
	inst.AnimState:PlayAnimation("hit")
	inst:ListenForEvent("animover", function(inst, data) inst:Remove() end)
	inst.SoundEmitter:PlaySound("dontstarve/common/tent_dis_pre")
	inst.persists = false
	inst:DoTaskInTime(16*FRAMES, function() inst.SoundEmitter:PlaySound("dontstarve/common/tent_dis_twirl") end)
end

local function onbuilt(inst)
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:PushAnimation("idle", true)
end

local function onsleep(inst, sleeper)


	if GetClock():IsDusk() or GetClock():IsNight() then
		local tosay = "ANNOUNCE_NONIGHTSIESTA"
		if GetWorld():IsCave() then
			tosay = "ANNOUNCE_NONIGHTSIESTA_CAVE"
		end
		if sleeper.components.talker then
			sleeper.components.talker:Say(GetString(sleeper.prefab, tosay))
			return
		end
	end
	
	local hounded = GetWorld().components.hounded

	local danger = FindEntity(inst, 10, function(target) return target:HasTag("monster") or target.components.combat and target.components.combat.target == inst end)
	
	if hounded and (hounded.warning or hounded.timetoattack <= 0) then
		danger = true
	end
	
	if danger then
		if sleeper.components.talker then
			sleeper.components.talker:Say(GetString(sleeper.prefab, "ANNOUNCE_NODANGERSIESTA"))
		end
		return
	end

	if sleeper.components.hunger.current < TUNING.CALORIES_MED then
		sleeper.components.talker:Say(GetString(sleeper.prefab, "ANNOUNCE_NOHUNGERSIESTA"))
		return
	end
	
	sleeper.components.health:SetInvincible(true)
	sleeper.components.playercontroller:Enable(false)

	GetPlayer().HUD:Hide()
	TheFrontEnd:Fade(false,1)

	inst:DoTaskInTime(1.2, function() 
		
		GetPlayer().HUD:Show()
		TheFrontEnd:Fade(true,1) 
		
		if GetClock():IsDusk() or GetClock():IsNight() then
			local tosay = "ANNOUNCE_NONIGHTSIESTA"
			if GetWorld():IsCave() then
				tosay = "ANNOUNCE_NONIGHTSIESTA_CAVE"
			end
			if sleeper.components.talker then
				sleeper.components.talker:Say(GetString(sleeper.prefab, tosay))
				sleeper.components.health:SetInvincible(false)
				sleeper.components.playercontroller:Enable(true)
				return
			end
		end
		
		if sleeper.components.sanity then
			sleeper.components.sanity:DoDelta(TUNING.SANITY_HUGE)
		end
		
		if sleeper.components.hunger then
			sleeper.components.hunger:DoDelta(-TUNING.CALORIES_MED, false, true)
		end
		
		if sleeper.components.health then
			sleeper.components.health:DoDelta(TUNING.HEALING_HUGE, false, "siestahut", true)
		end
		
	if IsDLCEnabled(REIGN_OF_GIANTS) then
		if sleeper.components.temperature and sleeper.components.temperature.current < TUNING.TARGET_SLEEP_TEMP then
			sleeper.components.temperature:SetTemperature(TUNING.TARGET_SLEEP_TEMP - 20)
		end
		
		local moisture_start = nil
		if sleeper.components.moisture and sleeper.components.moisture:GetMoisture() > 0 then
			moisture_start = sleeper.components.moisture.moisture
		end

		if moisture_start then
			sleeper.components.moisture.moisture = moisture_start - TUNING.SLEEP_MOISTURE_DELTA
			if sleeper.components.moisture.moisture < 0 then sleeper.components.moisture.moisture = 0 end
		end
		
		inst.components.finiteuses:Use()
		GetClock():MakeNextDusk()
		
		
	elseif not IsDLCEnabled(REIGN_OF_GIANTS) then
		--if sleeper.components.temperature then
		--	sleeper.components.temperature:SetTemperature(sleeper.components.temperature.maxtemp)
		--end
		
		inst.components.finiteuses:Use()
		GetClock():MakeNextDay()
		
	end
		
		sleeper.components.health:SetInvincible(false)
		sleeper.components.playercontroller:Enable(true)
		sleeper.sg:GoToState("wakeup")	
	end)

end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst:AddTag("house")
	inst:AddTag("tent")
	inst:AddTag("structure")
    
    MakeObstaclePhysics(inst, 1.5)
	
    inst.entity:AddDynamicShadow()
    inst.DynamicShadow:SetSize( 8, 6 )

    inst:AddTag("structure")
    anim:SetBank("asiahouse2")
    anim:SetBuild("asiahouse2")
    anim:PlayAnimation("idle")
    
	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon( "asiahouse2.tex" )

    inst:AddComponent("inspectable")

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(10)
	inst.components.workable:SetOnFinishCallback(onhammered)
	inst.components.workable:SetOnWorkCallback(onhit)

	inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(100)
    inst.components.finiteuses:SetUses(100)
    inst.components.finiteuses:SetOnFinished( onfinished )
	
    local light = inst.entity:AddLight()
    light:SetFalloff(.1)
    light:SetIntensity(.1)
    light:SetRadius(0.1)
    light:SetColour(20/255, 20/255, 20/255)
    light:Enable(true)
    inst.AnimState:SetBloomEffectHandle( "shaders/anim.ksh" )

	inst:AddComponent("sleepingbag")
	inst.components.sleepingbag.onsleep = onsleep
	MakeSnowCovered(inst, .01)
	inst:ListenForEvent( "onbuilt", onbuilt)
    return inst
end

return Prefab( "common/objects/asiahouse2", fn, assets),
		MakePlacer( "common/asiahouse2_placer", "asiahouse2", "asiahouse2", "idle" )