local sickness=container:get("sickness", {})
local stage_time_hours=1
local stage_time_minutes=5
local stage_additional_sound_base_time=stage_time_hours*60+stage_time_minutes
local stage_additional_sound_time_divider={
	[1]=3,
	[2]=5,
	[3]=6,
	[4]=9,
	[5]=9
}

local sick_corpses={
	["rat_plague"]=false,
	["zombie_parazit"]=false,
	["zombie_plague"]=false,
	["fly_plague"]=false
}

function is_infects_corpse(section)
	return sick_corpses[section]
end

function is_already_sick()
	return sickness.stage~=0
end

function try_to_start(corpse,check_outfit)
	if sick_corpses[corpse:section()] then --пробуем паразита
		local c_infect_rev=1
		if check_outfit then
			local outfit= db.actor:item_in_slot(inventory_slots.OUTFIT)
			if outfit then
				local c_infect_outfit_rev = art_hit.get_closed_armor()[outfit:section()]
				c_infect_rev=c_infect_outfit_rev and c_infect_outfit_rev or 1
			end
		end
		if math.random()<c_infect_rev then
			if not sickness then
				sickness={["stage"]=1}
			else
				sickness.stage=1
			end
			if not Timer:has("sickness_stage") then
				level.add_pp_effector("duality_circle.ppe", 8899, false)
				additional_sounds(true)
				set_sickness_icon(1)
				Timer:start_game("sickness_stage",0,stage_time_hours,stage_time_minutes,"sickness.sickness_effector()")
			end
		end
	end
end

local sickness_sounds={
	[[infection\infection_1]],
	[[infection\infection_2]],
	[[infection\infection_3]],
	[[rotting\rotting_10]]
}

function play_sickness_sound(stage)
	local snd=voice(sickness_sounds[stage])
	snd.volume = 2.0
	snd:play(db.actor, 0, sound_object.s2d)
end

local stage_icons={
	[1]="ui_bio_infection_green",
	[2]="ui_bio_infection_yellow",
	[3]="ui_bio_infection_orange",
	[4]="ui_bio_infection_red"
}

local ui_mw=nil
function get_ui() --for debug purpose
	if not ui_mw then
		set_sickness_icon(1) 
	end
	return ui_mw
end

function set_sickness_icon(stage)
	if ui_mw==nil then
		ui_mw=CUIStatic()
		local width=50
		if is_16_9_mode() then
			width=40
		elseif is_21_9_mode() then
			width=30
		end
		ui_mw:Init(3,0,width,48)
		ui_mw:SetStretchTexture(true)
		ui_mw:Show(true)	
		ui_mw:SetAutoDelete(false)
		ui_mw:SetWindowName("ui_bio_infection")
	end
	local icons_view=get_mainwnd():GetIconsView()
	if not icons_view:IsChildWindow(ui_mw) then
		icons_view:AddWindow(ui_mw)
	end
	if stage==0 then
		icons_view:RemoveWindow(ui_mw)
	else
		ui_mw:InitTexture(stage_icons[stage])
	end
end

function additional_sounds(first_run)
	if not db.actor:alive() then
		return
	end
	if sickness.stage and sickness.stage>0 then
		if not first_run then
			local snd_obj = sound_object("infection\\rotting_"..tostring(math.random(1,11)))  
			snd_obj:play_at_pos(db.actor, vector():set(0,0,0), 0, sound_object.s2d)
			db.actor.psy_health = -0.185
                        db.actor.power = -0.82
		end
		if not Timer:has("sickness_additional_sounds") then
			local next_sound_time=math.floor(stage_additional_sound_base_time/stage_additional_sound_time_divider[sickness.stage])
			Timer:start_game("sickness_additional_sounds", 0,0,next_sound_time, "sickness.additional_sounds()")
		end
	end
end

local sickness_stages={
	[1]=function(restore_state)
		set_sickness_icon(1)
		play_sickness_sound(1)
		if not restore_state then
			db.actor.psy_health = -0.4
                        db.actor.power = -0.88
			level.add_pp_effector("duality_circle.ppe", 8899, false)
			level.add_cam_effector("camera_effects\\head_shot.anm", 8899, false, "")
			Timer:start_game("sickness_stage",0,stage_time_hours,stage_time_minutes,"sickness.sickness_effector()")
		end
	end,
	[2]=function(restore_state) 
		set_sickness_icon(2)
		play_sickness_sound(2)
		if not restore_state then
			db.actor.psy_health = -0.4
                        db.actor.power = -0.9		
			level.add_pp_effector("duality_circle.ppe", 8899, false)
			level.add_cam_effector("camera_effects\\head_shot.anm", 8899, false, "")		
			Timer:start_game("sickness_stage",0,stage_time_hours,stage_time_minutes,"sickness.sickness_effector()")
		end
	end,
	[3]=function(restore_state) 
		set_sickness_icon(3)
		play_sickness_sound(3)
		if not restore_state then
			db.actor.psy_health = -0.4
                        db.actor.power = -0.95		
			level.add_pp_effector("duality_circle.ppe", 8899, false)
			level.add_cam_effector("camera_effects\\head_shot.anm", 8899, false, "")
			Timer:start_game("sickness_stage",0,stage_time_hours,stage_time_minutes,"sickness.sickness_effector()")
		end
	end,
	[4]=function(restore_state)
		set_sickness_icon(4)
		play_sickness_sound(4)
		if not restore_state then
			level.add_pp_effector("duality_circle.ppe", 8899, false)
			db.actor.psy_health = -0.55
                        db.actor.power = -0.98
			Timer:start_game("sickness_stage",0,1,0,"sickness.sickness_effector()")
		end
	end,
	[5]=function(restore_state)
		db.actor:kill(db.actor)
	end
}


function sickness_effector(restore_state)
	if restore_state==nil then
		restore_state=false
	end
	local stage=sickness_stages[sickness.stage]
	if stage then
		if sickness.stage~=0 then
			stage(restore_state)
			if restore_state then
				return
			end
		end
	else
		sickness.stage=0
		return
	end
	sickness.stage=sickness.stage+1
end

function clear_sickness()
	sickness.cure=
	{
		["started"]=true,
		["alpha_step"]=255,
		["alpha_speed"]=2,
		["alpha_direction"]=1
	}
        Timer:start_game_ms("clear_sickness_timed",50000+sickness.stage*10000,"sickness.clear_sickness_timed()")
end

function update_cure_anim()
	if not ui_mw or not sickness.cure or not sickness.cure.started then
		return
	end
	ui_mw:SetColor(subst_alpha(ui_mw:GetColor(),sickness.cure.alpha_step))
	sickness.cure.alpha_step=sickness.cure.alpha_step-sickness.cure.alpha_speed*sickness.cure.alpha_direction
	if sickness.cure.alpha_step <=0 then
		sickness.cure.alpha_step=0
		sickness.cure.alpha_direction=-1
	elseif sickness.cure.alpha_step >=255 then
		sickness.cure.alpha_step=255
		sickness.cure.alpha_direction=1
	end
end

function clear_sickness_timed()
	sickness.stage=0
	sickness.cure.started=false
	Timer:remove("sickness_stage")
	Timer:remove("sickness_additional_sounds")
	set_sickness_icon(0)
end